How to avoid duplicate dependencies in multiple module android application
Asked Answered
D

3

7

I have 2 modules in my android app. Core module and one feature module. Currently I have 3 dependencies common for these 2 modules and I am adding it in both modules gradle file (Which leads to more app size and redundancy). How to share these dependencies with multiple modules in android.

core module dependencies

   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'
}

feature module dependencies

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'
}

Used api in core module and used it in feature module using api project(path:':coreModule',configuration: 'default')

Dopester answered 21/8, 2019 at 6:16 Comment(0)
E
7

If you are writing

implementation 'com.xxx.android.x:1.2342'

which means that the current module requires this dependency to work. There by you need to declare it explicitly in both modules

One in core

One in other module

You can remove these kind of reduntant duplicate dependencies by using api other than implementation because api is used for sharing a dependency in one module with the others. So replace impementation with api for the duplicated dependencies.

For Example: Library uses androidx.core:core-ktx:1.0.2 Then inside library build.gradle add

api 'androidx.core:core-ktx:1.0.2'

here by you don't need to define it again in app module.

Encephalomyelitis answered 21/8, 2019 at 6:22 Comment(5)
Yes i did it. added all dependencies in a shared module using 'api' and used it in core module using "api project(path:':shared',configuration: 'default')" but couldn't resolve dependency in coreDopester
@VIJESH please post your gradleEncephalomyelitis
exclude module: 'modulename' is for excluding particular lib in a module. Issue is I am not able to access this library (added using api) in other modulesDopester
@VIJESH check the updated answer. I got your query now.Encephalomyelitis
Thanks, I was exactly looking at it.Kt
M
2

Android studio always warns about the duplicate dependencies and those duplicate dependencies create a time-consuming over head while building the release-apk file.

You can first identify the duplicate dependencies using the following command:

gradle -q dependencies yourProject:dependencies --configuration compile

The command result will show you human-readable tree hierarchy of all dependencies as mentioned below.

compile - Classpath for compiling the main sources.
...
+--- project :yourProject
|    +--- com.loopj.android:android-async-http:1.4.6
|    +--- org.apache.httpcomponents:httpmime:4.2.5
|    |    \--- org.apache.httpcomponents:httpcore:4.2.4
|    \--- com.google.code.gson:gson:2.3.1
...

Once you have identified the duplicate dependencies, you can exclude them from the specified library using the following syntax in your build.gradle file.

//OneSignal
api ('com.onesignal:OneSignal:[3.6.2, 3.99.99]') {
    exclude group: 'android.arch.lifecycle', module: 'extensions'
    exclude group: 'com.android.support', module: 'design'
}

I hope this helps.

Murillo answered 21/8, 2019 at 6:35 Comment(0)
S
0

It's a weird error I lost a lot of time trying to fix it, at the end I only had to re-clone the project and download the latest version of Android Studio(Koala)

Sedulous answered 15/8 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.