In modular development, the following requirements should be met as far as possible [1] :

  • Adding a new module must be easy (it usually happens)
  • The maintenance module configuration must be simple

Therefore, it is necessary to simplify gradle configuration files. You can omit a lot of gradle code in modules by doing the following

Step 1: Create a unified configuration file

Create a unified configuration file config. Gradle (name can be customized) in the project root directory.

  • useextUnified management constants
  • usesubprojectsandhasPropertyConfigure all children in a unified mannerbuild.gradleIn theandroidField public content
/** * config. Gradle is used to configure constants and dependent libraries in your project."config.gradle"* Then use"rootProject.ext"App.com pileSdkVersion, daggerLibs.api, daggerlibs.values () */ ext { / / * * * * * * * * * * * * * * * * * * * * * * * * * the Module configuration management * * * * * * * * * * * * * * * * * * * * * * * * * * app = [compileSdkVersion: 28, applicationId:"com.***.mvp",
           minSdkVersion    : 15,
           targetSdkVersion : 28,
           versionCode      : 1,
           versionName      : "1.0",
           buildToolsVersion: '28.0.3',] / / * * * * * * * * * * * * * * * * * * * * * * * * * reference library version number management * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * reference library version number management * * * * * * * * * * * * * * * * * * * * * * * * * * dependVersion = [kotlin_version:'1.3.31',
                     dagger_version                   : '2.21',
                     dagger_android_version           : '2.x'. ] / / * * * * * * * * * * * * * * * * * * * * * * * * * reference library version number management * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * class library maven address * * * * * * * * * * * * * * * * * * * * * * * * * * kotlinLibs = [kotlin_stdlib_jdk8:"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$dependVersion.kotlin_version"]... // DAGGER dependency daggerLibs = [API:"com.google.dagger:dagger:$dependVersion.dagger_version",
                  compiler                : "com.google.dagger:dagger-compiler:$dependVersion.dagger_version",
                  dagger_android          : "com.google.dagger:dagger-android:$dependVersion.dagger_android_version",
                  dagger_android_support  : "com.google.dagger:dagger-android-support:$dependVersion.dagger_android_version",
                  dagger_android_processor: "com.google.dagger:dagger-android-processor:$dependVersion.dagger_android_version"] / / * * * * * * * * * * * * * * * * * * * * * * * * * class library maven address * * * * * * * * * * * * * * * * * * * * * * * * * *} / / in all child build. Gradle add subprojects android field (module) file {afterEvaluate {project ->//if (project.hasProperty('android')) {android {compileSdkVersion app.compileSdkVersion defaultConfig {minSdkVersion app.minsdkVersion targetSdkVersion apptargetSdkVersion versionCode app.versionCode versionName app.versionNametestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                }
                buildTypes {
                    release {
                        minifyEnabled false
                        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                    }
                }
                buildToolsVersion appbuildToolsVersion
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { javaCompileOptions {  annotationProcessorOptions { arguments = [AROUTER_MODULE_NAME: project.getName()] } } } } } } }Copy the code

Step 2: Project rootsbuild.gradle

Add to the build. Gradle file header at the project root

apply from: "config.gradle".Copy the code

Step 3: Childrenbuild.gradle

Build. gradle(build.gradle in module)

apply plugin: 'com.android.application'// If there is a difference from the unified configuration file, Android {defaultConfig {applicationId app. ApplicationId}} dependencies {implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation junitLibs.junit
    androidTestImplementation androidxLibs.runner
    androidTestImplementation androidxLibs.espresso_core
    implementation androidxLibs.constraintlayout
    implementation project(':baselibrary')
    annotationProcessor daggerLibs.compiler
    annotationProcessor arouterLibs.compiler
    annotationProcessor butterknifeLibs.compiler
}
Copy the code

reference

Jeroen Mols: Modularization – Lessons learned

Sam Edwards: Kotlin + buildSrc for Better Gradle Dependency Management

JessYanCoding: MVPArms


  1. Jeroen Mols: Modularization – Lessons learned ↩︎