background
Multi-channel packaging, as the name suggests, is used to distinguish the same software and release versions on different channels.
This large app is almost a must-configure item on mobile phones, and there will be some subtle differences in the apk used by different models of mobile phones. For example, I use Kuan Community more. The Kuan Apk interface downloaded on the official website has a software management function at the top, and Kuan downloaded in the application stores of other mobile phone manufacturers does not have this function.
At present, we have a need to use multiple projects in a set of software code bases together, and package different channels of apks for different projects. Let’s learn to record them now.
Gradle script and Manifest file configuration
Since the apks from different channels need to make subtle functional distinctions, they need to get a flag bit similar to a key-value pair in the code. And can be called globally. First, we need to configure different channel names in the app-level gradle file.
The dimension names in flavorDimensions and productFlavors need to correspond one by one. Just write a "default".
defaultConfig { xxxx = xxxx ("default") }
The syntax is slightly different from Groovy. To add your own configuration, you usually use keywords such as create and register. Those who are not familiar with the configuration can try first.
In the domain of different channel packages, we can distinguish many parameter values. For example, I configured different channels to use different platforms to sign.
productFlavors { create("cheetah") { dimension = "default" signingConfig = ("cheetah") manifestPlaceholders["CHANNEL_VALUE"] = "cheetah_channel" } create("redfin") { dimension = "default" signingConfig = ("aaos") manifestPlaceholders["CHANNEL_VALUE"] = "redfin_channel" } }
In order to set it to the key-value pair you want, it is recommended to set a set of metadata in the Manifest manifest file and let Gradle assign it when packaging. Combining the two pieces of code above and below, we have a set of metadata in Manifest, the key is set to CHANNEL, and the value is the reference variable CHANNEL_VALUE. In the Gradle channel configuration, we use:
manifestPlaceholders["CHANNEL_VALUE"] = "cheetah_channel"
to assign a value to it.
<meta-data android:name="CHANNEL" android:value="${CHANNEL_VALUE}" />
When packaging, you can use different apk names to determine the name of the flavor.
{ { if (this is ) { if (flavorName == "redfin") { = "RedfinChannel_V${versionName}.apk" } else if (flavorName == "cheetah") { = "CheetahChannel_V${versionName}.apk" } } } }
Code call
After the global packaging configuration is completed, how can we use different channels in the code to distinguish between different channels?
There are two ways to obtain it here. One is to directly take the channel name when created in the Gradle script and directly get it through the FLAVOR field of the BuildConfig class; the other is to get the value of the metadata we set in Manifest, which is a little more complicated. The specific code is as follows:
object FlavorConfig { const val REDFIN = "redfin" const val CHEETAH = "cheetah" private var manifestMetaData = "" private var gradleData = "" init { val appInfo = ( , PackageManager.GET_META_DATA ) manifestMetaData = ("CHANNEL") as String gradleData = } fun getMetaDataOne() = { infoLog("metaData value: $this") } fun getMeatDataTwo() = { infoLog("gradle metaData value: $this") } }
This is the end of this article about Android multi-channel packaging configuration solution. For more related Android packaging configuration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!