安卓官方版fat-aar:使用Fused Library将多个Android库发布为一个库

安卓官方版fat-aar:使用Fused Library将多个Android库发布为一个库 - Wesley's Blog

在开发 sdk 时,内部一般会划分好几个模块。但当需要打包成一个模块发布时,往往需要依赖第三方插件。比如著名的 fat-aar:github.com/kezong/fat-...,但该库不支持 AGP8.0 了,有其他开发者单独fork了一个仓库来进行维护,支持 AGP8.0+:aasitnikov/fat-aar-android: Gradle plugin for merging android libraries (AAR)

现在安卓官方终于支持将多个库打包成一个aar 了:Publish multiple Android libraries as one with Fused Library | Android Studio | Android Developers。宣布从 AGP8.12 开始可以使用com.android.fused-library来进行合并多模块。不过从Maven Repository: com.android.fused-library >> com.android.fused-library.gradle.plugin上面可以看到,一些早期版本也可以支持的,不过可能有 bug,我试了 AGP8.10 也是可以的。

相比 fat-aar 的优点是:

  • 官方支持
  • 合并模块时支持依赖传递,也就是子模块的依赖树会出现在 pom 文件里面。fat-aar 需要通过pom.withXml 来处理。

缺点是:

  • 支持插件版本有限,官方表示是AGP8.12 开始才支持。
  • 传递依赖项不会打包。需要逐个指定打包进 aar 的依赖。

后面还会继续说该库目前的问题,先看看怎么用。

官方的工程示范:

readme 的插件名称目前是写错了的:应该是com.android.fused-library

gradle-recipes/applyFusedLibraryPlugin at agp-8.10 · android/gradle-recipes

可以参考这个进行插件引入:

Publish multiple Android libraries as one with Fused Library | Android Studio | Android Developers

问题

嵌套依赖不支持打包

官方的例子是支持嵌套的:

* indicates an include dependency of the :fusedLibrary module

ruby 复制代码
┌─────────────────────────────────────────┐
│             :app                        │
│               ▲                         │
│               │                         │
│         :fusedLibrary                   │
│         ▲           ▲                   │
│         │           │                   │
│    :androidLib2*  :androidLib1*         │
│         ▲            ▲                  │
│         │            │                  │
│ :androidLib3 com.google.code.gson:gson* │
└─────────────────────────────────────────┘
makefile 复制代码
dependencies {
    include(project(":androidLib1"))
    include(project(":androidLib2"))
    include("com.google.code.gson:gson:2.10.1")
    include(files("libs/simple-jar-with-A_DoIExist-class.jar"))
}

实际上androidLib3 没有被打包,变成了一个依赖fused-library-samples:androidLib3:unspecified:

json 复制代码
{
  "included": [
    "project :androidLib1",
    "project :androidLib2",
    "com.google.code.gson:gson:2.10.1"
  ],
  "dependencies": [
    "org.jetbrains.kotlin:kotlin-stdlib:1.9.22",
    "org.jetbrains:annotations:13.0",
    "fused-library-samples:androidLib3:unspecified"
  ]
}

有一些远程包暂时打包不了

A failure occurred while executing com.android.build.gradle.tasks.FusedLibraryMergeArtifactTask$FusedLibraryMergeArtifactWorkAction

java.lang.NullPointerException (no error message)

暂不支持携带源码发布

暂不能混淆

不支持Databinding

无法在单个融合库中融合多个 build 类型和产品变种。需要为不同的变体创建单独的融合库。

kotlin_module冲突

如果有两个base模块会产生冲突, base/base ,feature/base

2 files found with path 'META-INF/base_release.kotlin_module' from inputs:

需要修改一下kotlin_module 的名字

android - Duplicate files copied in APK META-INF/library_release.kotlin_module - Stack Overflow

bash 复制代码
ext {
    GROUP_ID = 'custom.group.id'
    ARTIFACT_ID = 'artifactid'
}
​
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
​
    compileOptions {
        kotlinOptions.freeCompilerArgs += ['-module-name', "$GROUP_ID.$ARTIFACT_ID"]
    }
​
    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }
}

官方已知问题

融合库是一个新插件,我们正在解决一些已知问题,以实现所有用例。

  • 合并的 AAR 中未包含 lint.jar 文件
  • 向其他 .aar 文件添加文件依赖项
  • 不支持合并 RenderScript 和 Prefab 工件

期待官方进一步完善。

相关推荐
alexhilton2 小时前
Jetpack Compose中的阴影艺术
android·kotlin·android jetpack
百***61875 小时前
Spring的构造注入
android·java·spring
Tom4i5 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
yi诺千金6 小时前
Android U 自由窗口(浮窗)——启动流程(system_server侧流程)
android
清空mega8 小时前
第11章 网络编程
android·网络
自动化BUG制造器8 小时前
Android UI 线程不一定是主线程
android
无知的前端8 小时前
一文读懂-Jetpack与AndroidX
android·kotlin·android jetpack
河铃旅鹿10 小时前
Android开发-java版:SQLite数据库
android·数据库·笔记·学习·sqlite
旋律逍遥10 小时前
《Framework 开发》3、开发工具及命令行知识装备
android
啦啦91171410 小时前
安卓手机/平板/TV版 Rotation强制横屏显示工具!免ROOT可用!再推荐突破手机限制的3款神器
android·智能手机·电脑