Android Studio's Gradle multi-channel packaging

Source: Internet
Author: User

Android Studio's Gradle multi-channel packaging


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 the gradle, it is simple.

Friend Alliance multi-channel packagingNeedless 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" ]        }    }  }

or batch Modification

Android {      Productflavors {        Xiaomi {}        _360 {}        Baidu {}        Wandoujia {}    }      productflavors.all {         Flavor-flavor.manifestplaceholders = [Umeng_channel_value:name]     }}

Very simple, direct execution./gradlew Assemblerelease, then you can wait for the package to complete silently.


Assemble combining build variants to create a taskAssemble this command creates its own task in conjunction with the build Type, such as:
    • ./gradlew Assembledebug
    • ./gradlew Assemblerelease
If we want to pack the release version of the Wandoujia channel, do the following:
./gradlew assemblewandoujiarelease
If we only hit the Wandoujia channel version:
./gradlew Assemblewandoujia
This command generates release and debug versions of the Wandoujia channel

Similarly I want to play all release version:
./gradlew Assemblerelease
This command will play out all the release versions of all channels under product flavor.
In summary, the assemble 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 two variant versions of Flavor1debug and Flavor2debug.
* * assemble * *: Allows 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

A final copy of the complete gradle file configuration I used in the project:

Apply plugin: ' Com.android.application ' def releasetime () {return new Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC"))}        Android {compilesdkversion buildtoolsversion ' 21.1.2 ' defaultconfig {applicationid "com.boohee.*" Minsdkversion targetsdkversion Versioncode 1 Versionname "1.0"//DEX breakout 65535    The limit multidexenabled true//default is Umeng channel manifestplaceholders = [Umeng_channel_value: "Umeng"]}        lintoptions {Abortonerror false} signingconfigs {debug {//No debug Config} Release {StoreFile file ("..  /yourapp.keystore ") Storepassword" Your password "Keyalias" your alias "Keypassword" Your Password "}} buildtypes {debug {//Display LOG Buildconfigfield" boolean "," Log_de BUG "," true "Versionnamesuffix"-debug "minifyenabled false ZIpalignenabled false shrinkresources false Signingconfig Signingconfigs.debug} releas            e {//Do not display LOG Buildconfigfield "boolean", "Log_debug", "false" minifyenabled true zipalignenabled true//Remove useless resource file shrinkresources true Proguardfiles Getdefault Proguardfile (' Proguard-android.txt '), ' Proguard-rules.pro ' Signingconfig signingconfigs.release APPL  Icationvariants.all {variant-variant.outputs.each {output, def outputFile =                    Output.outputfile if (outputFile! = null && outputFile.name.endsWith ('. apk ')) { The output apk name is called boohee_v1.0_2015-01-15_wandoujia.apk def fileName = "Boohee_v${defaultconfig.versio Nname}_${releasetime ()}_${variant.productflavors[0].name}.apk "output.outputfile = new File (OutputF   Ile.parent, FileName)                 }}}}}//Friends multi-channel packaging productflavors {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.jakew harton:butterknife:6.0.0 ' ...}


Reference: http://stormzhang.com/posts.html


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Studio's 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.