Gradle fails with error execCommand == null - gradle

I have searched for a while for this problem and am not able to solve it. I pulled a project down from a private git repo. Some people are able to build while others like myself are getting the following error:
Error:Gradle:Execution failed for task ':ProjectName:buildNative'.
> execCommand == null!
If anybody has encountered this and knows how to fix it, please let me know, it does not seem like a problem which is specific to the project I am on.
I think this is the part in the Gradle file where it is failing:
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK_HOME != null) {
def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
commandLine ndkBuild
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}

Seems like you don't have an ANDROID_NDK_HOME environment variable set. The code above doesn't treat that case correctly. As such, the problem is specific to your build. One way to fix it is to replace doLast with doFirst and to insert throw new StopExecutionException() after the printlns. Additionally you may have to set commandLine (or executable) to a dummy value.

Related

If else in Gradle scripts

I have this really strange behavior in Gradle and I cannot find a way out of it. In my gradle.properties file, I am using this checking condition:
//gradle.properties
if ( "${System.Property['DATABASE_DIR']}".compareTo('swdb') == 0 ) {
PROJECT_DATABASE_PATH=../database/swdb/include
}
else {
PROJECT_DATABASE_PATH=../database/include/
}
I created a new task called printProperties and it looks like this.
//build.gradle
task printProperties {
println "${System.properties['DATABASE_DIR']}".compareTo('swdb') == 0
println PROJECT_DATABASE_PATH
}
I get the following output when I run the printProperties task.
$gradle printProperties -DDATABASE_DIR=swdb
true
../database/include/
:printProperties UP-TO-DATE
BUILD SUCCESSFUL
Total time: 1.07 secs
It is really strange that the task prints true but the gradle.properties file does not evaluate the same condition correctly. Could anybody help me with this?
Your code shall take place in a init.gradle script.
You can find documentation here : https://docs.gradle.org/current/userguide/init_scripts.html
gradle.properties file is only for key=value pairs

Gradle variable initialization

I have got a little problem with variables / ext properties in gradle.
In my root project i have this:
task foo {
println project.fooContent
}
in my child project fooContent is defined like this:
ext { fooContent='somethingProjectSpecific' }
When executing :childproject:foo it says variable is not set.
Do you know how to circumvent that problem?
This variable is not set since you try to print it at configuration phase. Try with an action (<<) it will be printed on execution phase:
task foo << {
println project.fooContent
}

Gradle task that depends on a failure

Can a gradle task depend on the failure of another task?
For example, I have an auxillary task that opens the test report in a browser. I want the report to only appear when the task "test" fails, not when all tests pass as it does now.
task viewTestReport(dependsOn: 'test') << {
def testReport = project.testReportDir.toString() + "/index.html"
"powershell ii \"$testReport\"".execute()
}
You can try to set task's finilizedBy property, like:
task taskX << {
throw new GradleException('This task fails!');
}
task taskY << {
if (taskX.state.failure != null) {
//here is what shoud be executed if taskX fails
println 'taskX was failed!'
}
}
taskX.finalizedBy taskY
You can find the explanation gradle's user guide in chapter 14.11 "Finalizer tasks". Shortly, due to docs:
Finalizer tasks will be executed even if the finalized task fails.
So, you just have to check the state of the finilized task with TaskState and if it was failed, do what you wanted.
Update:
Unfortunately, because configuration is always executed for all tasks, seems not possible to create some custom task to set the flag to show report within script. On execution phase it is not possible too, because the task will not be called if previewsly runned task has failed. But you can do, what you wanted, providing the build script arguments, like:
task viewTestReport << {
if (project.hasProperty("showReport") && test.state.failure != null) {
//here is what shoud be executed on taskX fail
println 'test was failed!'
}
}
test.finalizedBy(viewTestReport)
In that case, you have to provide -PshowReport arguments, while you call any gradle task, if ou want to get the report in test task fail. For example, if you call:
gradle test -PshowReport
then report will be shown if test task fails, but if you call it:
gradle test
no report will be shown in any case.

gradle custom properties and println

I'm very new to gradle so I'm trying to make sense of it. I'm trying to print the custom properties using println and it works when I just use the property. But as soon as I put it in a string it fails and I'm not sure what is going on.
gradle.properties looks like this:
version = '2.0'
description = 'Project Description'
project.ext {
winADTSDKManager = 'SDK Manager.exe'
winADTSDKManagerPath = 'C:/Projects/WinSDKEnv/sdk/adt'
}
in build.gradle I try to run the code:
task androidSDKManager << {
description = 'Run Android SDK Manager'
println project.ext.winADTSDKManagerPath
println 'Starting: $project.ext.winADTSDKManagerPath'
}
The output looks like this:
c:\Projects\Prototypes\STouchGradle>gradle androidSDKManager
:androidSDKManager
'C:/Projects/WinSDKEnv/sdk/adt'
Starting: $project.ext.winADTSDKManagerPath
The second println statement is not printing the path like the one above. Any ideas on what is wrong with what im doing?
Update: 7/27/2014
OK I figured it out. I need to use double quotes like this
println "Starting: $project.ext.winADTSDKManagerPath"

Gradle executes all tasks?

I have a very simple build script like so
task hello{
println("hello World")
}
task bye {
println("bye")
}
On the command line I run
gradle hello and I get the following output:
hello World
bye
:hello UP-TO-DATE
Why is it executing the task "bye" (I'm assuming it gets executed since "bye" gets printed)? Thanks.
It's a common pitfall:
task hello {
println("Any code in here is about *configuring* the\
task. By default, all tasks always get configured.")
doLast {
println("Any code in here is about *executing* the task.\
This code only gets run if and when Gradle decides to execute the task.")
}
}
The distinction between configuration phase and execution phase is probably the single most important concept to understand in Gradle. It can be confusing at first, and may go away in the future. A kind of analogue in the Ant/Maven world is that these tools first parse XML build scripts and build an object model (perhaps resolving some properties along the way), and only then execute the build.
Adding to Peter answer, If you want to execute all task , you can specify the defaultTasks list.
defaultTasks 'clean', 'run'
task clean {
doLast {
println 'Default Cleaning!'
}
}
task run {
doLast {
println 'Default Running!'
}
}
task other {
doLast {
println "I'm not a default task!"
}
}
Output
Output of gradle -q
> gradle -q
Default Cleaning!
Default Running!
More details can be found here
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html

Resources