Android Studio Tutorial (vi): GRADLE multi-channel packaging _android

Source: Internet
Author: User

Because of the domestic Android market many channels, in order to calculate the download of each channel and other data statistics, we need to separate packaging for each channel, if you play dozens of of the market bag will not be annoying to death, but with the gradle, this is no longer a matter.

Friend Union multi-channel packing

No more nonsense, take the Friendship Union statistics as an example, in Androidmanifest.xml there will be such a paragraph:

Copy Code code as follows:

<meta-data
Android:name= "Umeng_channel"
Android:value= "channel_id"/>

The channel_id inside is the channel mark. Our goal is to automatically change the value at compile time.

The first step is to configure placeholder in Androidmanifest.xml.

Copy Code code as follows:

<meta-data
Android:name= "Umeng_channel"
Android:value= "${umeng_channel_value}"/>

The second step is to set the productflavors in Build.gradle
Copy Code code as follows:

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
Copy Code code as follows:

Android {
productflavors {
Xiaomi {}
_360 {}
Baidu {}
Wandoujia {}
}

Productflavors.all {
Flavor-> flavor.manifestplaceholders = [Umeng_channel_value:name]
}
}

Is it simple and clear? Direct execution./gradlew Assemblerelease, and then you can quietly drink a cup of coffee waiting to be completed.

Assemble combine build variants to create a task

The previous blog introduced the Assemble command, which creates its own task in conjunction with the build Type, such as:

Copy Code code as follows:

./gradlew Assembledebug

./gradlew Assemblerelease


In addition, assemble can also create new tasks with Product flavor, in fact assemble is used in conjunction with build variants, and builds variants = built Type + Product Fl Avor, for example, you will see:

If we want to pack the release version of the Wandoujia channel, execute the following command:

Copy Code code as follows:

./gradlew Assemblewandoujiarelease

If we only play Wandoujia channel version, then:
Copy Code code as follows:

./gradlew Assemblewandoujia

This command generates release and debug versions of the Wandoujia channel

For the same reason I want to play all release versions:

Copy Code code as follows:

./gradlew Assemblerelease

This command will call release versions of all channels under product flavor.

In summary, the assemble command creates a task with the following usage:

**assemble**: Allows direct construction of a variant version, such as Assembleflavor1debug.

**assemble**: Allows all apk of the specified build type to be built, for example, Assembledebug will build Flavor1debug and Flavor2debug two variant versions.

**assemble**: Allows you to build all the APK for the specified flavor, such as AssembleFlavor1 will build the Flavor1debug and Flavor1release two variant versions.

Complete Gradle Script

At the end of the welfare big broadcast, a complete gradle file configuration I used in the project:

Copy Code code as follows:

Apply plugin: ' Com.android.application '

Def releasetime () {
return new Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC")
}

Android {
Compilesdkversion 21
Buildtoolsversion ' 21.1.2 '

Defaultconfig {
ApplicationID "Com.boohee.*"
Minsdkversion 14
Targetsdkversion 21
Versioncode 1
Versionname "1.0"

Dex broke the 65535 limit.
multidexenabled true
The default is the 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 {
Show log
Buildconfigfield "Boolean", "Log_debug", "true"

Versionnamesuffix "-debug"
Minifyenabled false
Zipalignenabled false
Shrinkresources false
Signingconfig Signingconfigs.debug
}

Release {
Do not show log
Buildconfigfield "Boolean", "Log_debug", "false"

minifyenabled true
zipalignenabled true
removing unwanted resource files
Shrinkresources true
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 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 Union multi-channel packing
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.jakewharton:butterknife:6.0.0 '
...
}

We have questions or questions, suggestions welcome blog message, Android Studio tutorial to end here, I believe that all the basic will be used, there are other skills and operations rely on their own groping, and then have the time to be on the blog to organize some tips and so on, welcome everyone attention.

Related Article

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.