Android Compilation Error, Type is defined multiple times

2.4k views Asked by At

I need to build a library with android studio and here is my configuration:

A project with 2 modules: 1/App: Application Module containing an application to test my library 2/MyLibrary: Library Module, containing the library i'm working on.

Each of these module has an implementation to the same external .jar library file (put in libs folder of each module) by going to project structure-> dependencies and adding libs folder which results in:

dependencies {    
    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
}

The fact i need the jar in the two modules is that i need some object from this jar library as a function argument in the library i'm writting, and therefore i need my test module to be able to provide this kind of object.

When i try to compile, i have an error saying "Type com..... is defined multiple times" and indeed the external library is visible two times in intermediates folder

It's obvious the problem comes from my way of implementing this external library. I need a way to implement it once, or avoid the two conflicting...

My Question: What is the good way of implementing an external library in two modules inside the same project?

What i already tried: -Clean project -Rebuild project -Invalidate cache -Delete intermediate folder -Check package names

2

There are 2 answers

2
Serhiy Yatsinskiy On

There are several options:

  1. As "test module" should know about the "main library module" (has it as dependency), you can add this jar lib only in the main library module but not in the "test module"
  2. Create a third module, add the jar to it, and use this module as a dependency for both the "main library module" and "test module".
0
momone50 On

Found a workaround. Don't know if it is the good way of doing things.

In the library module i changed:

implementation files('libs\\my_lib.jar')

by:

compileOnly files('libs\\my_lib.jar')

and then everything seems to compile well. I'm opened to other solutions and anything that would help better understanding.