Gradle multi-channel packaging

Keywords: Android Gradle xml

Multi-channel packaging based on umeng statistics in Studio.

1. Configuring the Alliance Environment

Add relevant information in the < Application > tag of AndroidMainfest.xml: APP_KEY and channel number. Here the channel number is just a placeholder, which can dynamically replace the information in the placeholder during the packaging stage.

<application
    ... >
    <meta-data
        android:name="UMENG_APPKEY"
        android:value="@string/UMENG_APPKEY"/>
    <meta-data
        android:name="UMENG_CHANNEL" 
        android:value="${UMENG_CHANNEL_VALUE}" 
        />
    ...
</application>

2. Configuring signature information

Add the following information to build.gradle in app module: alias, alias password, signature file address, and signature password

signingConfigs {
    release {
        keyAlias 'xxx'
        keyPassword 'xxx'
        storeFile file('C:/Users/Administrator/Desktop/xxx.jks')
        storePassword 'xxx'
    }
}

3. Setting up the signature configuration of buildTypes

Because you want to type release package, you need to set the build Types signature configuration for release, as follows

buildTypes {
    // Setting default channel settings for debug version
    debug {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng_channel"]
    }
    // Official version signature settings
    release {
        zipAlignEnabled true
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release // Add this sentence.
    }
}

4. Configuring Channel Information

Channel numbers are self-made, such as wandoujia (pea pods). Don't use names that start with numbers, and don't use names like unknow. build.gradle(Module:app) sets product Flavors. The channel number placeholder ${UMENG_CHANNEL_VALUE} in xml is to dynamically replace the placeholder content. The configuration information is as follows:

android {
    productFlavors {
        kuan {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "kuan"]
        }
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        qh360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }
    }
}

5. Execute packing commands

gradlew clean
gradlew assembleRelease

3.sycn project can be packaged after a while, Build - > Generate signed APK, Flavors in multiple options, choose the channel you need

5. Complete, pay attention to creating directories

6. Hiding Signature Information

Signature information, which belongs to private information, should be protected, preferably not written in build.gradle, let alone uploaded to the code warehouse. It should be placed in a separate file and referenced in build.gradle to read the signature information.

Clear signing Configs
// TODO

7. Tracking the packaging process

It is recommended to add the -- stacktrace parameter after the command, so that in case the command fails to execute, you can see the detailed error information.

gradlew clean --stacktrace
gradlew assembleRelease --stacktrace

8. File name of customized apk: app_version name_version number_date_channel number.apk

def packageTime() {
    return new Date().format("yyyyMMdd-HHmmss", TimeZone.getTimeZone("UTC"))
}

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // Rename the output apk file
            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}_${defaultConfig.versionCode}_${packageTime()}_${variant.productFlavors[0].name}.apk"
                } else {
                    fileName = "app_v${defaultConfig.versionName}_${defaultConfig.versionCode}_${packageTime()}_debug.apk"
                }
                output.outputFile = new File(outputDirectory, fileName)
            }
        }
    }
}

Output directory: app/build/outputs/apk

9. Delete redundant unaligned apk files

In the app/build/outputs/apk folder, you can see some redundant files with unaligned text at the end of the file name. These files are useless and can be deleted. Just add the following commands to build.gradle

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // Rename the output apk file
        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}_${defaultConfig.versionCode}_${packageTime()}_${variant.productFlavors[0].name}.apk"
            } else {
                fileName = "app_v${defaultConfig.versionName}_${defaultConfig.versionCode}_${packageTime()}_debug.apk"
            }
            output.outputFile = new File(outputDirectory, fileName)
        }

        // Delete unaligned apk
        if (output.zipAlign != null) {
            output.zipAlign.doLast {
                output.zipAlign.inputFile.delete()
            }
        }
    }
}

Posted by kratos-2012 on Sun, 03 Feb 2019 08:51:16 -0800