Gradle configuration multi-channel packaging detailed explanation, so you no longer worry about multi-channel packaging! ! !

In short:

       I haven’t updated my blog recently because I have just changed jobs and there are many new tasks. As a result, my blog has not been updated. After changing to a new environment, I have been exposed to more knowledge. I have been doing the company’s own offline products before. , I almost forgot how to package in multiple channels. Recently, the new company project went online, and I had to rearrange this knowledge point. Just to share, how to configure multi-channel packaging with Gradle. (Now there are so many company projects, which means I’m tired!!! ) Let me share the detailed steps of packaging via Gradle:

 1. Configure the production signature of Debug to facilitate debugging

   // 配置Debug的生产签名,方便调试
    signingConfigs {
        release {
            storeFile file("签名文件名称.key")
            storePassword "签名文件密码"
            keyAlias "别名"
            keyPassword "签名文件密码"
        }
        debug {
            storeFile file("签名文件名称.key")
            storePassword "签名文件密码"
            keyAlias "别名"
            keyPassword "签名文件密码"
        }
    }

What if there is no signature file? I think android development should all be configured, the steps to generate signature:

1. Click build

2. Select Generate sigend Bundle/apk...

3. The following window pops up, select apk and click next.

4. I have a signature file. I selected the signature file, plus the password and alias configured above. If the Create new button is not selected:

5. Enter the alias here, set the password, select the alias location and other information, and a new alias will be generated. I will not go into details here, after all, this is not the main task of today;

Here is how to generate a signature file is explained.

 

2. Add the channels you want to generate apk:

/*配置渠道*/
    productFlavors {
        android {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "android",
                                    app_name: "您的app名称"]
        }
        huawei {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huawei",
                                    app_name: "您的app名称"]
        }
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi",
                                    app_name: "您的app名称"]
        }
        vivo {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "vivo",
                                    app_name: "您的app名称"]
        }
        oppo {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "oppo",
                                    app_name: "您的app名称"]
        }
        
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu",
                                    app_name: "您的app名称"]
        }
    }

3. Generate apk name through Gradle, with channel name, timestamp, version number, version name and other information:

 android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = defaultConfig.applicationId + "_" +
                    defaultConfig.versionName + "_" + defaultConfig.versionCode + "-" +
                    new Date().format("yyyyMMddHHmm") + "_" + productFlavors[0].getAt("name") +
                    ".apk"
        }
    }

4. Quote the app name of Gradle in the AndroidManfist.xml file, as shown in the following figure:

5. If you want to do statistics on Youmeng, you need to add the following code to get the channel name in Gradle:

 // 获取 UMENG_CHANNEL 对应的value
    public static String getChannelValue (Context context){
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            String value = info.metaData.getString("UMENG_CHANNEL");
            return value;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

6. Pack, pack according to the channel you need, (the signature method provided above is the method of packaging, not explained in detail)

Click Finish to package, the following figure shows the package result:

At this point, the multi-channel packaging of Gradle configuration is over. I hope it can help you. If it helps you, please help me. I will continue to share more benefits for everyone. . .

Guess you like

Origin blog.csdn.net/wk_beicai/article/details/109489830