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')
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值