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

V I*_*S H 7 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'
}
Run Code Online (Sandbox Code Playgroud)

功能模块依赖关系

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'
}
Run Code Online (Sandbox Code Playgroud)

在核心模块中使用api,并在功能模块中使用 api project(path:':coreModule',configuration: 'default')

Man*_*ath 7

如果你正在写

implementation 'com.xxx.android.x:1.2342'
Run Code Online (Sandbox Code Playgroud)

这意味着当前模块需要此依赖项才能工作。您需要在两个模块中显式声明它

核心之一

另一个模块中的一个

api您可以使用other not来删除这些冗余的重复依赖项, implementation 因为api用于与其他模块共享一个模块中的依赖项。因此,将重复的依赖项替换impementation为。api

例如: 库使用androidx.core:core-ktx:1.0.2 然后在库内build.gradle添加

api 'androidx.core:core-ktx:1.0.2'
Run Code Online (Sandbox Code Playgroud)

在这里您不需要在应用程序模块中再次定义它。