使用gradle構建android項目(續)
在幾個月之前,我已經寫過一篇使用gradle構建android項目的博客了http://blog.isming.me/2014/05/20/android4gradle/,那篇文章已經介紹了如何使用gradle進行項目構建,以及為谷歌會推薦使用gradle。當時android的gradle插件是0.11.0,現在插件的版本已經是0.14.3了,對于一些老的方法和api,有一些已經被移除,無法使用。因此有必要再寫一篇博客介紹這些被移除的部分和替代方案。同時由于個人學識原因,當時沒有介紹的一些技巧,其他功能,也會在本文中進行介紹。
和上一篇文章相比不兼容的地方
沒有看過我另一篇文章的,建議去看一下。
以下這些屬性改名,原先的不能用:
runProguard -> minifyEnabled (是否混淆)
zipAlign -> zipALignEnabled (是否zip對齊)
packageName -> applicationId
jniDebugBuild-> jniDebuggable
renderscriptDebug->renderscriptDebuggable
renderscriptSupportMode->renderscriptSupportModeEnabled
renderscriptNdkMode->renderscriptNdkModeEnabled
Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest使用variant.out代替,具體使用,看后面代碼
這些被移除替換的,在最新版的gradle插件中,已經不會提示過時,直接報錯,請警惕啊!!!!
新功能
multiDexEnabled 多dex支持
shrinkResources 移除未使用的資源
支持定義BuildConfig值和res的值,比如:
applicationVariants.all { variant -> variant.buildConfigField "int", "VALUE", "1" variant.resValue "string", "name", "value" }
還可以在defaultConfig
,buildType
,productFlavors
中定義,比如:
buildTypes { debug { applicationIdSuffix ".debug" signingConfig signingConfigs.myConfigbuildConfigField "String", "FOO", "\"bar1\"" buildConfigField "String", "FOO", "\"bar\"" resValue "string", "foo", "foo2" } }</pre>
通過這樣,我們可以對我們生成的最終程序,進行多樣劃的定制了。
Manifest文件內容占位符
這樣可以打包的時候,對Manifest進行自定義配置,使用方法:
在Manifest文件中定義一個占位符,比如以我們之前寫的umeng打包的例子為例,${UMENG_CHANNEL},這種格式.
在gradle配置文件中加替換,可以在
defaultConfig
,buildType
,productFlavors
中配置,比如: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和gradle
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.+' }</pre>
然后,我把谷歌最新的gradle配置的示例也拿回來了,分享給大家:點擊下載
參考資料:http://tools.android.com/tech-docs/new-build-system
原文地址:http://blog.isming.me/2014/11/21/use-gradle-new/,轉載請注明出處。