Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dependency from library module in app module

Maybe it's a silly question, but want to collect an app's dependencies to library.(so I can easily manage all dependency's version)

app/build.gradle

api project(path: ':library')

library/build.gradle

dependencies {
    implementation 'com.google.code.gson:gson:2.8.2' <- gson is just example, it can be anything.
}



In the above situation Can I use Gson from MainActivity.class? It seems to not be working..

If not, is there a way to achieve this?

I don't want to add the same dependency to both the app and library. If Gson is updated, I have to modify two positions, I hate it! :(

like image 696
Soo Chun Jung Avatar asked Jun 19 '18 05:06

Soo Chun Jung


People also ask

How do I add library dependencies?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

Is dependency same as library?

Dependency is a much more broader term than plain libraries. It can mean data, software installed, whatever. Maybe they meant to say “may depend on libraries and other dependencies”. A library is not the only thing software can depend on: configuration files, device drivers, databases, etc.

How do I import a library module?

Click on “Import Existing Project“. Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files.

CAN modules have dependencies?

Module dependencies are classes, archives, libraries and resources that your module files references. While a library is a set of class files stored in an archive or directory. Export check means if checked then this library will be implicitly added to the other module that references this one.


1 Answers

Just do the reverse.

In your case, use implementation in app and api in library modules.

implementation uses the dependency only for current module and api shares the dependency with other modules which use it.

like image 194
Nabin Bhandari Avatar answered Oct 28 '22 01:10

Nabin Bhandari