Android componentization topic - componentization configuration

Android componentization topic, which explains in detail the use and configuration of componentization and the principle of implementation.

This article explains the origin and configuration of componentization. The next issue will explain page routing jump, routing principle and apt

1. Origin of componentization

The relationship between modularity, componentization and plug-in?

(excerpted from Baidu Encyclopedia) modularization refers to the process of dividing the system into several modules layer by layer from top to bottom when solving a complex problem, and each module can work independently.

In the field of technical development, modularization refers to splitting code. When the code is particularly bloated, modularization is used to divide and rule the code, decouple and layer it.

In the field of Android, the specific implementation methods of modularization are: component and plug-in.

The difference between componentization and plug-in

A complete set of plug-in or component must be able to realize the six functions of separate debugging, integrated compilation, data transmission, UI jump, life cycle and code boundary. The most important and unique difference between plug-in and component-based is that plug-in can dynamically add and modify online modules. The dynamic capability of component-based is relatively weak. It can only dynamically load and unload existing online modules, and cannot add or modify.

2. How to realize componentization

The problems to be considered to realize componentization mainly include the following:

  • Code decoupling. Splitting and decoupling a huge project is very time-consuming and labor-consuming, but it is also the most basic and important step
  • Data transfer. Each component may be provided to other components for use, including data transfer between the main project and components, and between components
  • UI jump.
  • The lifecycle of the component. Life cycle of component loading, unloading and dimensionality reduction
  • Integration debugging. How to compile components on demand in the development phase? There may be only one or two components involved in integration in a debugging, so the compilation time will be greatly reduced and the development efficiency will be improved.
  • Code isolation. How to eliminate coupling.

The first step to realize componentization is to sort out the code splitting structure

The first step to realize componentization is to sort out the project engineering structure and clarify which functions can be used as components.

It is suggested to draw and sort out the project structure, as shown in the following figure:

The second step to realize componentization is to perform basic configuration before splitting the code

Unify the configuration of build and the switching of component / integration mode to realize the separate debugging of components

  1. Create a new config.build at the root of the project
ext {
    // false integration mode

    // true component mode

    isComponent = false

    androidConfig = [
            compileSdkVersion: 27,
            minSdkVersion    : 19,
            targetSdkVersion : 27,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    appIdConfig = [
            app   : "com.prim.component.demo",
            moudle1: "demo.prim.com.moudle1"
    ]

    supportLibrary = "27.1.1"

    dependencies = [
            appcompatv7: "com.android.support:appcompat-v7:${supportLibrary}"
    ]
}
  1. Add configuration to the main build
apply from: "config.gradle"
  1. moudle configuration, call the configuration in config.gradle
//Configure apply
if (isComponent) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

//Gets the main object in the config file that configures the rootProject project
def config = rootProject.ext.androidConfig

def appIdConfig = rootProject.ext.appIdConfig

def dependenciesConfig = rootProject.ext.dependencies

android {
    compileSdkVersion config.compileSdkVersion

    defaultConfig {
        minSdkVersion config.minSdkVersion
        targetSdkVersion config.targetSdkVersion
        versionCode config.versionCode
        versionName config.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //If moudle configures the app ID of the component for the component
        if (isComponent) {
            applicationId appIdConfig.app
        }

        //In the configuration BuildConfig code, you can call to determine whether the moudle is a component
        buildConfigField("boolean","isComponent",String.valueOf(isComponent))

        //Configuration resource file
        sourceSets {
            main {
                if (isComponent) {//If the moudle is a component, configure the AndroidManifest 
                    manifest.srcFile 'src/main/moudle/AndroidManifest.xml'
                    // Configuring the java code master file in component mode
                    java.srcDirs 'src/main/java','src/main/moudle/java'
                } else {
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation dependenciesConfig.appcompatv7
    implementation 'com.android.support.constraint:constraint-layout:+'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:+'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
  1. app configuration, calling components
apply plugin: 'com.android.application'

def config = rootProject.ext.androidConfig

def appIdConfig = rootProject.ext.appIdConfig

def dependenciesConfig = rootProject.ext.dependencies

android {
    compileSdkVersion config.compileSdkVersion
    defaultConfig {
        applicationId appIdConfig.app
        minSdkVersion config.minSdkVersion
        targetSdkVersion config.targetSdkVersion
        versionCode config.versionCode
        versionName config.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
         //In the configuration BuildConfig code, you can call to determine whether the moudle is a component
        buildConfigField("boolean","isComponent",String.valueOf(isComponent))
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation dependenciesConfig.appcompatv7
    implementation 'com.android.support.constraint:constraint-layout:+'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    if (!isComponent){//Import is allowed only when moudle1 is not a component
        implementation project(':moudle1')
    }
}

This article is transferred from https://juejin.cn/post/6844903743918440462 , in case of infringement, please contact to delete.

Added by Arenium on Fri, 12 Nov 2021 11:52:54 +0200