安卓官方版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 aninclude
dependency of the:fusedLibrary
module
┌─────────────────────────────────────────┐
│ :app │
│ ▲ │
│ │ │
│ :fusedLibrary │
│ ▲ ▲ │
│ │ │ │
│ :androidLib2* :androidLib1* │
│ ▲ ▲ │
│ │ │ │
│ :androidLib3 com.google.code.gson:gson* │
└─────────────────────────────────────────┘
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:
{"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
ext {GROUP_ID = 'custom.group.id'ARTIFACT_ID = 'artifactid'
}android {compileSdkVersion 25buildToolsVersion "25.0.0"compileOptions {kotlinOptions.freeCompilerArgs += ['-module-name', "$GROUP_ID.$ARTIFACT_ID"]}defaultConfig {...}buildTypes {...}
}
官方已知問題
融合庫是一個新插件,我們正在解決一些已知問題,以實現所有用例。
- 合并的 AAR 中未包含
lint.jar
文件 - 向其他 .aar 文件添加文件依賴項
- 不支持合并 RenderScript 和 Prefab 工件
期待官方進一步完善。