Getting started with Android modularization:)

Keywords: Android Gradle xml Java

Due to the development needs, there may be more and more projects and more repetitive modules in the future. We do not want to repeat development every time, so we have recently started the transformation of modular development.

Here's an overview of the process:

First, create the project in the original mode, which is nothing to say, and then create three new module s under the project.

As shown in the diagram:

Explain:

  1. app doesn't have to be introduced too much
    base-res and router (can be interpreted as common or merged into one module).base-res stores resource files such as jar packages, tool classes, and base classes that will be used throughout the project.
    Note: This module will always be a library, and the IDS in the R file are public static (ButterKnife cannot be referenced here because the R.id s used by the butter knife must be public static final)
  2. testone is a new module, referencing router, in which you can write a new business module.

2. Add isDebug=false to gradle.properties

Figure:

If you want to make a module an app at the time of development, the property Library is essential at the time of publishing.

3. Modify the file of testone module.

First, create two new folders under the main folder, debug and release, to store different AndroidManifest.xml configuration files.As shown in the diagram:

The difference between the two profiles is that the profile under the debug folder requires an entry to register the activity, including some permissions.
The release folder only requires users to register components that need to be registered.

Next, you need to modify the apply header in build.gradle under testone

if (isDebug.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

And introduced

sourceSets {
        main {
            if (isDebug.toBoolean()) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
                java {
                    exclude 'debug/**'
                }
            }
        }
    }

Note: If you paste in the code, it is possible that mainfest.srcFile will be capitalized by default. Note here

The final gradle s are as follows:

if (isDebug.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

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

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            if (isDebug.toBoolean()) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
                java {
                    exclude 'debug/**'
                }
            }
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile project(':router')
}

4. build.gradle modifications in app

The following code needs to be added where references are made

if (!isDebug.toBoolean()) {
        compile project(':testone')
    } else {
        compile project(':router')
    }

In this way, the framework for the most basic modular development is almost set up, and the rest is to fill in the base-res and the code in each module.

Be careful
1. Jumps can use methods inside ActivityRouter.
2. If is too many modules, android studio is too laggy, you can modify settings.gradle in the whole directory, commenting out the modules that are not used in it for the time being.(As long as the computer is not too good (:) I believe it will be used)

In the next section, if there are problems with the above, please point out - - progress together.Thank you

Posted by downfall on Mon, 03 Jun 2019 21:34:27 -0700