在幾個月以前,我已經寫過一篇使用gradle構建android項目的博客了http://blog.isming.me/2014/05/20/android4gradle/,那篇文章已經介紹瞭如何使用gradle進行項目構建,以及爲谷歌會推薦使用gradle。當時android的gradle插件是0.11.0,如今插件的版本已是0.14.3了,對於一些老的方法和api,有一些已經被移除,沒法使用。所以有必要再寫一篇博客介紹這些被移除的部分和替代方案。同時因爲我的學識緣由,當時沒有介紹的一些技巧,其餘功能,也會在本文中進行介紹。android
沒有看過我另外一篇文章的,建議去看一下。api
如下這些屬性更名,原先的不能用:app
runProguard -> minifyEnabled (是否混淆)
zipAlign -> zipALignEnabled (是否zip對齊) packageName -> applicationId jniDebugBuild-> jniDebuggable renderscriptDebug->renderscriptDebuggable
renderscriptSupportMode->renderscriptSupportModeEnabled
renderscriptNdkMode->renderscriptNdkModeEnabled
Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest使用variant.out代替,具體使用,看後面代碼jvm
這些被移除替換的,在最新版的gradle插件中,已經不會提示過期,直接報錯,請警戒啊!!!!maven
applicationVariants.all { variant -> variant.buildConfigField "int", "VALUE", "1" variant.resValue "string", "name", "value" }
還能夠在defaultConfig
,buildType
,productFlavors
中定義,好比:gradle
buildTypes { debug { applicationIdSuffix ".debug" signingConfig signingConfigs.myConfig buildConfigField "String", "FOO", "\"bar1\"" buildConfigField "String", "FOO", "\"bar\"" resValue "string", "foo", "foo2" } }
經過這樣,咱們能夠對咱們生成的最終程序,進行多樣劃的定製了。ui
這樣能夠打包的時候,對Manifest進行自定義配置,使用方法:spa
在Manifest文件中定義一個佔位符,好比以咱們以前寫的umeng打包的例子爲例,${UMENG_CHANNEL},這種格式.插件
在gradle配置文件中加替換,能夠在defaultConfig
,buildType
,productFlavors
中配置,好比:debug
defaultConfig { manifestPlaceholders = [ UMENG_CHANNEL:"defaultName"] }
同時,還能夠直接在Manifest文件中加包名的替換,直接使用${applicationId}
便可。
### 其餘技巧免費附送
若是使用過程當中常常出現OOM,那麼在gradle.properties
文件中增長一下內存,讓gradle可使用更多內存:
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError
若是由於中文問題,出現錯誤,最好在org.gradle.jvmargs後面再加上-Dfile.encoding=UTF-8
,那麼這個時候和在一塊兒就是:
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
若是,由於一些錯誤,不得不終止,再進來以後,沒法進行編譯,去projectpath/.gradle/<gradle-version>/taskArtifacts/
目錄下看有沒有*.lock
的文件,刪掉再重試。
android studio(如下簡稱as)今天發佈了1.0RC版,意味着正式版本的即將到來,同時在社區,QQ羣也能夠看到愈來愈多的人開始在使用android studio。常常也有不少人會問到升級的時候會遇到一些問題,主要緣由就是android studio的一些大版本升級後,通常有一個推薦gradle插件的版本,好比,as0.9要求0.14.+版本,as0.8要求0.12+版本。二者是互相依賴的,gradle插件的版本同時對於as也有最低版本要求。這樣,咱們升級as後也必須修改gradle的配置文件,提升插件版本,同時一些不能向下兼容的配置也須要修改。
在升級gradle和插件版本後,通常都會從新下載gradle,這樣會消耗你一點時間。
奉上我最近的妹子圖的gradle配置:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.14.+' } } apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.0" defaultConfig { minSdkVersion 9 targetSdkVersion 21 versionCode 3 versionName "1.1.1" multiDexEnabled false manifestPlaceholders = [ UMENG_CHANNEL_VALUE:"default_channel" ] buildConfigField "boolean", "ISDEBUG", "true" } lintOptions { abortOnError false } //簽名 signingConfigs { debug { //storeFile file("/home/sam/.android/debug.keystore") } //你本身的keystore信息 release { //storeFile file("/home/sam/sangmingming.keystore") //storePassword "" //keyAlias "sam" //keyPassword "" } } buildTypes { debug { signingConfig signingConfigs.debug buildConfigField "boolean", "ISDEBUG", "true" } release { buildConfigField "boolean", "ISDEBUG", "true" signingConfig signingConfigs.release minifyEnabled true zipAlignEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } //渠道Flavors,我這裏寫了一些經常使用的,大家本身改 productFlavors { //GooglePlay{} //NDuo{} xiaomi {} umeng {} } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } productFlavors.all { flavor -> flavor.manifestPlaceholders = [ UMENG_CHANNEL_VALUE:name ] } applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk") output.outputFile = new File(outputFile.parent, fileName) } } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.+' compile 'com.android.support:support-v4:21.+' compile 'com.android.support:cardview-v7:21.+' compile 'com.android.support:recyclerview-v7:21.+' }
而後,我把谷歌最新的gradle配置的示例也拿回來了,分享給你們:點擊下載
參考資料:http://tools.android.com/tech-docs/new-build-system
原文地址:http://blog.isming.me/2014/11/21/use-gradle-new/,轉載請註明出處。