Error building android library - direct local AAR file dependencies are not supported. A solution.

If you build .AAR library and add as a dependancy another .AAR library, you will get the following error:

Execution failed for task ':library_module:myAarLib'.
> Direct local .aar file dependencies are not supported when building an AAR. 
The resulting AAR would be broken because the classes and Android resources from any local .aar 
file dependencies would not be packaged in the resulting AAR. Previous versions of the Android 
Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The 
following direct local .aar file dependencies of the :library_module project caused this error: 
AnotherAARLib.aar

where myAarLib is a AAR library which depends on AnotherAARLib.aar:

implementation files('libs/AnotherAARLib.aar')

Note, that JAR files can be included as dependencies to AAR files without problems, for example:

implementation files('libs/smart-exception-common-0.2.1.jar')
implementation files('libs/smart-exception-java-0.2.1.jar')

A solution to this situation is a manual building of your target AAR:

  1. Build your target AAR without including other AAR dependencies.
  2. Merge the produced AAR with another AARs on which it depends.

Let’s go with an example. Suppose we have built a target x.aar without dependency on y.aar.

Step 1: compiling target AAR without AAR dependencies

The contents of x.aar:

libs
META-INF
AndroidManifest.xml
classes.jar
proguard.txt
R.txt

libs contains JAR files on which x.aar depends. classes.jar contains compiled *.class files.

The contents of y.aar:

jni
META-INF
res
AndroidManifest.xml
classes.jar
proguard.txt
R.txt

jni folder contains folders of native dynamic libraries:

arm64-v8a
armeabi-v7a
x86
x86_64

and res folder contains a raw folder with text files (licenses).

Step 2: merging two AARs

  1. Extract classes.jar from x.aar
  2. Rename classes.jar to classes.zip. This will allow your archiver program to add files/folders to zip without decompressing first.
  3. Extract classes.jar from y.aar, rename to classes.zip and extract the contents.
  4. Drag-and-drop the extracted contents from step 3 to classes.zip from step 2.
  5. Rename classes.zip from step 2 to classes.jar and drag-and-drop to x.aar.
  6. Drag-and-drop jni and res folders from y.aar to x.aar.

The merging process is finished.

Note that I assumed your desktop UI treats AAR files as ZIP archives while JAR files as executables which are not ZIP files. It’s possible your desktop treats JAR files as ZIPs so no need to rename them to ZIPs before drag-and-drops. The same relates to AAR, they may be unknown to your desktop as ZIP files. So you’ll need to treat them renaming to ZIPs before drag-and-drops.


See also