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

V I J E S H
  • 7,464
  • 10
  • 29
  • 37

2 Answers2

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.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
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.

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58