WISHY
WISHY

Reputation: 11999

Create one aar from multi module library in android

I have the following modules in my aaplication

Module

-app

-library_1 

-library_2

the module library_1 'has dependecy of library_2 build gradle library_1

dependencies {
api project(":library_2")
}

When I build the project I get the aar which I put in my app module

dependencies {
implementation files('libs/library_1-debug.aar')
}

Now when I run my app I get the exception java.lang.NoClassDefFoundError: Failed resolution of: Lcom/dinklo/library_2/LibraryB

The class LibraryB is defined in library_2 and being called from library_1

I am looking for a solution where I can get a single aar file for all the modules I have.

Upvotes: 6

Views: 2747

Answers (2)

SuperCed
SuperCed

Reputation: 133

Since kezong fat aar is not maintained anymore and does not work with gradle 8, I did a fix using bash. I don't use any plugin. I decompress aar archives and copy jni folder into decompressed final arr. Then I decompress also classes.jar and copy content into my final decompressed classes.jar in arr final package. This seems to work great ! You just have to use unzip and zip command in order to decompress and compress aar and jar. I works easily because I don't have any resource in my aar dependencies.

    #-----------------------------------------------------------------------------
 # Do kezong aar plugin job -- integrate dependency into aar package
 
 # decompress dependency aar 
 unzip $VM_AAR_FOLDER/dependency.aar -d $VM_AAR_FOLDER/dependency
 
 # decompress dependency classes.jar
 unzip $VM_AAR_FOLDER/dependency/classes.jar -d $VM_AAR_FOLDER/dependency/classes
 
 
 # decompress target aar file
 unzip $VME_SDK_BUILD_PACKAGE/target.aar -d $VME_SDK_BUILD_PACKAGE/target
 
 # decompress target.jar file
 unzip $VME_SDK_BUILD_PACKAGE/target/classes.jar -d $VME_SDK_BUILD_PACKAGE/target/classes
 
 # copy dependency classes into decompressed target classes folder
 cp -r $VM_AAR_FOLDER/dependency/classes/android $VME_SDK_BUILD_PACKAGE/target/classes/
 cp -r $VM_AAR_FOLDER/dependency/classes/com/company/dependency $VME_SDK_BUILD_PACKAGE/target/classes/com/company/
 
 # copy jni dependency Files into decompressed target folder
 cp -r $VM_AAR_FOLDER/dependency/jni $VME_SDK_BUILD_PACKAGE/target/
 
 # recreate classes.jar in target
 cd $VME_SDK_BUILD_PACKAGE/target/classes/
 rm -Rf ../classes.jar
 zip -r ../classes.jar *
 cd ..
 rm -Rf classes
 
 
 # recreate aar for target
 cd $VME_SDK_BUILD_PACKAGE/target/
 rm -Rf ../target.aar
 zip -r ../target.aar *
 cd ..
 rm -Rf target

Upvotes: 4

WISHY
WISHY

Reputation: 11999

I ended up using third party library Fat-aar

In root build gradle

 classpath 'com.github.kezong:fat-aar:1.3.8'

In your library module

apply plugin: 'com.kezong.fat-aar'

dependencies{
embed project(path: ':lib-2', configuration: 'default')}

I have added a sample project on github will be useful for others

Fat-arr Sample - Github

Upvotes: 4

Related Questions