gpt4 book ai didi

android - 懒惰的Android依赖项。 gradle依赖解析之前如何构建AARs库

转载 作者:行者123 更新时间:2023-12-03 04:15:53 25 4
gpt4 key购买 nike

我有一个封闭的源库组的简化项目方案:

- app
> references library1, library2
- library1
- library2
> references library3
- library3

所有3个库都生成 aar文件,这些文件可以照常在应用程序依赖项中引用。
dependencies {
implementation project(":library1")
implementation project(":library2")
}

当我想使用混淆的aar s (在 Release模式下构建)测试我的应用程序时,问题就开始了。目前,Android插件会忽略此操作,因此我发现的唯一方法是使用预构建的Aar进行此操作。
dependencies {
//for debug build just use the local project
debugImplementation project(":library1")
debugImplementation project(":library2")
//for release build use manually added aar in libs folder
releaseImplementation (name: 'library1-release', ext: 'aar')
releaseImplementation (name: 'library2-release', ext: 'aar')
//i need to add library3 too otherwise it will not find
//method referenced there because aar are not bundled togheter
//by default
releaseImplementation (name: 'library3-release', ext: 'aar')
}

这很好。

为此,我创建了一个像这样的小脚本(在app / scripts / build-aar.sh中)
//move from scripts folder to root project folder
cd "$(dirname "$BASH_SOURCE")/../../"
//assembleRelease all the aar (which enables Proguard obfuscation)
./gradlew clean assembleRelease --info &&
//copy them in the app libs folder
cp -rf library1/build/outputs/aar/library1-release.aar app/libs/library1-release.aar
cp -rf library2/build/outputs/aar/library2-release.aar app/libs/library2-release.aar
cp -rf library3/build/outputs/aar/library3-release.aar app/libs/library3-release.aar

这种方法的问题是我需要在git中对所有aar进行版本控制,否则当我选择“Release”作为构建变体时,应用程序将无法编译。

虽然我会gitignore删除libs文件夹中的所有aar并在应用程序搜索其依赖项之前即时构建它们。

我已经尝试过这样的事情:
applicationVariants.all { variant ->
if (variant.buildType.name == "release") {
variant.preBuildProvider.configure {
dependsOn(buildReleaseAar)
}
}
}

task buildReleaseAar {
dependsOn (
':library1:assembleRelease',
':library2:assembleRelease',
':library3:assembleRelease'
)
doLast {
//copy the aars from build folders in app/libs folder
}
}

但是之前已经检查了依赖项,所以如果没有libs文件夹中的aars,我什至无法再同步项目。

用于解决此问题的伪逻辑应为:

1)对像(obfuscatedAar)这样的依赖项有一个新任务

2)让此任务检查libs文件夹中是否存在aar,如果不对所有3个库都运行assembleRelease并在那里复制生成的AAR

像这样:
configurations {
releaseObfuscatedAar
}

dependencies {
//other implementations libs

//for debug build just use the local project
debugImplementation project(":library1")
debugImplementation project(":library2")

releaseObfuscatedAar('library1')
releaseObfuscatedAar('library2')
releaseObfuscatedAar('library3')
}

//and some other task that build the aar before checking if
//dependency is present

Questions:

  • Is this possible?
  • Is this a good approach to test my proguard-rules added in libraries projects (and be sure I didn't broke public API when obfuscation is on)?


Reference to same question in Gradle Forum

最佳答案

我知道我的回答来晚了。
我遇到了类似的问题,我依靠本地的maven存储库(位于~/.m2中的默认存储库)解决了这个问题。
一开始我的方法基本上是使:app:assemble${variant.name}任务依赖于:my-library:publishToMavenLocal
这种方法行得通,但是在库将其dependencies {...}放在本地maven存储库之前,仍然需要评估aar块。此外,如果仅在Android Studio / IntelliJ中同步项目,则assemble任务不会运行。
因此,这就是我最后所做的:tasks.getByPath(":prepareKotlinBuildScriptModel").dependsOn(":library:publishToMavenLocal")这使得在同步项目时使用您的aar填充本地Maven存储库,因此在以后的步骤中可以满足对这些aar的依赖。
如果您想尝试一下,请不要忘记在mavenLocal()闭包中添加repositories {...}
当然,此解决方案需要付出一定的成本,这是在每个同步上部署本地Maven。就我而言,它相对较快(由于gradle缓存),但是只是说:)

关于android - 懒惰的Android依赖项。 gradle依赖解析之前如何构建AARs库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56116560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com