Detailed steps of Android multi-channel packaging
1. What is multi-channel packaging
There may be different statistical requirements in different application markets. It is necessary to release an installation package for each application market. Here comes the multi-channel packaging of Android. Add different logos in the installation package to distinguish various channels, so as to facilitate the statistics of various applications in the market.
2. Several packaging methods
3. Start using
3.1 Youmeng Umeng
Step 1: add in Android manifest
<Meta-data android:name="UMENG_CHANNEL" android:value="${channel}" />
Step 2: add Baidu {} in build.gradle as the abbreviation of the specified channel name
build { ...... productFlavors { baidu {} xiaomi {} qihu360 {} yingyongbao {} huawei {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name] } }
Step 3: set the output APK name
Android studio version 2.3:
build { ...... applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def fileName = "driver_${variant.productFlavors[0].name}_v${defaultConfig.versionName}.apk" output.outputFile = new File(outputFile.parent,fileName) } } } }
Android studio version 3.0:
build { ...... applicationVariants.all { variant -> variant.outputs.all { outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk" } } }
If the following error occurs after gradle
You need to configure the dimension of flavor dimension to be the version number, so that all dimensions are unified
build { ...... defaultConfig { ...... flavorDimensions "versionCode" } }
Step 4: compile and package
Build - generate signed bundle or APK - select release or debug
3.2 Android studio comes with
Android studio multi-modal packaging is the same as Youmeng packaging, but the name in the label < meta data > can be defined by itself and is not limited to "umeng_channel"
<Meta-data android:name="UMENG_CHANNEL" //可以随意定义 android:value="${channel}" />
3.3 meituan walle
Step 1: configure the root build.gradle
buildscript { dependencies { classpath 'com.mcxiaoke.packer-ng:plugin:2.0.1' } }
Step 2: configure app build.gradle
apply plugin: 'packer' dependencies { ...... implementation 'com.mcxiaoke.packer-ng:helper:2.0.1' }
Step 3: plug in configuration
build { ...... packer { archiveNameFormat = '${buildType}-v${versionName}-${channel}' // 定义输出APK名称 archiveOutput = new File(project.rootProject.buildDir,"apks") // 设置APK输出目录 channelFile = new File(project.rootDir,"channel.txt") // 添加渠道配置文件 } }
Step 4: create a new channel configuration file channel.txt
Create a new channel.txt file in the project root directory, as shown in the figure
The content of the document is the channel name. It is required that there must be one channel per line
Step 5: compile and package
Use the terminal command:
gradlew clean apkRelease
For reference: [official document of meituan multi-channel packaging]( https://github.com/mcxiaoke/packer-ng-plugin )
4. Obtain channel information
1. How to obtain Youmeng and Android studio
By reading the < meta data > tag in the androidmanifest
private String getChannel() { try { PackageManager pm = getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(),PackageManager.GET_Meta_DATA); String channel = appInfo.MetaData.getString(key); // key为<Meta-data>标签中的name if (!TextUtils.isEmpty(channel)) { return channel; } } catch (Exception e) { e.printStackTrace(); } return null; }
2. Acquisition method of meituan walle
Meituan integrated self-contained acquisition method
private String getChannel() { try { PackageManager pm = getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(),PackageManager.GET_Meta_DATA); String channel = appInfo.MetaData.getString(key); // key为<Meta-data>标签中的name if (!TextUtils.isEmpty(channel)) { return channel; } } catch (Exception e) { e.printStackTrace(); } return null; }
summary
The above is a detailed explanation of several steps of Android multi-channel packaging introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!