The multi-channel packaging of gradle is very simple, because gradle has already done a lot of basic functions for us.
The following is an example of Umeng statistics. Generally, Umeng statistics will have such a statement in AndroidManifest.xml:
<meta-data android:name="UMENG_CHANNEL" android:value="CHANNEL_ID" />
Among them, CHANNEL_ID is the channel identifier of Umeng. The realization of multi-channel is generally realized by modifying the value of CHANNEL_ID.
Next, we will implement multi-channel version packaging step by step.
1. Configure PlaceHolder in AndroidManifest.xml and replace it with the value you want to set in the build.gradle file
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
2. Set productFlavors in build.gradle and modify the value of PlaceHolder
productFlavors { playstore { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "playStore"] } miui { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "miui"] } wandoujia { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] } }
or bulk edit
productFlavors { playstore { } miui {} wandoujia {} } // batch processing productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }
Follow the above two steps to compile and play the multi-channel package. The command is ./gradlew assembleRelease, which can package all the multi-channel packages.
If you just want to play a single channel package, you can execute the corresponding task. For example, gradle assemblePalyStoreRelease is the Release version of playing the PlayStore channel.
3. If you want, you can modify the final file name, if you need to generate different files for different needs. And modifying the file name is also very simple, refer to the following code to achieve
def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) } android{ applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { File outputDirectory = new File(outputFile.parent); def fileName if (variant.buildType.name == "release") { fileName = "app_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" } else { fileName = "app_v${defaultConfig.versionName}_${packageTime()}_debug.apk" } output.outputFile = new File(outputDirectory, fileName) } } } }
This method has certain limitations, that is, it will take a long time to compile after there are too many channel packages. Here, the third method of Meituan packaging is recommended.
full gradle script
// Declare an Android program apply plugin: 'com.android.application' // define a packing time def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) } android { // Build the version of the SDK compileSdkVersion 21 // version of build tools buildToolsVersion '21.1.2' defaultConfig { // application package name applicationId "com.**.*" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" // dex breaks the limit of 65535 multiDexEnabled true // Default is umeng's channel manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"] } // remove error from lint check lintOptions { abortOnError false } //Signature configuration signingConfigs { debug { // No debug config } release { storeFile file("../yourapp.keystore") storePassword "your password" keyAlias "your alias" keyPassword "your password" } } buildTypes { debug { // buildConfigField custom configuration default value buildConfigField "boolean", "LOG_DEBUG", "true" buildConfigField "String", "API_HOST", "\"http://api.test.com\""//API Hos versionNameSuffix "-debug" minifyEnabled false //Whether zip alignment zipAlignEnabled false shrinkResources false signingConfig signingConfigs.debug } release { // buildConfigField custom configuration default value buildConfigField "boolean", "LOG_DEBUG", "false" buildConfigField "String", "API_HOST", "\"http://api.release.com\""//API Host //// Whether to obfuscate minifyEnabled true zipAlignEnabled true // remove useless resource files shrinkResources true // obfuscation rule file 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')) { // The output apk name is boohee_v1.0_2015-06-15_wandoujia.apk def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } } } // Umeng multi-channel packaging productFlavors { wandoujia {} _360 {} baidu {} xiaomi {} tencent {} taobao {} ... } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } } dependencies { // Compile all jar packages in the libs directory compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:21.0.3' compile 'com.jakewharton:butterknife:6.0.0' ... }