Gradle could try productFlavors, which is an option that can be used to create customized versions of APK.

android {
    ...
    defaultConfig {
        minSdkVersion 8
        versionCode 10
    }
    productFlavors {
        flavor1 {
            packageName "com.example.flavor1"
            versionCode 20
        }
        flavor2 {
            packageName "com.example.flavor2"
            minSdkVersion 14
        }
    }
}
Copy the code

This allows you to output two custom APKs with different package names and version numbers. However, it would be very inefficient to use it for channel packages, because it would go through a compilation process every time. Imagine how slow it would be if you had to go through a compilation process for every channel package, 100 channel packages.

So if you’re comfortable with this inefficient channel pack, let’s go back to the focus of this article and ask: “If you’re patching for multiple channels, what should you do?”

You might say, patch for different channel packages. The Tinker-support plugin inserts a different TINKER_ID for each channel package, which uniquely identifies the current version of the channel package. We can see gradle’s configuration for multi-channel patches (see multi-channel hot updates for details) :

The above example only configures two channels, if you configure more than five, then you need one patch, and one patch is uploaded to the Bugly patch management background, and we only allow five versions of the patch to be delivered simultaneously. The productFlavors configuration changes the FLAVOR field of the buildConfig class, which results in a different dex for each channel package. Therefore, the patch can only be configured for each channel. This is very awkward, so what to do? Is there a version that fixes all channels with a single patch? The answer is: yes, but only if you make sure all channel package code is consistent.

2. Rapid multi-channel packaging through multi-channel packaging framework

It is recommended to usewalleTo play multi-channel package, a new generation of multi-channel packaging artifact.

Walle or similar packaging tools do not change the structure of dex, just modify the APK Signature Block to add custom channel information to generate channel packages.

Configuration example:

// Apply from: 'multiple-channel.gradle'Copy the code

Create multiple-channel.gradle with the following contents:

ApkOutputFolder = new File("${project.buildDir}/outputs/channels"); // Custom channel package APK file name apkFileNameFormat = '${appName}-${packageName}-${channel}-${buildType}-v${versionName}-${versionCode}-${buildTime}.apk'; ChannelFile = new File("${project.getprojectdir ()}/channel")}Copy the code

Create channel configuration:

Command line multichannel package:

./gradlew clean assembleReleaseChannels
Copy the code

The following output is displayed:

Ok, to this has been achieved fast multi – channel package.

How to obtain channel information? If you want to obtain channel information for some statistical analysis, you can follow the following methods (refer to Walle for details) :

Dependencies {the compile 'com. At meituan. Android. Walle: library: 1.1.3'}Copy the code

Get channel information in code:

String channel = WalleChannelReader.getChannel(this.getApplicationContext());
Copy the code

If you have integrated Bugly’s exception reporting, you can plug in channel information in the following ways:

String channel = WalleChannelReader.getChannel(getApplication()); Bugly.setAppChannel(getApplication(), channel); Init (getApplication(), "YOUR_APP_ID", true); // The SDK is initialized and the appId is replaced with your Bugly appId bugly.init (getApplication(), "YOUR_APP_ID", true);Copy the code

In this way, we can count Crash data of your app by channel dimension.

One patch fixes all channels

Always save the best for last. The hot update plugin will also generate the corresponding baseline version in bakApk, which is no different from normal packaging:

You only need to upload the patch package to the patch management background and deliver it.

The author randomly selected three channels to install on different devices, and patched them successfully:

Ok, basically our needs have been fulfilled, mom no longer need to worry about me working overtime to upload the patch pack.

To sum up, Bugly currently supports the hot update of channel pack in two ways:

Gradle is an excellent solution that can be packaged quickly and can be fixed with a single patch.