Gradle warning: variant.getOutputFile() an

2019-01-08 16:52发布

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

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

Now that I updated the Gradle plug-in to v.0.13.0 and Gradle to v.2.1. the following warnings appear:

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. 

How can I rewrite the Groovy script to get rid of the deprecation warnings?

4条回答
太酷不给撩
2楼-- · 2019-01-08 16:56

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

查看更多
小情绪 Triste *
3楼-- · 2019-01-08 17:04

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)
        }
    }
}
查看更多
仙女界的扛把子
4楼-- · 2019-01-08 17:09

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)
            }
        }
    }
}
查看更多
叛逆
5楼-- · 2019-01-08 17:20

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.

查看更多
登录 后发表回答