23

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

4
  • If MyLib is local, you should use compile project(':MyLib') Commented Jun 10, 2015 at 9:39
  • Yes, MyLib is local but packaged into an aar. MyLib and MyApp are not in the same project. It is two different project. Commented Jun 10, 2015 at 10:20
  • You can always refer to another folder outside the projetc Commented Jun 10, 2015 at 10:28
  • Refer to a project yes, but how it is possible to perform "compile projet('MyLib@aar')"? Commented Jun 10, 2015 at 11:30

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm now able to generate a pom-default.xml file containing deps when building the aar library to publish to local repo (using maven and maven-publish gradle plugins). But this pom file is not included inside the aar archive (it is only visible in the build folder but not included inside the final archive). How can I embedded it inside the aar archive?
@BrentM, is the same thing mentioned in the question possible, if the aar file is present in libs directory of Android applicaiton: compile(name:'library_name', ext:'aar') I understand that you did not find any definitive information for this use-case. But, is there any update which you have regarding this? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.