Objective
Some time ago, I encountered a problem in implementing the Gradle multi-module build, which we used to integrate the developer into the project in the form of a jar package, but since Android Studio has a multi-module concept, and our SDK has been built with multiple modules, But we have a problem here is that the modules are interrelated and cannot be packaged separately for each module, and each of the modules will generate the corresponding AAR, but not the dependent module code, and don't ask why I know that you changed the AAR suffix to zip, Then decompile the Classes.jar to see it. So here we have a need to merge AAR, and let's show you how to do it.
Android-fat-aar
When I encountered this problem, I went to GitHub to search, already someone will merge AAR script open source, open source address is as follows:
Https://github.com/adwiv/android-fat-aar
What is AAR?
What is AAR? What's the difference between it and the jar package? How should it be used? I am sure that we will have these doubts. First AAR is for the Android library , you can understand the IDE for the Android library packaging, an AAR contains what?
Its file suffix name is. AAR, which itself is a zip file that forces the following files to be included:
- /androidmanifest.xml
- /classes.jar
- /res/
- /r.txt
In addition, the AAR file can include one or more of the following optional entries:
- /assets/
- /libs/name.jar
- /jni/abi_name/name. So (where abi_name is one of the Android supported ABIs)
- /proguard.txt
- /lint.jar
Specifically see here to see how to create an Android Library:
Https://developer.android.com/studio/projects/android-library.html#aar-contents
What is the difference between a jar package and an AAR package?
jar: Contains only the class file and the manifest file, does not contain the resource file, the slice and so on all res files.
AAR: Contains all resources, class, and res resource files.
If you are simply working with some class libraries, you can use the files directly, *.jar
and if you want to use both the class library and the utility resources, then you can create an Android library that uses the files it generates *.aar
.
How to use the jar file we should be familiar with it, copy it to the project's Libs directory, and then add the following script in Gradle:
dependencies { compile fileTree(include: [‘*.jar‘dir:‘libs‘)}
The AAR files need to be copied to the Libs directory as well, and integrated in the following ways:
repositories { flatDir { dirs‘libs‘ }}dependencies { compile(name:‘your aar‘, ext:‘aar‘)}
Multi-module construction combined with AAR
This is the focus of this article, we can then each module under the Build/outputs/aar to find the compiled generated *.aar
files.
Step 1:
Add the Gradle file ' Fat-aar.gradle ' to your project directory and apply:
from‘fat-aar.gradle‘
Step 2: Define embedded dependencies
You need to modify your previous dependencies and change compile
to embedded
as the AAR you want to merge. Use the following example:
apply from : ' fat-aar.gradle ' dependencies {compile Filetree (dir: ' Libs '
, include : [ ' *.jar ' ]) //Order of dependencies decide which would has precedence in case of duplicates //during Manifest/resource merger Embedded project ( ': Librarytwo ' ) Embedded project ( ': Libraryone ' ) //We can embed Androi D libraries from Maven too embedded ' com.adwiv.internal:librarythree:1.0.0 ' Compile ' com.android.support:appcompat-v7:22.2.0 ' // Non embedded Dependency }
You can synthesize the AAR of multiple module generation in one of these ways, and you can create a new demo project to test
Android Multi-module build combined AAR solution