Channel loading res, libraries and Class in Android studio gradle

Keywords: Android Attribute Java Gradle

Reprint: Channel loading res, libraries and Class in Android studio gradle

Some time ago, when faced with such a demand, the Map function was used in App, and Android built-in google Map Service was used in the overseas version. For the domestic version (as we all know), we used the Golden Map. When importing the Golden Map, we need to add the corresponding sdk and. so files, so that the size of the final package will increase by about 2M. But the overseas version does not need this part of resources, so it needs to load different jar s, res, and different classes according to different channels, which helps to keep the code clean and control the size of the package.

Say nothing more, go straight to the next step!

1) First of all, different product Flavors should be created to facilitate packaging and debugging in different channels.

productFlavors {
    // Define separate dev and prod product flavors.
    dev {
        minSdkVersion 21
       ...
    }
    Chinese_dev {
        minSdkVersion 21
       ...
    }

2) To obtain the current Flavor() for selecting the appropriate resources:

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String taskStr = gradle.getStartParameter().getTaskRequests().toString()
    println("taskStr:" + taskStr)
    println()
    Pattern pattern;
    if (taskStr.contains("assemble")) {
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    } else {
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    }
    Matcher matcher = pattern.matcher(taskStr)
    if (matcher.find()) {
        returnmatcher.group(1);
    }
    return ""
}

3) Determine whether it is Flavor you need and choose the path of LIBS (where you need to create LIBS packages with different names, such as putting Gaud's related jar s in the cn_libs directory):?

def buildLibs() {
    String currentFlavor = getCurrentFlavor()
    println("current Flavor:" + currentFlavor)
    booleanisChinese = "Chinese_dev".equalsIgnoreCase(currentFlavor))
    println("--- is Chinese ---" + isChinese)
    if (isChinese) {
        return'cn_libs'
    }
    return 'libs'
}

4) Call the buildLibs method when loading dependencies to extract the resource packages to be loaded:

dependencies {
    compile fileTree(dir: buildLibs(), include: ['*.jar']

5) In order to separate different resources completely, we need to separate AndroidManifest.xml and resource files. Two separate directories, such as cn and en, can be created under the same level directory of the main directory and put into the corresponding code and resources.
For Android Manifest. xml, key resources use tools:node= "merge":

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="merge"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" tools:node="merge"/>

    <application
        tools:node="merge">

        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="key"/>

        <service android:name="com.amap.api.location.APSService"/>

    </application>

</manifest>

2. For code class replacement, if you want to switch code according to flavor, you need different flavor java packages and create the same class name under them. If you can't have the same class name under main, you can only type the corresponding class file when packing, and discard the others. Such as:
| Flavor A
| ——-> com.fan.util
| ————> class A
| main
| ——>com.fan.util
No more class A
| Flavor B
| ——>com.fan.util
| ————->class A

The directory created will probably look like this:

3. Configure the location of java. srcDirs, Android Manifest. XML and resource in the gradle file:

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
        java.srcDirs = ['src/main/java']
        res.srcDirs = ['src/main/res']
    }
    dev {
        java.srcDirs = ['src/us/java/']
    }
    Chinese_dev {
        jniLibs.srcDirs = ['libs-zh']
        manifest.srcFile 'src/cn/AndroidManifest.xml'
        java.srcDirs = ['src/cn/java/']
        res.srcDirs = ['src/cn/res']
    }
}

Then you can play happily through Build Variants!!!

Posted by koen on Mon, 24 Dec 2018 22:42:06 -0800