我们先在项目下自定义一个文件取名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 {}中直接调用