Gradle warning: variant.getOutputFile() and variant.setOutputFile() are deprecated

后端 未结 4 1260
栀梦
栀梦 2020-12-01 05:56

I am using the following simplified configuration in an Android application project.

android {
    compileSdkVersi         


        
相关标签:
4条回答
  • 2020-12-01 06:40

    Android Plugin for Gradle 3.0.0

    You can use like this

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

    you can get more on the features and new changes in android documentation https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#update_gradle

    0 讨论(0)
  • 2020-12-01 06:43

    Building on the answer from Larry Schiefer you can change the script to something like this:

    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)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:48

    The complete code snippet looks like that one:

    // 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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:58

    The build variant output API has changed in the latest Android Gradle plugin. It now allows multiple output files (or directories), which is why this method has been marked as deprecated. If you use variant.outputs instead, it will give you a Collection you can then iterate over and get each output file. You'll have to verify the file object is non-null and that it matches your criteria (e.g. has a '.apk' extension.) Then you can create a new File object and add it to the output within the collection.

    0 讨论(0)
提交回复
热议问题