1 Star 2 Fork 0

引领潮流 / fat-aar-android

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

fat-aar-android

license Maven Central

该插件提供了将library以及它依赖的library一起打包成一个完整aar的解决方案,支持AGP 3.0及以上。(目前测试的版本范围是AGP 3.0 - 4.2.0,Gradle 4.9 - 6.8)

如何使用

第一步: Apply classpath

添加以下代码到你工程根目录下的build.gradle文件中:

JCenter于2021.5.1不再提供服务, 如果你使用了JCenter中的版本,建议更改一下包名切换至Maven central, 比如: 'com.kezong:fat-aar:x.x.x' => 'com.github.kezong:fat-aar:x.x.x'

For Maven Central (The lastest release is available on Maven Central):

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.github.kezong:fat-aar:1.3.6'
    }
}

For JCenter (Deprecated, before 1.3.4):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.kezong:fat-aar:1.3.3'
    }
}

第二步: Add plugin

添加以下代码到你的主library的build.gradle中:

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

第三步: Embed dependencies

  • embed你所需要的工程, 用法类似implementation

代码所示:

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, just build in flavor1
    flavor1Embed project(path: ':lib-aar-local', configuration: 'default')
    // local full aar dependency, just build in debug
    debugEmbed(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.12.0'
    // don't want to embed in
    implementation('androidx.appcompat:appcompat:1.2.0')
}

第四步: 执行assemble命令

  • 在你的工程目录下执行assemble指令,其中lib-main为你主library的工程名称,你可以根据不同的flavor以及不同的buildType来决定执行具体的assemble指令
# assemble all 
./gradlew :lib-main:assemble

# assemble debug
./gradlew :lib-main:assembleDebug

# assemble flavor
./gradlew :lib-main:assembleFlavor1Debug

最终合并产物会覆盖原有aar,同时路径会打印在log信息中.

多级依赖

本地依赖

如果你想将本地所有相关的依赖项全部包含在最终产物中,你需要在你主library中对所有依赖都加上embed关键字

比如,mainLib依赖lib1,lib1依赖lib2,如果你想将所有依赖都打入最终产物,你必须在mainLib的build.gradle中对lib1以及lib2都加上embed关键字

远程依赖

如果你想将所有远程依赖在pom中声明的依赖项同时打入在最终产物里的话,你需要在build.gradle中将transitive值改为true,例如:

fataar {
    /**
     * If transitive is true, local jar module and remote library's dependencies will be embed.
     * If transitive is false, just embed first level dependency
     * Local aar project does not support transitive, always embed first level
     * Default value is false
     * @since 1.3.0
     */
    transitive = true
}

如果你将transitive的值改成了true,并且想忽略pom文件中的某一个依赖项,你可以添加exclude关键字,例如:

embed('com.facebook.fresco:fresco:1.11.0') {
    // exclude any group or module
    exclude(group:'com.facebook.soloader', module:'soloader')
    // exclude all dependencies
    transitive = false
}

更多使用方式可参考 example.

关于 AAR 文件

AAR是Android提供的一种官方文件形式; 该文件本身是一个Zip文件,并且包含Android里所有的元素; 可以参考 aar文件详解.

支持功能列表:

  • 支持flavor配置
  • AndroidManifest合并
  • Classes合并
  • Jar合并
  • Res合并
  • Assets合并
  • Jni合并
  • R.txt合并
  • R.class合并
  • DataBinding合并
  • Proguard合并
  • Kotlin module合并

Gradle版本支持

Version Gradle Plugin Gradle
1.0.1 3.1.0 - 3.2.1 4.4 - 6.0
1.1.6 3.1.0 - 3.4.1 4.4 - 6.0
1.1.10 3.0.0 - 3.4.1 4.1 - 6.0
1.2.6 3.0.0 - 3.5.0 4.1 - 6.0
1.2.8 3.0.0 - 3.5.9 4.1 - 6.8
1.2.11 - 1.2.14 3.0.0 - 3.6.9 4.1 - 6.8
1.2.15 - 1.2.16 3.0.0 - 4.0.2 4.1 - 6.8
1.2.17 3.0.0 - 4.0.2 4.9 - 6.8
1.2.18+ 3.0.0 - 4.1.0 4.9 - 6.8
1.3.+ 3.0.0 - 4.1.0 4.9 - 6.8
1.3.4 3.0.0 - 4.1.0 4.9+

Gradle Plugin和所需求的Gradle版本官方文档

