Android Studio系列教程六--Gradle多渠道打包

由于國內(nèi)Android市場眾多渠道,為了統(tǒng)計每個渠道的下載及其它數(shù)據(jù)統(tǒng)計,就需要我們針對每個渠道單獨(dú)打包,如果讓你打幾十個市場的包豈不煩死了,不過有了Gradle,這再也不是事了。

友盟多渠道打包

廢話不多說,以友盟統(tǒng)計為例,在AndroidManifest.xml里面會有這么一段:

里面的Channel_ID就是渠道標(biāo)示。我們的目標(biāo)就是在編譯的時候這個值能夠自動變化。

第一步 在AndroidManifest.xml里配置PlaceHolder

第二步 在build.gradle設(shè)置productFlavors

android{productFlavors{xiaomi{manifestPlaceholders=[UMENG_CHANNEL_VALUE:"xiaomi"]}_360{manifestPlaceholders=[UMENG_CHANNEL_VALUE:"_360"]}baidu{manifestPlaceholders=[UMENG_CHANNEL_VALUE:"baidu"]}wandoujia{manifestPlaceholders=[UMENG_CHANNEL_VALUE:"wandoujia"]}}}

或者批量修改

android{productFlavors{xiaomi{}_360{}baidu{}wandoujia{}}productFlavors.all{flavor->flavor.manifestPlaceholders=[UMENG_CHANNEL_VALUE:name]}}

很簡單清晰有沒有?直接執(zhí)行./gradlew assembleRelease, 然后就可以靜靜的喝杯咖啡等待打包完成吧。

assemble結(jié)合Build Variants來創(chuàng)建task

上一篇博客介紹了assemble這個命令,會結(jié)合Build Type創(chuàng)建自己的task,如:

./gradlew assembleDebug

./gradlew assembleRelease

除此之外assemble還能和Product Flavor結(jié)合創(chuàng)建新的任務(wù),其實(shí)assemble是和Build Variants一起結(jié)合使用的,而Build Variants=Build Type+Product Flavor, 舉個例子大家就明白了:

如果我們想打包wandoujia渠道的release版本,執(zhí)行如下命令就好了:

./gradlew assembleWandoujiaRelease

如果我們只打wandoujia渠道版本,則:

./gradlew assembleWandoujia

此命令會生成wandoujia渠道的Release和Debug版本

同理我想打全部Release版本:

./gradlew assembleRelease

這條命令會把Product Flavor下的所有渠道的Release版本都打出來。

總之,assemble命令創(chuàng)建task有如下用法:

**assemble**: 允許直接構(gòu)建一個Variant版本,例如assembleFlavor1Debug。

**assemble**: 允許構(gòu)建指定Build Type的所有APK,例如assembleDebug將會構(gòu)建Flavor1Debug和Flavor2Debug兩個Variant版本。

**assemble**: 允許構(gòu)建指定flavor的所有APK,例如assembleFlavor1將會構(gòu)建Flavor1Debug和Flavor1Release兩個Variant版本。

完整的gradle腳本

最后福利大放送,來一份我在項(xiàng)目中使用的完整的gradle文件配置:

applyplugin:'com.android.application'defreleaseTime(){returnnewDate().format("yyyy-MM-dd",TimeZone.getTimeZone("UTC"))}android{compileSdkVersion21buildToolsVersion'21.1.2'defaultConfig{applicationId"com.boohee.*"minSdkVersion14targetSdkVersion21versionCode1versionName"1.0"http://dex突破65535的限制multiDexEnabledtrue//默認(rèn)是umeng的渠道m(xù)anifestPlaceholders=[UMENG_CHANNEL_VALUE:"umeng"]}lintOptions{abortOnErrorfalse}signingConfigs{debug{//Nodebugconfig}release{storeFilefile("../yourapp.keystore")storePassword"your password"keyAlias"your alias"keyPassword"your password"}}buildTypes{debug{//顯示LogbuildConfigField"boolean","LOG_DEBUG","true"versionNameSuffix"-debug"minifyEnabledfalsezipAlignEnabledfalseshrinkResourcesfalsesigningConfigsigningConfigs.debug}release{//不顯示LogbuildConfigField"boolean","LOG_DEBUG","false"minifyEnabledtruezipAlignEnabledtrue//移除無用的resource文件shrinkResourcestrueproguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'signingConfigsigningConfigs.releaseapplicationVariants.all{variant->variant.outputs.each{output->defoutputFile=output.outputFileif(outputFile!=null&&outputFile.name.endsWith('.apk')){//輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apkdeffileName="boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"output.outputFile=newFile(outputFile.parent,fileName)}}}}}//友盟多渠道打包productFlavors{wandoujia{}_360{}baidu{}xiaomi{}tencent{}taobao{}...}productFlavors.all{flavor->flavor.manifestPlaceholders=[UMENG_CHANNEL_VALUE:name]}}dependencies{compilefileTree(dir:'libs',include:['*.jar'])compile'com.android.support:support-v4:21.0.3'compile'com.jakewharton:butterknife:6.0.0'...}

大家有問題或疑問、建議歡迎博客留言,Android Studio的教程暫且到這里結(jié)束了,相信大家基本的都已會使用了,還有其他技巧與操作靠大家自己摸索了,之后有時間也會在博客上整理下一些Tips之類的,歡迎大家關(guān)注。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容