50

After updating to AS 1.0 RC 1 and plugin 0.14.4 I am having problems with the renaming part of my build.gradle:

applicationVariants.all { variant ->
            def file = variant.outputFile
            variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
        }

throws now:

Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.

and also I cannot jump to the class ApplicationVariantImpl to look how the property might have been renamed. Anyone knows workarounds for this?

ligi
  • 39,001
  • 44
  • 144
  • 244

6 Answers6

83

try this

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def file = output.outputFile
        output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
    }
}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
Oleg Khalidov
  • 5,108
  • 1
  • 28
  • 29
  • this actually fixed my issue, plus i had to set classpath 'com.android.tools.build:gradle:0.14.2' in the android.gradle file – Noya Dec 05 '14 at 12:24
  • @Khalid Where to put it ? – AZ_ Jan 13 '15 at 09:13
  • Inside the ``android``-block of your script, there is a block ``buildTypes``. Inside that you can have blocks for any build configuration. In my case, the code resides inside ``release``. – Chris Jan 13 '15 at 10:07
  • This is a slightly misleading answer. Before you set the `applicationVariants` make sure have updated all your plugins or removed any you don't really need. In my case removing one misbehaving plugin and updating the others was enough to get the build working. After that adding or removing this block, using the more comprehensive example form @Chris, had no effect one way or the other. We'll see when I try to run the code but the gradle sync had no problems. – Adam van den Hoven Jan 14 '15 at 17:41
  • thanks - i upgraded an app from gradle 1.x to 2.3 and hit this error, so for whatever reason the code posted in the question works in the older gradle version but not 2.3 - this fixed me up. – bsautner Apr 16 '15 at 15:03
21

More comprehensively:

applicationVariants.all { variant ->
    variant.outputs.each  { output ->
        output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
    }
}
Chris
  • 3,192
  • 4
  • 30
  • 43
  • still got error while I'm on android studio 1.2beta, gradle plugin 1.0.1 – fifth Apr 24 '15 at 06:23
  • same as this question, Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl – fifth Apr 28 '15 at 02:12
  • Tricky situation, actually the forementioned solution always worked for me (on more than a dozen projects). However, here's a longshot: You could try and check your dependencies and update them to the most recent version. If your using multiple modules you may have an obsolete script hidden in one of them as well. – Chris Apr 28 '15 at 13:39
6

This can occur for few reasons:

1.) First as was said before by @Khalidov, try

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = ...
    }
}

2.) Second try update all other plugins.

For example I got this problem for Spoon, that resolved by update Spoon up to:

classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.14.1'
Community
  • 1
  • 1
cosic
  • 890
  • 12
  • 20
5

Or where there's only one variant:

def apk = outputs[0].outputFile

Instead of

def apk = variant.outputFile
Ollie C
  • 28,313
  • 34
  • 134
  • 217
1

Make sure you run the latest gradle version (not the plugin, gradle it self).

Check your gradle-wrapper.properties. Are you running gradle 2.1?

More info on compatibility: http://tools.android.com/tech-docs/new-build-system/version-compatibility

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
0

I managed to solve as follows:

old:

buildTypes { 
libertação {

    runProguard false  // esta linha tem que ser mudado

    proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ),  'proguard-rules.pro' 
} 

}

new:

buildTypes { 
libertação {

    minifyEnabled false  // nova versão

    proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ),  'proguard-rules.pro' 
} 

}

edited in file buil.gradle of your project as described in this post by ruan65 Error:(26, 0) Gradle DSL method not found: 'runProguard()'

and after edit too this line:

applicationVariants . all { variant -> 
variant . outputs . each { output -> 
    def file = output . outputFile 
    output . outputFile =  new  File ( file . parent , file . name . replace ( ".apk" ,  "-"  + defaultConfig . versionName +  ".apk" )) 
} 

}

as it was said up there. That settled me!

Community
  • 1
  • 1