我的 android 应用程序中有 2 个模块。核心模块和一个功能模块。目前我有这 2 个模块共有的 3 个依赖项,我将它添加到两个模块的 gradle 文件中(这会导致更多的应用程序大小和冗余)。如何在 android 中与多个模块共享这些依赖项。
核心模块依赖
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api 'com.github.bumptech.glide:glide:4.9.0'
}
功能模块依赖
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//implementation project(':testmodule')
api project(path:':coreModule',configuration: 'default')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
在核心模块中使用api
,在功能模块中使用api project(path:':coreModule',configuration: 'default')
最佳答案
如果你正在写
implementation 'com.xxx.android.x:1.2342'
这意味着当前模块需要这个依赖才能工作。你需要在两个模块中显式声明它
核心之一
其他模块中的一个
您可以使用以下方法删除这些冗余的重复依赖项
api
除了实现
因为 api
用于与其他模块共享一个模块中的依赖项。
因此,将 impementation
替换为 api
以用于重复的依赖项。
例如:库使用 androidx.core:core-ktx:1.0.2
然后在库 build.gradle
添加
api 'androidx.core:core-ktx:1.0.2'
这里你不需要在应用模块中再次定义它。
关于android - 如何避免多模块android应用程序中的重复依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57585481/