android - 如何避免多模块android应用程序中的重复依赖

标签 android android-gradle-plugin android-module

我的 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/

相关文章:

android - java.lang.RuntimeException 主题.Sherlock

android - 如何在android中将模块依赖转换为aar依赖?

android - 如何将存储在字节数组中的图像加载到 WebView?

android - 如何在 Android 上的 C++ 构造函数中失败

android - 找不到com.appdynamics:appdynamics-gradle-plugin:2.0

java - 动画菜单实现

java - Espresso 测试中的 NoClassDefFoundError

android - 手机和平板电脑模块与 Android 库

android - 在 app 模块中使用 buildSrc 模块版本

android - 在横向模式下调用 setRequestedOrientation() 时创建了两次 Activity