Flavor dimension

FlavorDimensions are used to define flavorDimensions. The more dimensions, the richer the channel package can be produced.

Single flavor dimension
defaultConfig {
    flavorDimensions "default"
}


productFlavors {
    google {
     
    }

    huawei {
        applicationIdSuffix ".huawei"}}Copy the code

Single flavor dimension can only build the flavor dimension defined in productFlavors, not combination. In the figure, only Google and Huawei channel packages can be built

Multidimensional flavor dimension
defaultConfig {
    flavorDimensions "company"."channel"
}


productFlavors {
    companyA {
       dimension "company"
    }

    companyB {
        dimension "company"
    }
    
   channelA {
         dimension "channel"
    }

   channelB {
         dimension "channel"}}Copy the code

According to the combination of company and channel, the channel package of companyAchannelA, companyAchannelB, companyBchannelA and companyBchannelB can be constructed above

BuildConfig

Generate properties for BuildConfig

 buildConfigField "boolean"."LOG_DEBUG"."false"
 // Dynamically assign the URL and port
 buildConfigField('String'.'BASE_URL'.'"https://www.baidu.com/"')
 buildConfigField('int'.'URL_PORT'.'5672')

Copy the code

Use BuildConfig to determine the current flavor

Use Flavor to apply the plug-in

If similar requirements exist in a project, the Google channel uses the ‘com.google.gms. Google-services’ plug-in, and the Huawei channel uses the ‘com.huawei.agconnect’ plug-in to determine the corresponding Flavor channel plug-in. The code is as follows

def String getCurrentFlavor() { Gradle gradle = getGradle() String tskReqStr = gradle.getStartParameter().getTaskRequests().toString() Pattern pattern; if (tskReqStr.contains("assemble")) pattern = Pattern.compile("assemble(\w+)(Release|Debug)") else pattern = Pattern.compile("bundle(\w+)(Release|Debug)") Matcher matcher = pattern.matcher(tskReqStr) if (matcher.find()) { return matcher.group(1).toLowerCase() } else { return ""; } } gradle.projectsEvaluated { preBuild.dependsOn(applyPluginByFlavor) } // Then check on the parameter, which comes from the command line task applyPluginByFlavor { def flavorType = "" flavorType = getCurrentFlavor(); If (flavortype.equals (" Google ")) {println(" building Google package ") apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.firebase.crashlytics' apply plugin: 'com. Google. Firebase. Firebase - perf'} the if (flavorType. Equals (" huawei ")) {println (" is constructing huawei package ") apply the plugin: 'com.huawei.agconnect' apply plugin: 'com.huawei.agconnect.apms' } }Copy the code

Obtaining MetaData properties

How do you violate the manifestplaceholder = HW_BANNER_ID: <meta-data android:name="HW_BANNER_ID" android:value="${HW_BANNER_ID}" />Copy the code

Get HW_BANNER_ID configured in Gradle

public String getMetaData(String key) {
        try {
            PackageManager packageManager = getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(this
                    .getPackageName(), PackageManager.GET_META_DATA);
            return applicationInfo.metaData.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
Copy the code