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

假如想象 提交于 2019-11-27 07:25:30
Thorbear

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!