多个project合成一个aar发布
2026/5/28 1:54:54 网站建设 项目流程

背景是有个sdk项目,最近我需要接入它使用。在接入的时候遇到了一些问题,所以有本文。

背景问题

sdk项目依赖了三个子模块,大约是这样的:

dependencies{implementation(project(":module-A"))implementation(project(":module-B"))implementation(project(":module-C"))}

我加了点代码publish到我新创建的maven服务。

  • 编译报错依赖 module-A、B、C找不到, 手动忽略了报错。
  • 运行时报错,这时候我发现module-A、B、C

这时候我发现这三个项目都不会发布到maven上。这几个项目我都不熟悉,特别是他们都定义了多个productFlavors。
这种情况怎么简单合成一个repo给我自己使用呢?

以前的经验

android多aar合并

android{ buildTypes{ release { minifyEnabled true } } }

大约就是自己写脚本合并,使用python,和项目无关,不用收到agp版本的制约。
但是这个写了脚本我后面也不一定会用到,我就在想有没有其他的方式。

fat-aar-:

https://github.com/aasitnikov/fat-aar-android

kezong/fat-aar-android 这个没维护了,只是支持到AGP3.0.找到一些它的issue,发现有一个下游的分支还在更新。

aasitnikov/fat-aar-android 下游这个支持到 agp 8.13

我试了一下,发现 存在问题:

  1. embed 的关键词没有找到
  2. 项目中使用kotlin-dsl,不知道是不是我哪里设置不对,用的时候有一些报错

fusedlibrary

fused-library

这个也存在一些问题:

  1. viewBinding = true 不支持
  2. minifyEnabled true会有报错,比较奇怪

不过总体是可以用的,可以这样使用:

  1. 创建一个新的module,这个module下只有一个build.gradle.kts的文件
  2. 在 build.gradle.kts 这里依赖其他的module,差不多是这样
dependencies{include(project(":module-A"))include(project(":module-B"))include(project(":module-C"))}
  1. step by step的说明见这里 add-fused-library-module

  2. 发布,直接publish就好

./gradlew clean :myFusedLibrary:publish

step by step细节

  1. Enable fused library support by addingandroid.experimental.fusedLibrarySupport=trueto thegradle.propertiesfile.
  2. Appendinclude(":myFusedLibrary")to thesettings.gradle.ktsfile.
  3. Addandroid-fusedlibrary = { id = "com.android.fused-library", version.ref = "agp" }under the[plugins]section in thegradle/libs.versions.tomlfile.
  4. Addalias(libs.plugins.android.fusedlibrary) apply falsein the plugins block in the top levelbuild.gradle.ktsfile.
  5. To create themyFusedLibrarymodule, create a new directory called myFusedLibrary (right-click ‘My Application’ > New > Directory).
  6. Create abuild.gradle.ktsfile in the myFusedLibrary module (right-click the myFusedLibrary module > New > File).
  7. Paste the following into themyFusedLibrary/build.gradle.ktsfile:
plugins{alias(libs.plugins.android.fusedlibrary)`maven-publish`}valbuildTime=java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))androidFusedLibrary{namespace="org.yeshen.fusedlibrary"minSdk=21}dependencies{include(project(":module-A"))include(project(":module-B"))include(project(":module-C"))}publishing{publications{register<MavenPublication>("release"){groupId="org.yeshen.fusedlibrary"artifactId="awesome"version="1.0.$buildTime"from(components["fusedLibraryComponent"])}}repositories{maven{url=uri("file://${rootDir}/../yeshen/file_release")name="Local"}}}

参考

  1. fused-library
  2. aasitnikov/fat-aar-android
  3. android多aar合并

本系列其他文章

  1. 多个project合成一个aar发布
  2. android多aar合并
  3. 多AAR合并之二Merge multiple jar/aar maven url into one

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询