• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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?

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)
            }
        }
    }
}
  • 60
Reply Report
    • Have you actually tried running this code? I get Could not find property 'file' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@4b50996. This android gradle plugin is brutal.
      • 2
    • @Merk Actually, yes, I came here looking for an answer to the same question, and finally managed to get it working with the snippet above (although I've got some slightly different code inside the if). Sounds like you've written something wrong if you cannot find the property 'file', since no such property is used in my snippet :)
      • 2
    • Puzzling because this is the offending line: output.outputFile = new File(file.parent, "AppName_playstorebuild.apk"). BTW I am using classpath 'com.android.tools.build:gradle:0.13.0'
      • 1
    • @Merk And did you include the two lines before it? Without def file = output.outputFile the property 'file' will obviously not exist.

The complete code snippet looks like that one:

// Customize generated apk's name with version number
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            def manifestParser = new com.android.builder.core.DefaultManifestParser()
            def fileName = outputFile.name.replace(".apk", "-DEBUG-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}
  • 16
Reply Report

The build variant output API has changed in the latest Android Gradle plugin. It now allows multiple output files (or directories), which is why this method has been marked as deprecated. If you use variant.outputs instead, it will give you a Collection you can then iterate over and get each output file. You'll have to verify the file object is non-null and that it matches your criteria (e.g. has a '.apk' extension.) Then you can create a new File object and add it to the output within the collection.

  • 10
Reply Report
      • 2
    • Unfortunately, at this point in time this is largely undocumented. You have to rely on in-line help within Android Studio (not very useful) or go digging through Groovy/Java code within the plugin itself to find the details. Since outputs is a Collection you should be able to find docs on the Groovy/Java Collection object.
      • 2
    • Neither variant.outputs.outputFile.name = "NewBuildName.apk" nor variant.outputs.add(new File("NewBuildName.apk")) has any effect, so I'm not sure what action you're suggesting.