I have a task that is supposed to run a shell script. In Gradle, I have defined the following:
defaultTasks 'renaming'
... some other stuff goes here ...
task renaming(type: Exec) {
commandLine 'sh', 'src/main/bin/rename.sh'
}
I have the shell script under my module in src/main/bin/
However, it is not getting run (for test purposes, the shell creates a directory called "asfasf"). How can I fix this?
As you give sh a relative path, it is resolved against the working directory of the Exec task. Either set the working directory for the Exec task or use file('src/main/bin/rename.sh') as second argument to commandLine.
Related
I'm trying to add a custom task to my Android gradle build process.
task doSomethingCustom(type: Exec) {
println 'Do Something Custom'
commandLine 'bash', '-c', 'find ./app/res | grep res_name | xargs -I % custom_script_in_path %'
}
[...]
preBuild.dependsOn doSomethingCustom
I would like to run this custom script "custom_script_in_path" that is in the $PATH of all building machines as part of the build process. This works fine from the command line, and I can open the resulting app and see the modified behavior.
However, when I run this within the Android Studio environment, it errors out giving me the following error:
Execution failed for task ':android_app:doSomethingCustom'.
> Process 'command 'bash'' finished with non-zero exit value 127
UPDATE
Verified that the path seen by AndroidStudio is:
path: /usr/bin:/bin:/usr/sbin:/sbin
I also tried:
commandLine 'sh', '-c', 'command...'
To no success.
I'm working on
MacOSX: 10.11.6
AndroidStudio: 2.1.3
How can I modify the $PATH seen by AndroidStudio?
I need that every time before running the tests, run the task with the command for adb
adb shell pm reset-permissions
I tried doing this in my gradle file, it`s compilling, but how does it run?
buildscript {
...
}
android {
...
}
task resetPermission(type: Exec) {
commandLine 'adb', 'shell', 'pm', 'reset-permissions'
}
You'll need to edit your test running configurations, to add the task.
See my (similar) answer here : https://stackoverflow.com/a/35157119/4706693
I'm attempting to add a pre-pre-build shell script to my gradle/Android-Studio build. I've added the following to app/build.gradle:
task prePreBuild << {
commandLine 'ls'
}
preBuild.dependsOn prePreBuild
When I invoke my build with ./gradlew assembleDebug I get the following error:
Could not find method commandLine() for arguments [ls] on project ':app'
If I replace the commandLine line with something like println 'Hello' then it works fine, and I can see the output from my new task.
I searched for other mentions of "Could not find method commandLine" and found nothing. What is the correct way to invoke a shell script from this gradle task?
You need to indicate the type of the task or use the exec block:
task execute(type: Exec) {
}
or
exec {
}
You can find more info on https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
I'm almost certain i am overlooking something.
I have an android gradle project with a build.gradle file. Inside here, I specify the task:
task doSomething(type: Exec) {
println("okay clearly you have got to be getting in here")
commandLine 'sh /Users/dzt/Desktop/create_a_file_on_desktop.sh'
}
and that doesn't run at all. the shell file just literally does:
#!/bin/sh
echo "hi" > /Users/dzt/Desktop/i_am_a_byproduct.txt
and i ran chmod u+x on it so it is executable (i double checked on regular bash shell).
I also tried to use the groovy command:
"cd ../ && sh /Users/dzt/Desktop/create_a_file_on_desktop.sh".execute()
which does not work either. I'm a little stumped. i do NOT see the output file. however, i do see my print statement in the gradle console.
What is going on here?
** EDIT **
okay, i drilled it down more ->
cd ../ does not work at all. why is this? i need to use a relative path, at least relative to this directory
The call must be
commandLine 'sh', '/Users/dzt/Desktop/create_a_file_on_desktop.sh'
or else this is considered one command. But you want to start the sh with the script as param. On the other hand, since you have set the execute-bit, you can as well just call the shell script directly.
See http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html
Running cd like you want with cd ../ && sh script does also not work like this, since && is a shell script command. If you want to run like this, you have to run the shell and make it run as a command. E.g.
commandLine 'sh', '-c', 'cd ~/scripts && sh myscript.sh'
Gradle does not allow cd command for some reason. some commands just do NOT work using groovy.
instead, i used cd inside my shell script. that seems to work just fine.
First, you have to put in the root level gradle.build file. The you need to write it like this, to actually be able to execute the task.
task doSomething << {
group 'yourGroupName'
description 'Runs your bash script'
exec {
workingDir "$projectDir/../pathto/"
commandLine 'bash', '-c', './bashscript.sh'
}
}
Then you can execute with: ./gradlew -q doSomething. In this case I used bash, but you can use any supported scripting shell, like sh, perl, python etc.
In an attempt to compile external jars, I have to use the terminal and do a clean. However, when I go into the root directory of my project and execute
gradlew clean
I get the following message:
-bash: gradlew: command not found
Here's a screenshot of my application folder's home directory.
Let me know if you need anything else, I'm not sure why this is happening.
gradlew is not in your global path. To execute the 'clean' task (or any task for that matter) using the gradle wrapper (gradlew) in your project directory in your terminal, specify the current directory with the './':
./gradlew clean
You need to give it the permission by running this one first:
chmod 777 gradlew
gradlew is not in your global path. To execute the 'rebuild' task (or any task for that matter) using the gradle wrapper (gradlew) in your project directory in your terminal, specify the current directory with the './':
./gradlew rebuild