Gradle警告: variant.getOutputFile()和variant.setOutputFile()已被弃用。

40

我在一个 Android 应用程序项目中使用以下简化的配置。

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 20
        versionCode 1
        versionName "1.0.0"

        applicationVariants.all { variant ->
            def file = variant.outputFile
            def fileName = file.name.replace(".apk", "-" + versionName + ".apk")
            variant.outputFile = new File(file.parent, fileName)
        }
    }    
}

现在我将Gradle插件更新到v.0.13.0和Gradle到v.2.1,出现以下警告:

WARNING [Project: :MyApp] variant.getOutputFile() is deprecated. 
    Call it on one of variant.getOutputs() instead.
WARNING [Project: :MyApp] variant.setOutputFile() is deprecated. 
    Call it on one of variant.getOutputs() instead.
WARNING [Project: :MyApp] variant.getOutputFile() is deprecated. 
    Call it on one of variant.getOutputs() instead.
WARNING [Project: :MyApp] variant.setOutputFile() is deprecated. 
    Call it on one of variant.getOutputs() instead. 

我该如何重写Groovy脚本以消除弃用警告?

4个回答

64

Larry Schiefer 的回答基础上,你可以将脚本更改为以下内容:

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = outputFile.name.replace('.apk', "-${versionName}.apk")
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }
}

你有试过运行这段代码吗?我得到了Could not find property 'file' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@4b50996. 这个Android Gradle插件真是太难用了。 - Merk
@Merk 实际上,是的,我来这里寻找同样的问题的答案,最终通过上面的代码片段成功解决了(尽管在if语句内部我有一些稍微不同的代码)。 听起来如果你找不到'file'属性,那么你可能写错了什么,因为在我的代码片段中没有使用这样的属性 :) - Thorbear
@Merk 你有包含它之前的两行代码吗?如果没有 def file = output.outputFile,那么属性 'file' 显然是不存在的。 - Thorbear
我遇到了一个错误。Apk的名称和Android Studio在本地路径中寻找的名称不同。我在这里发布了类似的内容:http://stackoverflow.com/questions/30082087/gradle-autoincrement-and-rename-apk-i-o-error - hadez30
1
我在M1 MBP上遇到了这个错误:java.lang.RuntimeException: com.android.build.gradle.internal.crash.ExternalApiUsageException: groovy.lang.GroovyRuntimeException: Cannot set the value of read-only property 'outputFile'。目标API为30,Gradle插件版本为4.2.2,Gradle版本为6.9。 - Olcay Ertaş
显示剩余2条评论

16

完整的代码片段看起来像这样:

// Customize generated apk's name with version number
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            def manifestParser = new com.android.builder.core.DefaultManifestParser()
            def fileName = outputFile.name.replace(".apk", "-DEBUG-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}

11
构建变体输出 API 在最新的 Android Gradle 插件中发生了变化。它现在允许多个输出文件(或目录),这就是为什么这种方法被标记为已弃用的原因。如果您改用 variant.outputs,它将提供一个Collection,您可以遍历并获取每个输出文件。您需要验证文件对象不为空,并且符合您的条件(例如具有 '.apk' 扩展名)。然后,您可以创建一个新的 File 对象并将其添加到集合中的输出中。

"variant.outputs.outputFile.name = "NewBuildName.apk"" 和 "variant.outputs.add(new File("NewBuildName.apk"))" 都没有任何效果,所以我不确定你建议的是什么操作。 - Merk
6
请问在 "output" 对象中有哪些属性可以查看相应的文档? - whizzle
4
很不幸,在目前这个阶段,这方面的文档大多未被记录。你必须依靠Android Studio内部帮助(不是很有用),或者深入挖掘插件中的Groovy/Java代码来查找详细信息。由于outputs是一个Collection,因此您应该能够在Groovy/Java Collection对象的文档中找到相关说明。 - Larry Schiefer

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接