【问题标题】:Gradle warning: variant.getOutputFile() and variant.setOutputFile() are deprecatedGradle 警告:variant.getOutputFile() 和 variant.setOutputFile() 已弃用
【发布时间】:2014-11-17 19:45:24
【问题描述】:

我在一个 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 脚本以消除弃用警告?

【问题讨论】:

    标签: android gradle build.gradle deprecation-warning


    【解决方案1】:

    基于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 实际上,是的,我来这里是为了寻找同一个问题的答案,最后设法让它与上面的 sn-p 一起工作(尽管我里面有一些稍微不同的代码如果)。如果找不到属性“文件”,听起来你写错了,因为我的 sn-p 中没有使用这样的属性:)
    • 令人费解,因为这是违规行:output.outputFile = new File(file.parent, "AppName_playstorebuild.apk")。顺便说一句,我正在使用classpath 'com.android.tools.build:gradle:0.13.0'
    • @Merk 您是否包括了前面的两行?如果没有def file = output.outputFile,“文件”属性显然将不存在。
    • 根据我的测试,output.outputFile = new File(outputFile.parent, fileName) 似乎仍然会导致抛出此警告。开始了一个单独的问题线程here
    【解决方案2】:

    完整的sn-p代码如下:

    // 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)
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      最新的 Android Gradle 插件中的构建变体输出 API 已更改。它现在允许多个输出文件(或目录),这就是此方法被标记为已弃用的原因。如果您改用variant.outputs,它会给您一个Collection,然后您可以迭代并获取每个输出文件。您必须验证文件对象是非空的并且它符合您的条件(例如,具有“.apk”扩展名。)然后您可以创建一个新的 File 对象并将其添加到集合中的输出中。

      【讨论】:

      • variant.outputs.outputFile.name = "NewBuildName.apk"variant.outputs.add(new File("NewBuildName.apk")) 都没有任何效果,所以我不确定你建议采取什么行动。
      • 查看“输出”对象中有哪些属性的文档在哪里?
      • 不幸的是,目前这在很大程度上没有记录。您必须依靠 Android Studio 中的在线帮助(不是很有用),或者在插件本身中挖掘 Groovy/Java 代码以查找详细信息。由于outputsCollection,您应该能够在Groovy/Java Collection 对象上找到文档。
      【解决方案4】:

      Gradle 3.0.0 的 Android 插件

      你可以这样使用

      android.applicationVariants.all { variant ->
          variant.outputs.all {
              outputFileName = "${variant.name}-${variant.versionName}.apk"
          }
      }
      

      您可以在 android 文档中获得更多关于功能和新变化的信息 https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#update_gradle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-06
        • 2015-03-23
        • 2019-07-09
        • 1970-01-01
        • 1970-01-01
        • 2020-07-03
        • 1970-01-01
        相关资源
        最近更新 更多