The demo address

Android componentization topic, explain in detail the use and configuration of componentization, as well as the principle of implementation.

This article explains the origin and configuration of componentization, the next section will explain page route jump and routing principle and APT

1. Origin of componentization

What is the relationship between modularity, componentization, and pluginization?

Modularity refers to the process of solving a complex problem by dividing the system from top to bottom into several modules, each of which can work independently.

In technology development, modularity is about splitting up code, dividing and governing code, decoupling and layering it when it’s particularly bloated.

In the field of Android, the specific implementation method of modularization is: componentization and plug-in.

In more detail

The difference between componentization and plug-in

A complete set of plug-ins or componentization must be able to implement individual debugging, integrated compilation, data transfer, UI jump, lifecycle, and code boundaries. The most important and only difference between pluginization and componentization is that pluginization can dynamically add and modify online modules, while componentization has relatively weak dynamic capability. It can only dynamically load and unload existing online modules, and cannot add or modify them.

2. How to achieve componentization

To realize componentization, the following issues need to be considered:

  • Code decoupling. Decoupling a large project is a time-consuming and labor-intensive task, but it is the most fundamental and important step
  • Data transfer. Each component has the potential to be used by other components, and data is passed between the main project and components, and between components
  • The UI jump.
  • The lifecycle of the component. Lifecycle of component loading, unloading, and dimension reduction
  • Integration debugging. How do you build components on demand during development? Only one or two components may be integrated at a time, which can greatly reduce compilation time and improve development efficiency.
  • Code isolation. How to prevent coupling.

The first step to componentization is to clean up the code split structure

The first step to componentization is to sort out the engineering structure of the project and identify what functions are available as components.

It is suggested to draw and sort out the project structure, as shown below:

The second step in implementing componentization is to do the basic configuration before splitting the code

Unify the builde configuration and switch of component/integration mode, realize the independent debugging of components

  1. Create a new config.build at the root of the project
ext {
    // falseIntegration pattern //trueComponent 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}"]}Copy the code
  1. Add configuration to the main build
apply from: "config.gradle"

Copy the code
  1. Moudle configuration, call config.gradle configuration
/ / configure the applyif (isComponent) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'} / / get the config file configuration rootProject project the main object of 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.versionNametestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"// If moudle configures the component's APP ID for the componentif(isComponent) {applicationId appidconfig.app} // the BuildConfig code can call to determine whether moudle isComponent buildConfigField("boolean"."isComponent", string.valueof (isComponent)) // Configure the resource filesourceSets {
            main {
                if(isComponent) {// Configure AndroidManifest Manifest. SrcFile if moudle is a component'src/main/moudle/AndroidManifest.xml'// Configure the Java code master file java.srcdirs in component mode'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'
}

Copy the code
  1. App configuration, calling component
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"// The BuildConfig code can be called to determine whether moudle is 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 the implementation Project only if moudle1 is not a component (':moudle1')}}Copy the code

Next, we will explain the page route jump and the principles of routing and APT.

Android componentization topic: Componentization Configuration

APT of actual combat

Principles of Routing Framework

Service communication between modules