更新日志

  • 1.3.6
  • 1.3.5
    • 修复在仅有jar工程时jar无法合并的问题. #255 #288
    • 修复在使用Gradle 6.0-6.8时的编译错误. #277
  • 1.3.4
    • 支持Gradle 6.8 #274
  • 1.3.3
    • 修复异常"Can not find task bundleDebugAar". #84
    • 修复当工程解析失败时产生的异常.
    • 当AndroidManifest合并时抛出异常.
  • 1.3.1
    • R.class合并采用Transform,解决大部分R class找不到的问题.
    • 支持consumerProguardFiles合并
    • 支持kotlin_module合并,支持top-level机制
    • 支持flavor中missingDimensionStrategy
    • 修复依赖的flavor产物更名后无法找到的问题
    • 修复AGP 3.0 - 3.1 Jar包无法合并的问题
    • 修复某些情况下AGP版本获取不到的问题
  • 1.2.20
    • 修复获取产物名时的空指针异常. #214
    • r-classes.jar重命名,加上包名作为前缀.
  • 1.2.19
    • 支持embed没有class.jar的aar #157
    • 支持embed没有AndroidManifest.xml的aar #206
    • 修复上传至maven时,R.class未包含进aar的BUG #200
  • 1.2.18
    • 适配gradle plugin 4.1.0 #201
  • 1.2.17
    • 支持databinding合并 #25 #67 #142
    • Use Gradle's configuration avoidance APIs #195
    • Support incremental build #199 #185
    • Fix wrong directory for aar's jar libs #154
  • 1.2.16
    • 修复gradle plugin版本不在根目录下就找不到的问题 #172
    • 修复在gradle plugin 4.0构建的产物中有可能styleable资源找不到的问题 #163
  • 1.2.15
    • 支持gradle plugin 4.0.0 #147
    • 修复在Android Studio 4.0.0上embed的库无法直接索引源码的问题 #148
    • 修复lint编译错误 #152
  • 1.2.12
    • 添加对buildType以及flavor的支持,例如debugEmbed以及flavorEmbed. #135 #137
    • 修复一些编译时的warning.
  • 1.2.11
    • 修复在gradle plugin 3.6.0下编译variants会error的情况 #126
    • 修复在gradle plugin 3.6.0下编译出来的aar,在编译apk时会出现资源符号对象找不到的问题
  • 1.2.9
    • 适配gradle plugin 3.6.1 #120
  • 1.2.8
    • 适配gradle6.0+版本 #97
  • 1.2.7
    • 修复在3.5.0中androidmafest合并报错的问题 #62 #65
  • 1.2.6
    • 适配gradle plugin 3.5.0 #53#58
  • 1.2.5
    • 修复任务名称重复导致编译错误的问题 #48
    • 如果开启minifyEnabled,所有的jar包将合入classes.jar文件
  • 1.2.4
    • 修复在windows平台上,jni和asset无法打入aar的bug #11 #37
  • 1.2.3
    • 修复未直接依赖的R类找不到的问题 #11 #35
    • 不再需要为需要embed的依赖项主动添加compileOnly
    • embed的transitive默认值设置成false
  • 1.1.11
    • 修复gradle plugin version有可能判断错误的问题 #28
    • 修复LibraryManifestMerger.java中出现的build warning #29
    • 优化resource、assets、jni等的合并规则,不再合并至"main",而是合并至"variant" #27
    • 修复重复build造成release包下jni丢失的问题 #27
  • 1.1.10
    • 修复使用gradle plugin 3.0.1时的jar合并错误 #24
    • 修复无法正常rebuild(同时使用clean assemble)的问题 #24
  • 1.1.8
    • 弃用旧接口,处理编译时输出的warning #10
    • 将AndroidManifest的合并规则由Application改为Library #21 #23
  • 1.1.7
    • 修复直接publish至maven时,aar的R文件未合并的问题 #7
  • 1.1.6
    • 适配gradle plugin 3.3.0+ #4 #9
    • 适配gadle 4.10.0+ #8
    • 支持子module的flavor编译
    • 修复子module的class文件编译不实时更新的问题
  • 1.0.3
    • 修复assets未合并的问题
  • 1.0.1
    • 支持gradle plugin 3.1.0 - 3.2.1
    • 支持资源合并,R文件合并

常见问题

  • Application无法直接依赖embed工程: application无法直接依赖你的embed工程,必须依赖你embed工程所编译生成的aar文件

    • 为了调试方便,你可以在选择在打包aar时,在主library工程中使用embed,需要直接运行app时,采用implementation或者api
  • 资源冲突: 如果library和module中含有同名的资源(比如 string/app_name),编译将会报duplication resources的相关错误,有两种方法可以解决这个问题:

    • 考虑将library以及module中的资源都加一个前缀来避免资源冲突;
    • gradle.properties中添加android.disableResourceValidation=true可以忽略资源冲突的编译错误,程序会采用第一个找到的同名资源作为实际资源.
  • 关于混淆

    • 如果minifyEnabled设置为true,编译时会根据proguard规则过滤工程中没有引用到的类,导致App集成时找不到对象,因为大多数AAR都是提供接口的SDK,建议大家仔细梳理proguard文件。

致谢

MIT License Copyright kezong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

fat-aar 展开 收起
Groovy 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/yinlingchaoliu/fat-aar-android.git
git@gitee.com:yinlingchaoliu/fat-aar-android.git
yinlingchaoliu
fat-aar-android
fat-aar-android
master

搜索帮助