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

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

在开发 sdk 时,内部一般会划分好几个模块。但当需要打包成一个模块发布时,往往需要依赖第三方插件。比如著名的 fat-aar:https://github.com/kezong/fat-aar-android,但该库不支持 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

复制代码
┌─────────────────────────────────────────┐
│             :app                        │
│               ▲                         │
│               │                         │
│         :fusedLibrary                   │
│         ▲           ▲                   │
│         │           │                   │
│    :androidLib2*  :androidLib1*         │
│         ▲            ▲                  │
│         │            │                  │
│ :androidLib3 com.google.code.gson:gson* │
└─────────────────────────────────────────┘
kotlin 复制代码
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

groovy 复制代码
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 工件

期待官方进一步完善。

相关推荐
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen2 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone2 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui