Android several steps of multi-channel packaging

Time:2020-10-6

1. What is multi channel packaging

In different application markets, there may be different statistical requirements, and it is necessary to publish an installation package for each application market. Here we introduce the multi-channel packaging of Android. Add different marks in the installation package to distinguish various channels and facilitate the statistics of APP in the market.

2. Several packing methods

  • Umeng
  • Android studio comes with
  • Walle

3. Getting started

3.1 Umeng

Step 1: add in Android manifest


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

Step 2: in build.gradle Baidu {} is 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 2.3 version:


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 3.0 version:


build {
 ......
 applicationVariants.all { variant ->
 variant.outputs.all {
 outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk"
 }
 }
}

If the following error appears after gradle

You need to configure the dimension of flavor dimension to be the version number, so that the 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

Android studio multimodal packaging is the same as that of Youmeng, but the name in the tag < meta data > can be defined by yourself, and is not limited to “Umeng”_ CHANNEL”

<meta-data 
 android:name= "UMENG_ Channel "// can be defined at will
 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}' // defines the output APK name
 archiveOutput = new File( project.rootProject.buildDir , "apks") // set the output directory of apk
 channelFile = new File( project.rootDir " channel.txt ") // add channel profile
 }
}

Step 4: create a new channel profile channel.txt

Create in the project root directory channel.txt File, as shown in the figure

The content of the document is the name of the channel. It is required that there must be one channel for each line

Step 5: compile and package

Use the terminal command:

gradlew clean apkRelease

Reference: [official document of meituan multi channel packaging]( https://github.com/mcxiaoke/packer-ng-plugin )

4. Access to channel information

1. Access to Youmeng and Android studio

By reading the < meta data > tag in Android manifest

private String getChannel() {
 try { 
   PackageManager pm = getPackageManager(); 
   ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); 
   String channel =  appInfo.metaData.getString (key); // key is the name in the < meta data > tag 
   if (!TextUtils.isEmpty(channel)) {  
   return channel;
   }
  } catch (Exception e) { 
   e.printStackTrace();
  }
  return null;
}

2. How to get walle

The method of acquiring self brought by meituan

private String getChannel() {
 try { 
   PackageManager pm = getPackageManager(); 
   ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); 
   String channel =  appInfo.metaData.getString (key); // key is the name in the < meta data > tag 
   if (!TextUtils.isEmpty(channel)) {  
   return channel;
   }
  } catch (Exception e) { 
   e.printStackTrace();
  }
  return null;
}

summary

The above is the detailed explanation of several Android multi-channel packaging steps introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and the editor will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]