build.gradle依赖配置

1、在project中添加一个自定义gralde文件,如config.gradle,统一管理app或其他module的build.gralde中要用到的配置

ext {// extend
    // false:组件模式
    // true:集成模式
    isModule = true
    android = [
            compileSdkVersion: 30,
            minSdkVersion    : 21,
            targetSdkVersion : 30,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    appId = [
            "app"    : "com.example.component",
            "module1": "com.example.module1",
            "module2": "com.example.module2"
    ]

}

2、在project的build.gradle中引用config.gradle,即添加语句apply from: “config.gradle”

// Top-level build file where you can add configuration options common to all sub-projects/modules.

apply from: "config.gradle"

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

3、在app或其他module的build.gradle中使用config.gralde已定义好的配置,如下所示

plugins {
    id 'com.android.application'
}

// 赋值与引用
def cfg = rootProject.ext.android
def appId = rootProject.ext.appId

android {
    compileSdk cfg.compileSdkVersion

    defaultConfig {
        applicationId appId["app"]
        minSdk cfg.minSdkVersion
        targetSdk cfg.targetSdkVersion
        versionCode cfg.versionCode
        versionName cfg.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        /**
         * javaCompileOptions -> 配置Java编译的选项
         * 通过在build.gradle编译配置(javaCompileOptions)中配置注解处理配置
         * (annotationProcessorOptions),这样在注解处理器中加上@SupporttedOptions({...})就能
         * 拿到改配置,上面Demo中配置的值是project.getName(),拿到的就是module的名字。
         */
        javaCompileOptions {
            annotationProcessorOptions {
                // 把模块名字保存到参数中(key -> values)
                arguments = [moduleName: project.getName()]
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    // compile被废弃
    // api:同compile
    // implementation: 不会进行传递依赖。即router-annotation模块只对 本模块(router-core) 开放,无法在 app 模块中使用
    api project(':router_annotation')

    annotationProcessor project(':router_compiler')
    implementation project(':base')

    if (isModule) {
        implementation project(':module2')
        implementation project(':module1')
    }
}

你可能感兴趣的:(架构)