Zac Sweers
Zac Sweers

Reputation: 3201

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

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?

Upvotes: 2

Views: 1885

Answers (3)

Rohan Dhamal
Rohan Dhamal

Reputation: 11

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.

Upvotes: 1

MikeL
MikeL

Reputation: 5621

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

Upvotes: 2

Zac Sweers
Zac Sweers

Reputation: 3201

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.

Upvotes: 0

Related Questions