Mark Pazon
Mark Pazon

Reputation: 6205

Multiple modules using the same jar

I have a project with several modules. Some modules depends on the same jar. Right now each of the modules have their own "libs" folder containing 2 jars of the same library. Updating a jar file can be problematic since I will now have to change all the jars from all of the modules. Also, I encounter a compilation problem saying that there are duplicate classes. One workaround is to remove the jar files from all of the modules except for one. Is this the only way to do it or is there a better way?

Upvotes: 1

Views: 3634

Answers (3)

atla praveen
atla praveen

Reputation: 49

In the latest android versions, the compile project is deprecated in build.gradle. Then the below line can be added to include the build lib module.

dependencies {
     implementation project(path: ':name_of_the_module') 
}

Upvotes: 0

Ashwin Khadgi
Ashwin Khadgi

Reputation: 322

I have been able to fix this issue. This reading about Gradle helped me a lot. Here is what I have done:

Instead of putting the JAR file in moduleA/libs folder, I have imported the JAR file in Android Studio by clicking on the project then right click -> new -> module. I then clicked on Import .JAR/.AAR package. This created a module containing the JAR file + a gradle script.

Then, in moduleA’s gradle script, I have added this in the dependencies: compile project(path: ':name_of_the_jar_file')

I rebuilt all and it works. The JAR file is now present in the APK and there is no more crash at runtime.

Upvotes: 11

Prithiv Raj
Prithiv Raj

Reputation: 69

Create a new module just to have the common jar file in libs. And add this new module as dependency to the other modules that requires the jar file.

  1. First create a new module, say, "commonJar". File -> New -> New Module -> Choose Android Library

  2. Now, add the jar library to this module's libs folder.

  3. Then, add this module as dependency in build.gradle of other modules. compile project(':commonJar')

Upvotes: -1

Related Questions