Gradle Warning: Variant.Getoutputfile() and Variant.Setoutputfile() Are Deprecated

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

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

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

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.

What is applicationVariant.outputs?

It should be BaseVariant.getOutputs() and implement at BaseVariantImpl.

The code you check is probably at a very old version that outputs has not be added.

Could not find property 'zipAlignEnabled' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated

Did you try:

if (variant.buildType.zipAlignEnabled) { ...

(Bonus): You also might run into trouble using:

variant.outputFile = ...

if so you might find the following link useful: Gradle warning: variant.getOutputFile() and variant.setOutputFile() are deprecated

Change apk name with Gradle

As CommonsWare wrote in his comment, you should call appendVersionNameVersionCode only for staging variants. You can easily do that, just slightly modify your appendVersionNameVersionCode method, for example:

def appendVersionNameVersionCode(variant, defaultConfig) {
//check if staging variant
if(variant.name == android.buildTypes.staging.name){
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}

def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
}

Gradle Android Plugin: Hook into post-compile task for all product flavors

You can't extend the assemble and build tasks like that because they are replaced with the appropriate build variant related tasks right before Gradle's execution phase. For example: assemble becomes assembleRelease.

You can still hook into the build process when the builds graph is being put together, though. If you want to execute your own code right before the package task, you can use this snippet:

task doStuff << {
// Do stuff
}

tasks.whenTaskAdded { theTask ->
if (theTask.name.contains('package')) {
theTask.dependsOn 'doStuff'
}
}

This code will make no distinction between build variants; you can do that in the if condition, if necessary.



Related Topics



Leave a reply



Submit