This is the 7th day of my participation in Gwen Challenge

benefits

Componentization is to package and debug each relatively independent business module independently in the development process. The visible benefit is to shorten the compilation and test time. Of course, more importantly, the separation can effectively reduce or even eliminate the coupling of each business logic module and facilitate collaborative development. Each module can be multiplexed horizontally. So without further ado let’s get right into the code.

code

Antecedents feed

Gradle scripts are written in Groovy, which is also a JVM language. With Groovy’s ability to make code level changes in Gradle scripts, it’s not just about configuring dependencies.

New project

First we need to create a new component ComponentTest, and then create a new userinfo lib. We can look at the top line of the build.gradle file for each package. We can change library to Application and see that UserInfo also appears on the installable App menu.

// app
plugins {
    id 'com.android.application'
}
// userinfo
plugins {
    id 'com.android.library'
}
Copy the code

That’s the first step to componentization. Modify the configuration code above to run the component on the phone, but there are applicationId and AndroidManifest.xml to handle, so let’s do that.

Start operation

First we create a new config.gradle file in the project root directory. This creates a new class file that can be imported from other gradle files. An isModule variable is defined in the file, and we control whether or not we package modularized.

ext {

    isModule = false

}
Copy the code

Next we import the above configuration file in the build.gradle file in the root directory.

apply from: "config.gradle"
Copy the code

Now we can use the variable we defined in the lib build file.

// Use this variable to control whether to package as lib or app
if (isModule) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
// Set applicationId if packaged separately
if (isModule.toBoolean()) {
    applicationId "xyz.xyz0z0.userinfo"
}
//
sourceSets {
    main {
        // Use a different androidmanifest.xml file for independent debugging and integration debugging. This is independent debugging and requires application information
        if (isModule.toBoolean()) {
            manifest.srcFile 'src/main/moduleFile/AndroidManifest.xml'
        } else {
            // This is integrated debugging, all you need is activity information
            manifest.srcFile 'src/main/AndroidManifest.xml'}}}Copy the code

Here we are based on the initial analysis, according to the packaging mode we need to set up the package plug-in, applicationId, AndroidManifest file. Here we need to create a new moduleFile folder in the source directory and create an Androidmanifest.xml file in it, which is used for packaging separately, so we need to configure some additional information. The specific code can be found in the source project.

See the effect

Now that we change the isModule value, we can control how userInfo is packaged, and it works perfectly on the phone when packaged independently. So we’ve taken the first step toward componentization. We also need to configure component communication and other steps, please refer to the second share.

This article is a personal learning record, can not avoid mistakes or not enough in-depth understanding of technical points, but also hope for criticism and advice. The corresponding source address is github.com/xyz0z0/Comp… For details, see the corresponding COMMIT records