variant.getOutputFile() is deprecated. Call it on one of variant.getOutputs() instead

纵饮孤独 提交于 2019-12-11 11:19:15

问题


This has been coming up since the bump to gradle 2.1 in Android Gradle plugin 0.13.0, but for the life of me I can't understand why logs this warning sometimes.

Consider this block for renaming APKs based on variant type:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def oldFile = output.outputFile
        if (oldFile != null && oldFile.name.endsWith('.apk')) {
            def newFile = "Fancy conditionally-formatted file name here"
            print "\nBefore"
            output.outputFile = new File(oldFile.parent, newFile)
            print "\nAfter"
        }
    }
}

Looking at the gradle logs, I see this:

Before
WARNING [Project: <myproject>] variant.getOutputFile() is deprecated. Call it on one of variant.getOutputs() instead.
WARNING [Project: <myproject>] variant.getProcessResources() is deprecated. Call it on one of variant.getOutputs() instead.
After

Which would seem to suggest that calling the line output.outputFile = new File(oldFile.parent, newFile) throws this warning. Thing is, Google specifically uses this style in their example at the bottom of this page. If we can't touch output at all, how can we set its outputFile?

On top of this, I don't see how getProcessResources() is involved.

Any ideas?


回答1:


Change variant.outputFile to variant.outputs[x].outputFile




回答2:


Instead of

def newFile = "Fancy conditionally-formatted file name here"
output.outputFile = new File(oldFile.parent, newFile)

Do following

def apk = output.outputFile
def newName = getNewName() // any fancy formatted file name
apk.renameTo(new File(apk.parentFile, newName))

Hope it helps.




回答3:


It turned out that the Gradle plugin was still using deprecated calls under the hood. As of Android Gradle Plugin 0.14.4, these warnings no longer appear.



来源:https://stackoverflow.com/questions/26688660/variant-getoutputfile-is-deprecated-call-it-on-one-of-variant-getoutputs-in

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