Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify dependencies in aar library?

I created an Android library (called MyLib) which depends on other library available on maven repo (like gson, retrofit, etc.).

MyLib
 |
 |-- Retrofit
 |-- Gson
 |-- ...

MyLib is packaged to an aar file.

The goal is to publish an aar library which can be included into an Android app (called MyApp) without specifying a second time the dependencies that MyLib uses.

MyApp
 |
 |-- MyLib
 |    |-- Retrofit
 |    |-- gson
 |    |-- ...

This is my build.gradle file for MyLib

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.3.1'
}

Now, if I want to build and run MyApp without dependency issue, I had to use the following build.gradle for MyApp (if I don't specify retrofit and gson as deps, a runtime exception is thrown because the deps are not available).

dependencies {
    compile('MyLib@aar')

    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.3.1'
}

I don't want to specify in MyApp the dependencies that are used inside MyLib, How should I write my build.gradle files?

Thansk in advance

like image 421
reevolt Avatar asked Jun 10 '15 07:06

reevolt


People also ask

How do I add a dependency to AAR?

Add your AAR or JAR as a dependency To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your .

Does AAR contain dependencies?

The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module. It means that, if you are importing a aar file using a flatDir repository you have to specify the dependencies also in your project.


Video Answer


1 Answers

When publishing an aar to a maven repository (local or remote) and including it using compile (...@aar) transitive dependencies are turned off.

To enable transitive dependencies for an aar library:

compile ('com.mylib:mylib:1.0.0@aar'){
       transitive=true
}

You can read more about this here:

Can an AAR include transitive dependencies?

This works with aar libraries that are published to a remote or local maven repository, In your case it sounds like the library will not be published to even a local maven repository. I can't find any definitive information as to if it will work in your circumstances, but you should give it a shot.

like image 60
BrentM Avatar answered Sep 28 '22 03:09

BrentM