aditya
aditya

Reputation: 29

Do I need to add same external library in all module if i want that external library in all modules in one project?

I want to develop a app that have multiple modules related to each other, and some modules require same library (like Picasso). I want that I define that library in one place and use it in multiple modules.

Upvotes: 2

Views: 806

Answers (2)

kiroglue
kiroglue

Reputation: 376

No, you don't have to define same dependency to all your modules.

Adding one dependency can be forwarded to parent modules transitively as long as base module depends to child module and dependency definition in build.gradle(child) is pointed out by api keyword. Here api and implementation keyword details.

Let me give one additional case to clarify transitive dependencies. If you define your dependency only child, it will work as we expect. But what if we define same library with different version numbers?

Assume you have default app(base module) and childModule with Constraint Layout dependency. For app:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(path: ':childModule')
...
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
...

}

For child:

dependencies {
...
api 'androidx.constraintlayout:constraintlayout:1.1.3'
..

}

If you get dependency tree by ./gradlew app:dependencies command, you will see version 1.1.1 converted to 1.1.3. Because gradle resolve version conflicts by selecting higher version numbers. In fact, we still see child dependency is forwarded to base.

--- androidx.constraintlayout:constraintlayout:1.1.1 -> 1.1.3 (*)

Note: Dependencies which are incoming with 3rd party libraries are transitive as your module dependencies too.

Upvotes: 0

Radesh
Radesh

Reputation: 13575

You must create BaseModule and in dependency add your library BUT

Instead of implementation use api

Now all project or module (like app) that contain BaseModule can use that library

For example

api 'com.squareup.picasso:picasso:2.71828'

Upvotes: 2

Related Questions