The solution of merging aar works with the android gradle plugin, the android plugin's version of the development is 3.0.1
and higher. (Tested in gradle plugin 3.0.1 - 3.4.2, and gradle 4.6 - 5.5)
Add snippet below to your root build script file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:xxx'
classpath 'com.kezong:fat-aar:1.2.5'
}
}
Add snippet below to the build.gradle
of your android library:
apply plugin: 'com.kezong.fat-aar'
change implementation
or api
to embed
while you want to embed the dependency in the library. Like this:
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
// java dependency
embed project(path: ':lib-java', configuration:'default')
// aar dependency
embed project(path: ':lib-aar', configuration:'default')
// aar dependency
embed project(path: ':lib-aar2', configuration:'default')
// local full aar dependency
embed project(path: ':lib-aar-local', configuration:'default')
// local full aar dependency
embed (name:'lib-aar-local2',ext:'aar')
// remote jar dependency
embed 'com.google.guava:guava:20.0'
// remote aar dependency
embed 'com.facebook.fresco:fresco:1.11.0'
// don't want to embed in
// implementation is not recommended because the dependency may be different with the version in application, resulting in the R class not found.
compileOnly 'com.android.support:appcompat-v7:27.1.1'
}
If you want to including local transitive dependencies in final artifact, you must add embed
for transitive dependencies in your main library.
For example, mainLib depend on subLib1, subLib1 depend on subLib2, If you want including all dependencies in final artifact, you must add embed
for subLib1 and subLib2 in mainLib build.gradle
If you want to including all remote transitive dependencies which in pom file, you need change the embed
's transitive value to true in your build.gradle
, like this:
// the default value is false
// invalid for local aar dependency
configurations.embed.transitive = true
If you change the transitive value to true,and want to ignore a dependency in its POM file, you can add exclude keywords, like this:
embed('com.facebook.fresco:fresco:1.11.0') {
exclude(group:'com.facebook.soloader', module:'soloader')
}
More usage see example.
AAR is a file format for android library. The file itself is a zip file that containing useful stuff in android. See anatomy of an aar file here.
support list for now:
- productFlavors
- manifest merge
- classes jar and external jars merge
- res merge
- assets merge
- jni libs merge
- proguard.txt merge
- R.txt merge
- R.class merge
- Proguard note. Produce lots of(maybe)
Note: duplicate definition of library class
, while proguard is on. A workaround is to add-dontnote
inproguard-rules.pro
. - The overlay order of res merge is changed: Embedded dependency has higher priority than other dependencies.
- Res merge conflicts. If the library res folder and embedded dependencies res have the same res Id(mostly
string/app_name
). A duplicate resources build exception will be thrown. To avoid res conflicts:- consider using a prefix to each res Id, both in library res and aar dependencies if possible.
- Adding "android.disableResourceValidation=true" to "gradle.properties" can do a trick to skip the exception, but is not recommended.
- 1.2.5
- Fix task name repeat error #48
- If minifyEnabled, jar files would build into classes.jar
- 1.2.4
- Fix jni and assets can't embed in windows platform #37
- 1.2.3
- 1.1.11
- 1.1.10
- 1.1.8
- 1.1.7
- Support embed R file when upload maven #7
- 1.1.6
- 1.0.3
- Fix assets merge
- 1.0.1
- Support gradle plugin 3.1.0 - 3.2.1
- Support R class file merge