一个发布到maven central的android库项目,为了方便管理,在build.gradle文件中加入了打包处理的一段脚本:

//生成aar

libraryVariants.all { variant ->

def name = "library-${versionMajor}.${versionMinor}.${versionPatch}.aar"

//打包输出文件名

variant.outputs.all {

outputFileName = name

}

//打包完成后的动作

variant.assembleProvider.get().doLast {

File out = new File("${project.rootDir}/maven")

variant.outputs.all { file ->

// 复制到指定文件夹

copy {

from file.outputFile

into out

}

}

}

}

这段脚本是修改默认的输出文件名,并在打包完成后将打包文件复制到指定目录下。方便对打包文件的管理。 执行gradle任务assembleRelease生成aar,脚本的目的倒是实现了,但是build窗口会出现一个警告,显示如下内容:

API 'variantOutput.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.

It will be removed in version 9.0 of the Android Gradle plugin.

For more information, see https://d.android.com/r/tools/task-configuration-avoidance.

To determine what is calling variantOutput.getPackageLibrary(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.

Affected Modules: mylibrary

在gradle.properties文件中加入一行android.debug.obsoleteApi=true,再次执行任务assembleRelease,警告内容变成如下所示:

API 'variantOutput.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.

It will be removed in version 9.0 of the Android Gradle plugin.

For more information, see https://d.android.com/r/tools/task-configuration-avoidance.

REASON: Called from: D:\Android\UtilsTest\mylibrary\build.gradle:90

WARNING: Debugging obsolete API calls can take time during configuration. It's recommended to not keep it on at all times.

其中REASON: Called from: D:\Android\UtilsTest\mylibrary\build.gradle:90这一行指明了出现警告的代码:from file.outputFile。 file.outputFile是用来获取打包文件完整路径的脚本,也就是这行脚本调用了已经弃用,并且在gradle9.0中会移除的函数getPackageLibrary()。

虽然目前对我的项目还没有什么影响,但我还是想把这个问题彻底解决掉。 file.outputFile不能用,就要另外想办法获得打包文件的路径。按照提示先把脚本改成下面这样,用println看看这个getPackageLibraryProvider()到底是个什么东东:

variant.assembleProvider.get().doLast {

File out = new File("${project.rootDir}/maven")

variant.outputs.all {

def provider = variant.packageLibraryProvider.get()

println("provider:$provider")

}

}

provider输出结果是这样:

provider:task ':mylibrary:bundleReleaseAar'

呃,看不懂。 干脆在println("provider:$provider")这行下个断点,观察一下。 按上图所示选择Run/Debug的配置项,然后Debug。 观察provider变量,发现两个可疑属性。其实上面几个archiveFile、archiveFileName之类的也比较可疑,但这两个属性名更接近我们的目标。

variant.assembleProvider.get().doLast {

File out = new File("${project.rootDir}/maven")

variant.outputs.all {

def provider = variant.packageLibraryProvider.get()

println("provider:$provider")

println("dir: ${provider.destinationDir}")

println("directory: ${provider.destinationDirectory}")

}

}

继续打印这两个属性看看:

provider:task ':mylibrary:bundleReleaseAar'

**dir:D:\Android\UtilsTest\mylibrary\build\outputs\aar**

directory: task ':mylibrary:bundleReleaseAar' property 'destinationDirectory'

map(java.io.File task ':mylibrary:bundleReleaseAar' property 'destinationDirectory' org.gradle.api.internal.file.DefaultFilePropertyFactory$ToFileTransformer@69b41613)

D:\Android\UtilsTest\mylibrary\build\outputs\aar

task ':mylibrary:bundleReleaseAar' property 'archiveFileName'

task ':mylibrary:bundleReleaseAar' property 'archiveBaseName'

map(org.gradle.api.file.Directory task ':mylibrary:bundleReleaseAar' property 'destinationDirectory' org.gradle.api.internal.file.DefaultFilePropertyFactory$PathToDirectoryTransformer@3075dc62)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

可以看到destinationDir就是我们想要的打包文件的路径。 本来以为大功告成,但是倒数第二行Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.又告诉我们使用了弃用功能,与gradle8.0不兼容。 继续在gradle.properties文件中加入org.gradle.warning.mode=all,再次执行assembleRelease 这次在dir:D:\Android\UtilsTest\mylibrary\build\outputs\aar这行前面多了详细说明,表示destinationDir已弃用,应使用destinationDirectory代替:

The AbstractArchiveTask.destinationDir property has been deprecated. This is scheduled to be removed in Gradle 8.0. Please use the destinationDirectory property instead. See https://docs.gradle.org/7.5/dsl/org.gradle.api.tasks.bundling.AbstractArchiveTask.html#org.gradle.api.tasks.bundling.AbstractArchiveTask:destinationDir for more details.

at build_6ehiz20mxn46b9vkyazyyepc1$_run_closure1$_closure7$_closure10$_closure11.doCall(D:\Android\UtilsTest\mylibrary\build.gradle:49)

并给出了文档链接:https://docs.gradle.org/7.5/dsl/org.gradle.api.tasks.bundling.AbstractArchiveTask.html#org.gradle.api.tasks.bundling.AbstractArchiveTask:destinationDir 查看文档并找到destinationDirectory的详细说明的过程就不说了,最后,把脚本改成如下:

//打包完成后的动作

variant.assembleProvider.get().doLast {

File out = new File("${project.rootDir}/maven")

variant.outputs.all {

def provider = variant.packageLibraryProvider.get()

def file = provider.destinationDirectory.getAsFile().get()

copy {

from file

into out

}

}

}

很多时候代码有问题,编程工具虽然给了相应的提示,但是解决问题却不是一两行代码就能搞定的。百度也不是万能的。 记录下这个过程,为以后有类似问题提供思路。

参考文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: