Android Studio: including AAR library from library project

In my Android Studio project, I have two subprojects / modules: an Android application ( App1 ) and an Android library project ( LibraryProject1 ). App1 depends on LibraryProject1 . So far so good.

However, LibraryProject1 , in turn, needs to import the AAR library to work properly.

So my configuration is as follows:
App1 includes LibraryProject1
LibraryProject1 includes dependency.aar

Now, to enable dependecy.aar , I use the method described here:

How to manually enable an external aar package using the new Android Gradle build system

So basically in my build.gradle for LibraryProject1 I have:

 repositories { flatDir { dirs 'libs' } } dependencies { compile (name:'dependency', ext:'aar') //my AAR dependency compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' } 

Obviously, I placed the dependency.aar file in the libs directory of LibraryProject1

However, this does not work. It seems that the repository added by LibraryProject1 is completely ignored, and the local "libs" folder is not included as a repository, which leads to compilation failure.

If I add the repository from App1 build.gradle , it will work, but I do not want to do this, it needs build.gradle AAR file, not App1 .

How can i do this?

+5
source share
2 answers

Well, I found a way, but since it is very โ€œhacked,โ€ I will leave the question open if anyone comes up with a better โ€œrightโ€ solution.

Basically the problem is that the flatDir repository flatDir ignored at compile time if included from the LibraryProject1 build.gradle script, so I use App1 build.gradle to โ€œenterโ€ the flatDir repository in LibraryProject1 . Something like that:

 //App1 build.gradle dependencies { //get libraryproject1 Project object Project p = project(':libraryproject1') //inject repository repositories{ flatDir { dirs p.projectDir.absolutePath + '/libs' } } //include libraryproject1 compile p } 

This allows LibraryProject1 include an external AAR library without including App1 . It is hacked, but it works. Please note that you still need to put:

 //LibraryProject1 build.gradle repositories { flatDir { dirs './libs' } } 

inside LibraryProject1 build.gradle otherwise, even if the project itself is compiled, the IDE will not recognize the types included in the AAR library. Note that ./ along the way also seems important, without it, the IDE still does not recognize types.

+3
source

I ran into the same problem, and I figure it out by putting all the libraries in it, depends on the LibraryProject1 library in LibraryProject1 / libs like .jar. I think the aar library cannot be linked to another aar library. Hope to help you, Best regards

0
source

All Articles