Hand-in-hand instructions on building component architecture for Android projects

Keywords: Android Java xml Gradle

Article directory

1. overview

Project address: ComponentizationDemo
Componentization: app is divided into modules according to business, function, etc. Then it can be implemented in specific modules with MVC, MVP, MVVM and other architectures.
Features: These modules are integrated into apps when making formal apps, and they can be debugged one by one as each app Module is debugged.

So how to switch library Module and app? By setting the AS module property:

  • application: An Android program that runs independently, our APP
    apply plugin: 'com.android.application'
  • library: Can not run independently, generally Android program-dependent Libraries
    apply plugin: 'com.android.library'

2. Basic configuration

Unified Management build Configuration and Switching of Component/Integration Mode

  1. Add the config.gradle file under the root directory as follows:
ext {
    // Component Switch: false Integration of true Components
    isComponent = false
    android = [
            compileSdkVersion: 28,
            defaultConfig    : [
                    buildToolsVersion        : 28,
                    applicationId            : 'com.wzc.componentizationdemo',
                    minSdkVersion            : 15,
                    targetSdkVersion         : 28,
                    versionCode              : 1,
                    versionName              : "1.0",
                    testInstrumentationRunner: 'android.support.test.runner.AndroidJUnitRunner'
            ]

    ]
    //Three party Library
    dependencies = [
            appcompatV7     : 'com.android.support:appcompat-v7:28.0.0',
            constraintLayout: 'com.android.support.constraint:constraint-layout:1.1.3',
            junit           : 'junit:junit:4.12',
            runner          : 'com.android.support.test:runner:1.0.2',
            espressoCore    : 'com.android.support.test.espresso:espresso-core:3.0.2'
    ]
}
  1. Add apply from: "config.gradle" to build.gradle in the root directory, as follows:

3. Setting Module

  1. New library Module
    Add:
	if (rootProject.ext.isComponent) {
  		apply plugin: 'com.android.application'
	} else {
 		apply plugin: 'com.android.library'
	}
So we can set the module property in config.gradle through isComponent
  1. Android Manifest File Merge Problem
    Since it's an app, it must have Application, Main Activity, and package name.
    2.1 Method 1:
    New src/main/debug/Android Manifest.xml and src/main/debug/java folders in module

    Add:
android {
	...
    sourceSets {
        main {
            if (rootProject.ext.isComponent) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
                // The main file of java code in configuration component mode (if ClassNotFoundException clean module appears), which is handled differently from Modele_C, so exclude debug folder is not needed in else.
                java.srcDirs 'src/main/java','src/main/debug/java'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
            }
        }
    }
        defaultConfig {
        if (rootProject.ext.isComponent) {
            applicationId 'com.wzc.module_a'
        }
        minSdkVersion rootProject.ext.android.defaultConfig.minSdkVersion
        targetSdkVersion rootProject.ext.android.defaultConfig.targetSdkVersion
        versionCode rootProject.ext.android.defaultConfig.versionCode
        versionName rootProject.ext.android.defaultConfig.versionName
        testInstrumentationRunner rootProject.ext.android.defaultConfig.testInstrumentationRunner
    }
	...
}

2.2 Method 2:
New src/main/debug/Android Manifest.xml and src/main/java/debug folders in module
Add:

android {
    ...
    sourceSets {
        main {
            if (rootProject.ext.isComponent) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    //Exclude all files under the java/debug folder
                    exclude '*debug'
                }
            }
        }
    }
    defaultConfig {
        if (rootProject.ext.isComponent) {
            applicationId 'com.wzc.module_c'
        }
        minSdkVersion rootProject.ext.android.defaultConfig.minSdkVersion
        targetSdkVersion rootProject.ext.android.defaultConfig.targetSdkVersion
        versionCode rootProject.ext.android.defaultConfig.versionCode
        versionName rootProject.ext.android.defaultConfig.versionName
        testInstrumentationRunner rootProject.ext.android.defaultConfig.testInstrumentationRunner
    }
	...
}
  1. Reference to module in main app
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation rootProject.ext.dependencies.appcompatV7
    implementation rootProject.ext.dependencies.constraintLayout
    testImplementation rootProject.ext.dependencies.junit
    androidTestImplementation rootProject.ext.dependencies.runner
    androidTestImplementation rootProject.ext.dependencies.espressoCore
    // isComponent = false represents integration
    if (!rootProject.ext.isComponent) {
        //main module
        implementation project(':module_a')
        implementation project(':module_b')
        implementation project(':module_c')
    }
}

Posted by hypuk on Thu, 10 Oct 2019 13:34:10 -0700