• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Gradle ListingFileRedirectTask 引发的打包问题

武飞扬头像
南柯好萌
帮助1

起因
项目老旧,AS 一直提示升级,故想着折腾一下

项目修改了打包输出的路径,问题由此而起

项目旧的配置
Gradle 6.5
Android Gradle Plugin 4.1.1
项目新配置
Gradle 7.2
Android Gradle Plugin 7.1.2
问题
FAILURE: Build failed with an exception.

  • What went wrong:
    A problem was found with the configuration of task ‘:app:create[XXTAG]ReleaseApkListingFileRedirect’ (type ‘ListingFileRedirectTask’).

    • In plugin ‘com.android.internal.version-check’ type ‘com.android.build.gradle.internal.tasks.ListingFileRedirectTask’ property ‘listingFile’ specifies file ‘[XXPath]/app/build/outputs/apk/[xxTag]/release/output-metadata.json’ which doesn’t exist.

      Reason: An input file was expected to be present but it doesn’t exist.

      Possible solutions:

      1. Make sure the file exists before the task is called.
      2. Make sure that the task which produces the file is declared as an input.

      Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#input_file_does_not_exist for more details about this problem.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

简而言之,就是‘output-metadata.json’这个文件找不到了,由于它是在默认生成路径找的,但是项目的配置里已经修改了生成路径,刻舟求剑,当然找不到了

遇到这个问题后,我先是网上搜了一下,搜到一个方案是把自定义输入路径注释掉就行了,这是一种可行的方法。再加一个 task 把默认路径生成的 apk 文件做一下搬运,也可以达到目的之前修改输入路径的效果。所谓山不过来我过去,目的达到即可。

但是,今天要说的是另一个方案。

顺着错误提示,在‘com.android.tools.build:gradle:7.1.2’里找‘com.android.build.gradle.internal.tasks.ListingFileRedirectTask’,它的注释如下:

org.gradle.api.Task to create a redirect file that contains the location of the IDE model file. The location of the redirect file is never changing and cannot be “replaced” by anyone. The location is passed through the model to the IDE which is expecting to always find the redirect file at the same location independently on where tasks will put APK, Bundle, etc…
For instance, if any other plugin decide to replace the APKs, the APK_IDE_MODEL will be automatically created by the variant API in the new location. The redirect file will not change and will just point to the new location for the model file.
Caching is disabled as the full path to the listing file is used as input. Plus the task execution should be so fast, that it outweighs the benefits in performance.
大意就是生成一个头比较铁的重定向文件,位置还改不了。

这个 Task 是在 TaskManager 中添加的,代码如下:

// create the listing file redirect
taskFactory.register(
ListingFileRedirectTask.CreationAction(
creationConfig = creationConfig,
taskSuffix = “Apk”,
inputArtifactType = InternalArtifactType.APK_IDE_MODEL,
outputArtifactType = InternalArtifactType.APK_IDE_REDIRECT_FILE
)
)
把 task 都打印一下,发现了它: create {productFlavorTAG} ApkReleaseApkListingFileRedirect ,顺序介于 package {productFlavorTAG} 和 install {productFlavorTAG} 之间,那么,这个 task 在开发阶段打 debug 包安装到手机有用,对我们打 release 包而言是可有可无的,那不如就给它禁用掉吧,在 app 的 build.gradle 末尾添加配置如下:

tasks.whenTaskAdded {task ->
    if (task.name.contains("ReleaseApkListingFileRedirect")) { // 过滤release
        task.enabled = false
    }
}

再次打包,就可以正常生成 apk 文件了

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgafjei
系列文章
更多 icon
同类精品
更多 icon
继续加载