Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...