Gradle multi-channel packaging for Android Studio
Gradle multi-channel packaging for Android Studio
As there are many channels in the Chinese Android Market, we need to package separately for each channel to collect statistics on downloads from each channel and other data. If you want to pack dozens of market packages, don't get bored, but with Gradle, this is simple.
Umeng multi-channel PackagingIn this case, the following section is used in AndroidManifest. xml:
The Channel_ID is the channel ID. Our goal is to automatically change this value during compilation.
Step 1 configure in AndroidManifest. xml
PlaceHolder
Step 2: set it in build. gradleProductFlavors
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 modify them in batches.
android { productFlavors { xiaomi {} _360 {} baidu {} wandoujia {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }}
Run./gradlew assembleRelease directly, and then wait for the package to complete.
Assemble and Build Variants to create a taskThe assemble command creates your own task in combination with Build Type, for example:
. /Gradlew assembleDebug. /gradlew assembleRelease In addition, assemble can also be used with Product Flavor to create new tasks. In fact, assemble is used together with Build Variants, while Build Variants = Build Type + Product Flavor.
If we want to package the release version of The wandoujia channel, execute the following command:
./Gradlew assemblewandoujiarel.pdf
If we only use the wandoujia channel version, then:
./Gradlew assembleWandoujia
This command generates the Release and Debug Versions of the wandoujia channel.
Similarly, I want to Release all the Release versions:
./Gradlew assumerelease
This command releases the Release versions of all channels under Product Flavor.
In short, the assemble command has the following usage to create a task:
**
Assemble**: You can directly build a Variant version, such as assembleFlavor1Debug.
**
Assemble**: Allows you to Build all the APK of the specified Build Type. For example, assembleDebug will Build two Variant versions: Flavor1Debug and Flavor2Debug.
**
Assemble**: Allows you to build all the APK of a specified flavor. For example, assembleFlavor1 will build two Variant versions: Flavor1Debug and Flavor1Release.
Complete gradle script
Finally, let's take a 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 21 buildToolsVersion '21. 1.2 'defaultconfig {applicationId com. boohee. * minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName 1.0 // dex exceeds the limit of 65535. multiDexEnabled true // The default value is umeng channel manifestPlaceholders = [UMENG_CHANNEL_VALUE: umeng]} lin TOptions {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_DEBUG, true allow-debug allow false deny false shrinkResources false signingConfig signingConfigs. debug} Release {// do not display Log buildConfigField boolean, LOG_DEBUG, false minifyEnabled true zipAlignEnabled true // Remove useless resource files shrinkResources true proguardFiles done'), 'proguard-rules. pro 'signingconfig signingConfigs. release applicationVariants. all {variant-> variant. outputs. each {output-> def outputFile = output. outputFile if (outputFile! = Null & outputFile.name.endsWith('.apk ') {// The output apkname is boohee_v1.0_2015-01-15_wandoujia.apk def fileName = export output. outputFile = new File (outputFile. parent, fileName) }}}// you can package productFlavors {wandoujia {}_ 360 {} baidu {} xiaomi {} tencent {} taobao {}...} in multiple channels {}...} 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 '...}