Multi-channel configuration (two methods)

1, can be written in the main module (APP) build.gradle

Android {compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig {applicationId "com.test.moduledemo" MinSdkVersion 21 targetSdkVersion 29 versionCode 1 versionName "1.0"} flavorDimensions "versionCode" productFlavors { Xiaomi {applicationId = "com.test.xiaomi" // Different channel configuration different parameters buildConfigField "int", "TEST_VALUE", "1" buildConfigField "String", "TEST_NAME", ""xiaomi""} huawei{applicationId = "com.test.huawei" // buildConfigField "int", "TEST_VALUE", "2" buildConfigField "String", "TEST_NAME", "huawei""} productFlavors. All {// Set the CHANNEL number (xiaomi, huawei) flavor - > flavor. ManifestPlaceholders. Put (" CHANNEL ", {variants -> def name = ((item.name!) {variants -> = "app") ? project.name : rootProject.name.replace(" ", "")) + "_" + variant.flavorName + "_" + variant.buildType.name + "_" + variant.versionName + "_" + new Date (). The format (' yyyyMMddhhmm) + "apk" / / relative path app/build/outputs/apk/huawei/release/def path = ".. /.. /.. /.. /.. Outputs. Each {output -> def outputFile = output.outputFile if (outputFile! = null && outputfile.name.endswith ('.apk')) {// Specify a path to output.outputFileName = new File(path, Name)}} // You can do something else after packaging, you can copy to the specified directory, Varie.rootdir. DoLast {File out = new File(" ${project.rootdir}/apk ") varie.outputs. Ant. Move file: file.outputFile, todir: ant. Move file: file.outputFile, todir: ant. "${project.rootdir}/apk"}}}/ / Multi-channel signature configuration signingConfigs {test {storeFile file(".. /test.keystore") storePassword 'test' keyAlias 'test' keyPassword 'test' v1SigningEnabled true v2SigningEnabled true } xiaomi { storeFile file(".. /xiaomi.keystore") storePassword 'xiaomi' keyAlias 'xiaomi' keyPassword 'xiaomi' v1SigningEnabled true v2SigningEnabled true } huawei { storeFile file(".. /huawei.keystore") storePassword 'huawei' keyAlias 'huawei' keyPassword 'huawei' v1SigningEnabled true v2SigningEnabled True}} buildTypes {debug {// debug this setting doesn't work, maybe it's a compiler problem? // productFlavors.xiaomi.signingConfig signingConfigs.test // productFlavors.huawei.signingConfig signingConfigs.test } release { productFlavors.xiaomi.signingConfig signingConfigs.xiaomi productFlavors.huawei.signingConfig SourceSets {xiaomi.res.srcDirs' SRC /main/res-xiaomi' huawei.res.srcDirs 'SRC /main/res-huawei' dependencies {xiaomiApi(' XXXXXXX ') huaweiImplementation(' XXXXXXXX ')}Copy the code

Create an flavors. Gradle file in the root directory of the project (same directory as settings.gradle)

Android {flavorDimensions "versionCode" productFlavors {xiaomi{applicationId = "com.test.xiaomi" // Different configurations of different parameters buildConfigField "int", "TEST_VALUE", "1" buildConfigField "String", "TEST_NAME", ""xiaomi""} huawei{applicationId = "com.test.huawei" // buildConfigField "int", "TEST_VALUE", "2" buildConfigField "String", "TEST_NAME", "huawei""} productFlavors. All {// Set the CHANNEL number (xiaomi, huawei) flavor - > flavor. ManifestPlaceholders. Put (" CHANNEL ", name)}} / /... More configuration}Copy the code

Gradle file(‘flavors. Gradle ‘) on the build.gradle file of the main module

Pay attention to

If the project is more complex, it may be possible to set different channel packages with buildConfigField, and different information fields may fail, Put (“TEST_VALUE”, 1) and add it to androidManifest.xml

<application>
    <meta-data  
      android:name="TEST_VALUE"  
      android:value="${TEST_VALUE}" />
</application> 
Copy the code

Get its value in code by doing the following:

ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(),  
  PackageManager.GET_META_DATA);
  int testValue = applicationInfo.metaData.getInt("TEST_VALUE");
Copy the code

packaging

Command line packaging:

Gradlew assembleRelease for Windows . / gradlew assembleRelease assembleRelease is playing all channel Release package assembleDebug is playing the Debug packages of all channels You can specify the channel package: Gradlew assembleXiaoMiRelease assembleHuaWeiRelease

Multi-channel packaging

Compiler packaging