Android Studio Series Tutorial six--gradle multi-channel packaging

Source: Internet
Author: User

Original link: http://segmentfault.com/a/1190000004050697?utm_source=tuicool&utm_medium=referral

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 = [Umeng_channel_value: "_360"] } Baidu {  manifestplaceholders = [Umeng_channel_value : "Baidu"] } Wandoujia {  manifestplaceholders = [umeng_channel_value: "Wandoujia"]< c22> }                }} 

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:

    • ./gradlew Assembledebug

    • ./gradlew Assemblerelease

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, the assemble command creates a task with the following usage:

    • Assemble<variant Name>: Allows a Variant version to be built directly, such as Assembleflavor1debug.

    • Assemble<build type Name>: Allows all apk to be built with the specified build type, for example Assembledebug will build Flavor1debug and Flavor2debug two variant versions.

    • Assemble<product Flavor name>: Allow all apk to be built for the specified Flavor, For example, AssembleFlavor1 will build two variant versions of Flavor1debug and Flavor1release.

The complete Gradle script

Finally, a great benefit, a full gradle file configuration that I used in the project:

Apply plugin:' Com.android.application ' def releasetime () {  ReturnNew Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC"))}android {Compilesdkversion21stBuildtoolsversion' 21.1.2 'Defaultconfig { ApplicationID"Com.boohee.*" Minsdkversion14 Targetsdkversion21st Versioncode1 Versionname"1.0"  Dex exceeds the limit of 65535 MultidexenabledTrue  The default is Umeng Channel Manifestplaceholders = [Umeng_channel_value:"Umeng"]}lintoptions { AbortonerrorFalse}Signingconfigs { Debug {   No Debug Config } Release {  StoreFile file (".. /yourapp.keystore ")  Storepassword"Your password"  Keyalias"Your Alias"  Keypassword"Your password" }}Buildtypes { Debug {   Show log  Buildconfigfield"Boolean","Log_debug","True"  Versionnamesuffix"-debug"  MinifyenabledFalse  ZipalignenabledFalse  ShrinkresourcesFalse  Signingconfig Signingconfigs.debug } Release {   Do not display log  Buildconfigfield"Boolean","Log_debug","False"  MinifyenabledTrue  ZipalignenabledTrue   Removing useless resource files  ShrinkresourcesTrue  Proguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '  Signingconfig Signingconfigs.release  Applicationvariants.all {variant-   Variant.outputs.each {output-    def outputFile = Output.outputfile     if (outputFile! =Null && outputFile.name.endsWith ('. apk ')) {      Output apk name is called boohee_v1.0_2015-01-15_wandoujia.apk     def fileName ="Boohee_v${defaultconfig.versionname}_${releasetime ()}_${variant.productflavors[0].name}.apk"     Output.outputfile =New File (OutputFile.Parent, FileName)    }   }  } }} Friend Alliance multi-channel packagingproductflavors { Wandoujia {} _360 {}  Baidu {}  Xiaomi {}  Tencent {}  Taobao {}  ... } Productflavors.all {flavor->  Flavor.manifestplaceholders = [Umeng_channel_value:name]}}dependencies { compile Filetree (dir:  ' Libs ', include: [ ' *.jar '])  compile  Com.android.support:support-v4 : 21.0.3 '  compile  ' com.jakewharton:butterknife:6.0.0 '  ...}              

We have questions or questions, suggestions welcome blog comments, Android Studio Tutorial for a moment to end here, I believe we have the basic will be used, there are other skills and operations rely on their own groping, and then there will be time on the blog to sort out some tips and so on, welcome attention.

Android Studio Series Tutorial six--gradle multi-channel packaging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.