多个Module时dependencies管理,gradle管理

我们先在项目下自定义一个文件取名dependencies.gradle,在里面定义公共依赖方法

static def setAllDependencies(DependencyHandler dependencyHandler) {
    dependencyHandler.implementation 'androidx.core:core-ktx:1.7.0'
    dependencyHandler.implementation 'androidx.appcompat:appcompat:1.3.0'
    dependencyHandler.implementation 'com.google.android.material:material:1.4.0'
    dependencyHandler. implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    dependencyHandler. testImplementation 'junit:junit:4.13.2'
    dependencyHandler.androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    dependencyHandler.androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    dependencyHandler. implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.2.0")
    dependencyHandler. implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0")
}
ext.allModuleDependencies = this.&allModuleDependencies

2.然后在project的build.gradle中添加 apply from: "dependencies.gradle"

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "dependencies.gradle"
buildscript {

    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
}

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

3.在module或者App下gradle文件中添加

dependencies {
    rootProject.setAllDependencies(dependencies)
    //implementation project(path: ':bgfbase')

}

可以i添加公共依赖的代码放到一个公共方法里,让每个module的build.gradle中的dependencies {}中直接调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值