25

After updating Android Studio to version 2.2 and the Gradle-plugin to 2.2.0, I get following error:

"Could not get unknown property 'assembleRelease' for project ':app' of type org.gradle.api.Project."

When I change the plugin version back to 2.1.3 the code still works, but that's no long-term option for me.

My code:

apply plugin: 'com.android.application'

dependencies {
...
}

android {
...
}

...    
assembleRelease.doLast {
  file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}

Hint: project.ext.androidVersionCode is a variable defined otherwhere and contains a build number. The code in assembleRelease.doLast shall just move/rename the generated apk file.

Thank you for advices! tangens

4
  • Could you provide more code?
    – Volodymyr
    Sep 20, 2016 at 12:09
  • I made some changes to make it clearer what I want to do.
    – tangens
    Sep 20, 2016 at 12:35
  • 2
    Take a look at this: stackoverflow.com/a/38569576/2652124 To find the task you can use the Find in Path option, as the answer's comment says. Sep 20, 2016 at 13:22
  • Thank you for this hint, but it does not resolve my problem. But it's helpful, though.
    – tangens
    Sep 20, 2016 at 15:25

6 Answers 6

16
tasks.whenTaskAdded { task ->
  if (task.name == 'assembleRelease') {
    task.finalizedBy 'yourRenameTasks'
  }
}
4
  • Where do you put this - in the app module, or top-level project directory? Oct 17, 2016 at 15:57
  • @IgorGanapolsky app module
    – HalZhang
    Oct 17, 2016 at 15:59
  • It doesn't do anything for me when pressing the green Run button. Oct 17, 2016 at 16:02
  • tasks.whenTaskAdded { task -> if (task.name == 'assembleDebug') { task.dependsOn 'setEnvDebug' } else if (task.name == 'assembleRelease') { task.dependsOn 'setEnvRelease' } } Dec 5, 2016 at 3:40
15

You may rewrite your task a bit and try like this:

task renameBuildTask() << {
  file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
  dependsOn 'assembleRelease'
}

Also you can check this question to get better understanding.

EDIT

As @tangens said in a comment:

It works when I replace the call gradle assemble by e.g. gradle renameBuildTask. Thank you! The answer contains an error. Correct would be: task renameBuildTask() << { ... }

2
  • 1
    It works when I replace the call gradle assemble by e.g. gradle renameBuildTask. Thank you! The answer contains an error. Correct would be: task renameBuildTask() << { ... }
    – tangens
    Sep 20, 2016 at 15:42
  • 1
    Where do you put this, in app's build.gradle file? I am getting an error: No signature of method java.lang.String.dependsOn() is applicable for argument types Oct 17, 2016 at 15:32
7

maybe wrap code in afterEvaluate{} will be work:

afterEvaluate {
    assembleRelease.doLast {
       file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
    }
}

gradle-2.14.1 and android gradle plugin 2.2.0

details: Could not get unknown property 'assembleDebug' (2.2-beta)

5

I had the same problem after upgrading Android Studio to 2.2 and Gradle to 2.2. I have task copyApk that needs to be run at the end of building. For brevity, let me skip what was working before, and post only what is working right now:

tasks.create(name: 'copyApk', type: Copy) {
    from 'build/outputs/apk/myapp-official-release.apk'
    into '.../mobile'
    rename('myapp-official-release.apk', 'myapp.apk')
}

tasks.whenTaskAdded { task ->
    if (task.name == 'assembleRelease') {
        task.dependsOn 'copyApk'
    }
}

Gradle console shows copyApk was run near the end after packageOfficialRelease, assembleOfficialRelease, right before the last task assembleRelease. "Official" is a flavor of the app. I got the workaround from this SO post. I essentially copied the answer here for your convenience. All credits go to the author of that post.

2
  • Which gradle file do you put these tasks into? Oct 17, 2016 at 15:59
  • 1
    In the app's build.gradle.
    – Hong
    Oct 17, 2016 at 18:07
0

inside buildTypes {} method, I put this code : worked like a charm

task setEnvRelease << {
            ant.propertyfile(
                    file: "src/main/assets/build.properties") {
                entry(key: "EO_WS_DEPLOY_ADDR", value: "http://PRODUCTION IP")
            }
        }

        task setEnvDebug << {
            ant.propertyfile(
                    file: "src/main/assets/build.properties") {
                entry(key: "EO_WS_DEPLOY_ADDR", value: "http://DEBUG IP TEST")
            }
        }
tasks.whenTaskAdded { task ->
            if (task.name == 'assembleDebug') {
                task.dependsOn 'setEnvDebug'
            } else if (task.name == 'assembleRelease') {
                task.dependsOn 'setEnvRelease'
            }
        }
0

you can do this:

task copyApk(dependsOn: "assembleRelease") << {
    file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.