Android Studio Tutorial (vi) : Gradle multi channel package

  • 2020-06-15 10:17:57
  • OfStack

Due to the large number of channels in the domestic Android market, in order to count the downloads and other data statistics of each channel, we need to pack separately for each channel. It would be boring for you to pack packages for more than 10 markets, but with Gradle, this is no longer a problem.

Umeng multi-channel package

Without further ado, for example, in AndroidManifest.xml there is a paragraph like this:


<meta-data
    android:name="UMENG_CHANNEL"
    android:value="Channel_ID" />

Channel_ID inside is the channel designation. The goal is to have this value change automatically at compile time.

Step 1 Configure PlaceHolder in ES16en.ES17en


<meta-data
    android:name="UMENG_CHANNEL"
    android:value="${UMENG_CHANNEL_VALUE}" />

Step 2 Set productFlavors on ES22en.gradle

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]
    }
}

It's simple, it's clear, right? Direct execution./gradlew assembleRelease, then you can quietly drink a cup of coffee and wait for the packaging to complete.

assemble combines Build Variants to create task

In my last blog, I introduced the command assemble. I will combine Build Type to create my own task, such as:


./gradlew assembleDebug ./gradlew assembleRelease

In addition, assemble can also combine with Product Flavor to create new tasks. In fact, assemble is used in combination with Build Variants 1, and Build Variants = Build Type + Product Flavor.

If we want to package the release version of the wandoujia channel, just execute the following command:


./gradlew assembleWandoujiaRelease

If we only type wandoujia channel version, then:

./gradlew assembleWandoujia

This command generates versions of Release and Debug for the wandoujia channel

Similarly, I would like to type all versions of Release:


./gradlew assembleRelease

This command will type out the Release version for all channels under Product Flavor.

In summary, the assemble command creates task as follows:

**assemble** : Allows you to directly build 1 VERSION of Variant, such as assembleFlavor1Debug.

**assemble** : Allows you to build all APK that specify Build Type. For example, assembleDebug will build Flavor1Debug and Flavor2Debug.

**assemble** : Allows you to build all APK that specify flavor. For example, assembleFlavor1 will build Flavor1Debug and Flavor1Release.

The complete gradle script

Finally, a copy of the complete gradle file configuration that 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 breakthrough 65535 The limits of
        multiDexEnabled true
        // The default is umeng The 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 {
            // According to Log
            buildConfigField "boolean", "LOG_DEBUG", "true"             versionNameSuffix "-debug"
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }         release {
            // Don't show Log
            buildConfigField "boolean", "LOG_DEBUG", "false"             minifyEnabled true
            zipAlignEnabled true
            // Remove useless resource file
            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')) {
                     // The output apk The name for 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)
                    }
                }
            }
        }
    }     // Umeng multi-channel package
    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'
    ...
}

You have questions or questions, Suggestions welcome blog comments, Android Studio's tutorial is over for now, I believe you have basically used, there are other skills and operations by you to explore, after there will be time to sort out on the blog under 1 Tips and the like, welcome to pay attention to.


Related articles: