Search code examples
androidmoduleaar

Android How to add same AAR file in an app as well as module of an app


I'm developing an Android app that has some modules too. Ex. app name is 'MyApp' and the modules of the MyApp are 'Module1', and 'Module2'.

I've an AAR file ('myaar-1.1.0.aar'). I have added the myaar.aar file in MyApp successfully. I also want this AAR file in my Module1. So I have tried to add it in Module1 as follows:

  • Created a libs folder and added AAR file there.
  • In Module1's gradle file, added code as below:

implementation files('libs/myaar-1.1.0.aar')

During compilation, I'm getting an error as " Duplicate class myTestActivity found in modules myaar-1.1.0.aar"

I searched StackOverflow, but could not find a solution for this case.

How can I use the AAR file in the main project as well as in a project's module?


Solution

  • If you want to use the AAR in the module named Module1, then you will need to add it to that module.

    You can do so like this:

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

    I am assuming that MyApp depends on Module1, so, in order to allow MyApp to recognize and use the AAR, you need to make it transitive.

    To do that, in your dependencies block of Module1, add this:

    dependencies {
         api project('package.name.of.your.aar:yourAAR@aar')
    }
    

    There are also other ways to put it in the dependencies block:

    • api project (name:'AAR name', ext:'aar')
    • api project(path: ':AAR_NAME')