variant.getOutputFile() is deprecated. Cal

2019-07-31 15:55发布

问题:

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.