Android Library 包含依赖模块打包成 AAR
要将 Library 引用的 Module 一起打包成 AAR,可以按照以下步骤进行操作:
- 在 Library 的
build.gradle文件中,添加以下代码:
android {
// ...
defaultPublishConfig "release"
publishing {
publications {
aar(MavenPublication) {
groupId 'com.example.library'
artifactId 'library'
version '1.0.0'
// 打成的 AAR 包中包含的 Module
artifact("$buildDir/outputs/aar/module1-release.aar")
artifact("$buildDir/outputs/aar/module2-release.aar")
// ...
pom.withXml {
// ...
}
}
}
}
}
- 在每个 Module 的
build.gradle文件中,添加以下代码:
android {
// ...
defaultConfig {
// ...
}
buildTypes {
release {
// ...
}
}
publishing {
publications {
aar(MavenPublication) {
groupId 'com.example.library'
artifactId 'module1'
version '1.0.0'
artifact("$buildDir/outputs/aar/module1-release.aar")
pom.withXml {
// ...
}
}
}
}
}
- 在 Library 的根目录下的
build.gradle文件中,添加以下代码:
allprojects {
repositories {
// ...
maven {
url "$rootDir/repo"
}
}
}
- 在 Library 的根目录下,执行以下命令:
./gradlew clean build publishToMavenLocal
-
在 Library 的根目录下,生成了一个
repo目录,里面包含了 Library 和所有 Module 的 AAR 文件。 -
在其他项目中,添加以下代码:
repositories {
// ...
maven {
url "$rootDir/repo"
}
}
dependencies {
// ...
implementation 'com.example.library:library:1.0.0'
}
这样就可以将 Library 引用的 Module 一起打包成 AAR,并在其他项目中使用了。
原文地址: https://www.cveoy.top/t/topic/oYIH 著作权归作者所有。请勿转载和采集!