Reprint Address: http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/
Because of the many channels in the domestic Android market, in order to count the download of each channel and other data statistics, we need to be packaged separately for each channel, if let you hit dozens of market package is not bored to death, but with Gradle, this is no longer a thing.
Friend Alliance multi-channel packaging
Needless to say, in the case of Friends of the league statistics, in Androidmanifest.xml there will be such a paragraph:
<meta-data android:name="UMENG_CHANNEL" android:value="Channel_ID" />
The channel_id inside is the channel mark. Our goal is to automatically change this value at compile time.
- The first step is to configure placeholder in Androidmanifest.xml.
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
- The second step is set in Build.gradle productflavors
Android{Productflavors{Xiaomi{Manifestplaceholders=[Umeng_channel_value:"Xiaomi"] } _360 {manifestplaceholders Span class= "o" >= [umeng_channel_value: "_360" Span class= "O" >] } baidu {manifestplaceholders Span class= "o" >= [umeng_channel_value: "Baidu" Span class= "O" >] } wandoujia {manifestplaceholders = [umeng_channel_value: " Wandoujia "" } } }
or batch Modification
android { productFlavors { xiaomi {} _360 {} baidu {} wandoujia {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }}
Very simple and clear, is there? Execute directly ./gradlew assemblerelease , then you can have a quiet cup of coffee and wait for the package to finish.
Assemble combining build variants to create a task
The previous blog describes the assemble command, which creates its own task in conjunction with the build Type , such as:
In addition, assemble can be combined with Product Flavor to create a new task, in fact assemble is used in conjunction with Build variants , and build variants = Build Type + Product Flavor , for example everyone understands:
If we want to pack the release version of the Wandoujia channel, it's good to do the following:
- ./gradlew Assemblewandoujiarelease
If we only play Wandoujia channel version, then:
- ./gradlew Assemblewandoujia
This command generates release and debug versions of the Wandoujia channel
In the same vein, I want to play all release versions:
- ./gradlew Assemblerelease
This command will flavor the release version of all the channels under product.
In summary, theassemble command creates a task with the following usage:
**assemble**: Allows you to build a variant version directly, such as Assembleflavor1debug.
**assemble**: Allows you to build all the APK for the specified build type, for example Assembledebug will build Flavor1debug and Flavor2debug two variant versions.
**assemble**: Allows all apk to be built for the specified flavor, for example AssembleFlavor1 will build Flavor1debug and Flavor1release two variant versions.
The complete Gradle script
Finally, a great benefit, a full gradle file configuration that I used in the project:
ApplyPlugin:' Com.android.application 'DefReleasetime(){ReturnNewDate().Format("Yyyy-mm-dd",TimeZone.getTimeZone("UTC"))}Android{Compilesdkversion21stBuildtoolsversion' 21.1.2 'Defaultconfig{ApplicationID"Com.boohee.*"Minsdkversion14Targetsdkversion21stVersioncode1Versionname"1.0"//DexBreakthrough65535The restrictionsMultidexenabledTrue//Default isUmengThe ChannelManifestplaceholders=[Umeng_channel_value:"Umeng"]}Lintoptions{AbortonerrorFalse}Signingconfigs{Debug{//NoDebugConfig}Release{StoreFileFile(".. /yourapp.keystore ")Storepassword"Your password"Keyalias"Your Alias"Keypassword"Your password"}}Buildtypes{Debug{//ShowLogBuildconfigfield"Boolean","Log_debug","True"Versionnamesuffix"-debug"MinifyenabledFalseZipalignenabledFalseShrinkresourcesFalseSigningconfigSigningconfigs.Debug}Release{//Do not showLogBuildconfigfield"Boolean","Log_debug","False"MinifyenabledTrueZipalignenabledTrue//Remove the UselessResourceFileShrinkresourcesTrueProguardfilesGetdefaultproguardfile(' Proguard-android.txt '),' Proguard-rules.pro 'SigningconfigSigningconfigs.ReleaseApplicationvariants.All{Variant-Variant.Outputs.each{Output-DefOutputFile=Output.OutputFileIf(OutputFile!=Null&&OutputFile.Name.EndsWith('. apk ')){//OutputapkName calledBoohee_v1.0_2015-01-15_wandoujia.apkDefFileName="Boohee_v${defaultconfig.versionname}_${releasetime ()}_${variant.productflavors[0].name}.apk"Output.OutputFile=NewFile(OutputFile.Parent,FileName)}}}}}//Friend Alliance multi-channel packagingProductflavors{Wandoujia{}_360{}Baidu{}Xiaomi{}Tencent{}Taobao{}...}Productflavors.All{Flavor-Flavormanifestplaceholders = [umeng_channel_value< Span class= "P" >: name }} Dependencies {compile filetree ( Dir: ' Libs ' include: [ ' *.jar ' ] compile ' com.android.support:support-v4:21.0.3 ' compile com.jakewharton:butterknife:6.0.0 ' . /span>
More Android Studio Tutorials: Http://stormzhang.com/categories.html#devtools-ref
Android Studio Series Tutorial six--gradle multi-channel packaging