Gradle .execute method showing error after gradle update - android

My project was running perfect with gradle plugin version 3.0.1 after updating to version 3.4.1 I am going through sync failure.
Previously I was using wrapper 4.10.1 which is now updated to 5.1.1.
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
tasks.copyReleaseBuildToXX.execute()
tasks.copyReleaseBuildToXXXX.execute()
}
}
copyReleaseBuild task is written something like below...
task copyReleaseBuild(type: Copy) {
def releaseDir = getProjectProperty('releaseDir')
if (releaseDir?.trim()) {
//if release folder is provided
def releaseAarFile =
getProjectProperty('sourceCodeDir') + "/android-corekit/kit/build/outputs/aar/kit-release.aar"
from releaseAarFile
into releaseDir
}
task copyReleaseBuildToXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapper/libs"
}
task copyReleaseBuildToXXXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapperapp/libs"
}
}
I have been trying to resolve from last two days but nothing is working as I lack knowledge of groovy.
Please check error log I am getting while building the project...

It happens because you are using an updated version of gradle (check the migration to gradle v5)
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
Now you can't call the execute method directly, but you have to use the tasks dependencies to obtain the same result.
Just define in your build.gradle:
task copyReleaseBuild {
dependsOn 'assembleRelease'
//...
}
and remove this:
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
}
}

Upgrading your build from Gradle 4.x to 5.0
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
in your case seems you have dependency between tasks: copyReleaseBuild must run after assembleRelease
so, this simple build.gradle represents this dependency:
task copyReleaseBuild {
dependsOn 'assembleRelease'
doLast {
println 'run copyReleaseBuild'
}
}
task assembleRelease {
doLast {
println 'run assembleRelease'
}
}
and the command gradle copyReleaseBuild
runs both tasks:
# gradle copyReleaseBuild
> Task :assembleRelease
run assembleRelease
> Task :copyReleaseBuild
run copyReleaseBuild
BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed

Related

Gradle Task dependsOn multiple parameters

I have 3 gradle tasks:first,second,third.
task first {
doLast {
println "+++++first+++++"
}
}
task second {
doLast {
println "+++++second+++++"
}
}
task third(dependsOn :[second,first]) {
doLast {
println "+++++third+++++"
}
}
execute 'gradle third' in cmd,the result is
E:\android\GradleTest>gradle third
> Task :app:first
+++++first+++++
> Task :app:second
+++++second+++++
> Task :app:third
+++++third+++++
dependsOn task order is [second,first],But why execute task order is "+++++first+++++" " +++++second+++++"?
When declaring Task Dependencies using dependsOn like that you're not specifying an order; you're just saying third depends on both one and two. Gradle can then look at the dependency graph of all the tasks in the build and decide in which order to run them all.
If you also have a dependency between one and two you should declare it explicitly.
You can do more subtle things with task ordering using mustRunAfter and shouldRunAfter but these are intendend for special cases only, and are rarely what you want. Just focus on the dependencies and let Gradle worry about the ordering.

How to retrieve library version in gradle task

Suppose that the project dependencies are declared as follows:
dependencies {
implementation 'io.mylib:core:1.0.0-SNAPSHOT'
// ....
}
I would like to have a gradle task that will retrieve the mylib dependency and check the version. If the version of the lib is SNAPSHOT then fail the build.
Something like this:
task checkLibVersion(){
version = getDependencyVersion("io.mylib:core") // <-- how to retrieve version
if(version.endsWith("-SNAPSHOT"){
throw new GradleException("Mylib uses snapshot version")
}
}
This task would be part of the release pipeline and will fail the job if current version of mylib is snapshot.
Does anyone know how to retrieve the version of a particular dependency from a gradle task?
OK, it seems this can be done pretty easy if the version number is extracted into an extension property:
In the build.gradle of the root project:
buildscript {
ext.libVersion = '1.3.21-SNAPHOT'
// ....
}
task checkLibVersion(){
def version = "$libVersion"
if (version.endsWith("-SNAPSHOT")){
throw new GradleException("Mylib uses SNAPSHOT version!")
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.dependsOn checkLibVersion()
}
}

After update to Android Studio 2.2 / gradle plugin 2.2.0: "could not get unknown property 'assembleRelease'"

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
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.finalizedBy 'yourRenameTasks'
}
}
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() << { ... }
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)
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.
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'
}
}
you can do this:
task copyApk(dependsOn: "assembleRelease") << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}

Error:Could not find property 'assembleDebug' on project ':app'

I am using 'com.android.tools.build:gradle:2.2.0-alpha6' and Android Studio 2.2 Preview 6. The build runs perfectly fine on Gradle 2.1.0, but to enable instant run it asks me to update Gradle plugin.
On updating Gradle plugin, the build shows "Error:Could not find property 'assembleDebug' on project ':app'". I already tried cleaning .gradle and .idea and reloading the project, but nothing works.
Please help.
find which task is depending on assembleDebug task
changing the following did the trick for me at least:
from:
task findbugs(type: FindBugs, dependsOn: assembleDebug)
to:
task findbugs(type: FindBugs, dependsOn: "assembleDebug")
so just surrounding the task with quotes was enough.
It's from In that case, a workaround is this way:
//assembleDebug.doFirst {
// println '=============assembleDebug============='
//}
//assembleRelease.doFirst {
// println '=============assembleRelease============='
//}
//
// =======>
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleDebug============='
} else if (task.name == 'assembleRelease') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleRelease============='
}
}
If you have no any "assemble" in your project, so check an answer from this post:
Could not get unknown property 'assemble'

Run gradle task of type Copy from gradle code

I have gradle task:
task copyDebugConfigJson(type: Copy) {
outputs.upToDateWhen { false }
from 'some/file/dir/file.json'
into 'some/other/file/dir/'
rename ('file.json', "config.json')
}
And I can invoke it from terminal like that:
./gradlew copyDebugConfigJson
and it works. But I would like to make it happen automatically during build so for example:
android {
buildTypes {
debug {
// somehow make copyDebugConfigJson task start there
}
}
}
Or anytime before app starts running I would like this copy to happen. In general I ask you how can start type:Copy task from gradle blocks.
You can make your task run before compilation:
afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(copyDebugConfigJson)
}
}

Categories

Resources