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.
Related
Im going to use 7za library
now i try to use 7za to extract files and add files from 7z
My problem is that every time i execute nothing happen
Here is my 7z.sh code
and also i set permission and its on /data/local/d
#!/system/bin/sh
export LD_PRELOAD=
umask 000
cd "$1"
shift
/data/local/d/7za "$#"
and in terminal i execute using
su
cd /data/local/d/
sh 7z.sh x -tzip d.zip
And i getting error
Incorrect command line and x : no such file directory
Help me thanks for solution
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.
What the question says really - can you issue any commands directly to gradlew via the command line to build, package and deploy to a device?
$ gradle installDebug
This will push the debug build apk to device, but you have to manually start the application.
Since you are using Gradle, you could simple add your own task in build.gradle
task appStart(type: Exec, dependsOn: 'installDebug') {
// linux
commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
// windows
// commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
}
then call it in your project root
$ gradle appStart
Update:
If you are using applicationIdSuffix ".debug", add .debug to the appId only but leave the activity untouched:
'com.example.debug/com.example.MyActivity'
1. Build project, install generated apk to device
# at the root dir of project
$ gradle installDebug
2. Open app on device
$ adb shell am start -n yourpackagename/.activityname
One line sentence:
Build project & Install generated apk & Open app on device
$ ./gradlew installDebug && adb shell am start -n com.example/.activities.MainActivity
There are three commands to accomplish this:
./gradlew assembleDebug #To build the project
adb install -r ./app/build/outputs/apk/app-debug.apk #To install it to the device
adb shell am start -n $PACKAGE/$PACKAGE.$ACTIVITY #To launch the application in the device, where $PACKAGE is the development package and $ACTIVITY is the activity to be launched (the launcher activity).
I've been writing a bash script to do this, with other few features.
A more flexible way to do it is by using monkey:
task runDebug (type: Exec, dependsOn: 'installDebug') {
commandLine android.getAdbExe().toString(), "shell",
"monkey",
"-p", "your.package.name.debugsuffix",
"-c", "android.intent.category.LAUNCHER", "1"
}
Some advantages to this method:
getAdbExe doesn't require adb to be on the path and uses the adb version from the sdk pointed to in local.properties.
The monkey tool allows you to send a launcher intent, so you aren't required to know the name of your activity.
Build -> uninstall old verion -> install new version -> run application.
echo "Build application" && ./gradlew clean build &&
echo "Uninstall application" && adb uninstall [application package] &&
echo "Install application" && adb -d install app/build/outputs/apk/<build type>/[apk name].apk echo "Run application" &&
adb shell am start -n [application package]/.[application name]
Or if you want install and run application in debug type.
./gradlew installDebug && adb shell am start -n [application package]/.[application name]
I wrote this task to be able to install and also open the application on the device. Since I had multiple buildTypes and flavors with different application ids, it was not feasible to hard code the package name. So I wrote it like this instead:
android.applicationVariants.all { variant ->
task "open${variant.name.capitalize()}" {
dependsOn "install${variant.name.capitalize()}"
doLast {
exec {
commandLine "adb shell monkey -p ${variant.applicationId} -c android.intent.category.LAUNCHER 1".split(" ")
}
}
}
}
This would give you open{variant} for every install{variant} task you already have.
task appStart(type: Exec, dependsOn: 'installDebug') {
commandLine android.adbExe, 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
}
I've had 0 exposure to BASH scripting and this is something I would love to learn. I can't figure out how to run a conditional statement based on the output of ant debug on an Android build.
I would like to essentially say something like
if(`ant debug` == SUCCESS) {
// EXECUTE THESE COMMANDS
} else {
// EXECUTE THESE COMMANDS
}
How can I determine if the ant debug has passed or failed in shell script?
SOLUTION
Okay here is what I have:
ant clean
if ant debug; then
echo "success"
else
echo "failure"
fi
I'll give a quick summary for you.
In Bash, conditionals are based around the exit codes of programs. An exit code of 0 is accepted as true, while everything else is accepted as false.
For example, the true program always exits with an exit code of 0, which means that something like this is possible:
if true;
echo "It is true"
fi
Most commands honor this system, but not every program does. The first thing to check is what exit code ant returns on success and failure. You can check the exit code of the previous command with $?.
Here is an example:
$ true
$ echo $?
0
$ false
$ echo $?
1
If ant does honor the exit code system properly, then something like the following should be possible:
if ant debug; then
echo success
else
echo failure
fi
I know nothing about Ant debugging, but there are two approaches to doing what you want to do in Bash. The first is to test output like you've shown:
if test $(ant debug) == 'SUCCESS'; then
# do stuff
else
# do other stuff
fi
You can make your shell script portable to other variants on the Bourne shell by using backticks instead of $(.....) like you wrote in your question, but that starts to become a hassle if your commands later involve nested quotes.
The second way, which is a little more robust, is to test the exit value of the commands instead of their output. This depends on Ant (or whatever) having exit codes that are documented and stable, but it means that if the output of the commands changes, your scripts will continue to work. For example, the POSIX standard says that if a programs succeeds in doing whatever it's supposed to do, it should exit() with a value of zero:
ant debug > /dev/null
ant_exit_code=$?
# other commands can safely go here now that $? is captured
if test $ant_exit_code -eq 0; then
# do stuff
else
# do other stuff
fi
And yes, Bourne shell really does end an if block with "fi". :-)
A quick man ant shows that ant debug invokes Ant with the debug task. Ant's tasks are kinda of user-defined XML scripts, and by default Ant searches a build.xml file in the current directory. You can generate the file with the android tools, however a template is kept in android-sdk/tools/ant and you can view it online (line 1005 defines the debug target).
So ant debug isn't really a command, and should not be put in a script toolchain. However, if you find your output to be stable and feel brave, you can always compare string. This is the definitve guide.
if [ `ant debug` = $SOMETHING ]; then
echo Success
else
echo Failure
fi
I am trying to use python to help do some automation around an incremental build function in the Android build system. Generally, from a given directory, I would execute the following command to build whatever is in that directory and subdirectories:
mm -j8
This is analogous to a "make" command, only it is incremental build and is defined as a function in a bash file called envsetup.sh. What is does it not important, just know that it's a function defined in a bash script somewhere in the file system. To execute this, I can also do:
bash -c ". /path/to/envsetup.sh; mm -j8"
This method of calling it will be important in calling the function from python. I have followed the solution here which shows how to call a function within a bash script from python. I have used this method in a simple script that, in theory, should just spit out the STDOUT and STDERR from executing the command:
import subprocess
command = ['bash', '-c', '. /path/to/envsetup.sh; mm -j8']
(stdout, stderr) = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
print 'stdout: ' + stdout
print 'stderr: ' + stderr
The call to Popen, however, never returns. What am I doing wrong that would allow bash to execute the command properly, but Python hangs when executing the command?
tl; dr:
Your issue is the use of shell=True. Set it to shell=False and it'll work.
With this option set, python will just run the first element of the command array, i.e. bash as a shell script. So currently, python is launching a shell of its own, in order to run your command (bash). It'll run bash with no arguments, and bash will then wait for input, blocking your python script.
The shell=True setting is for use cases where you are passing a shell script in as a single string. When you're explicitly specifying a shell and its parameters as the process to invoke, as you are doing above, you should set shell=False.
>>> import subprocess
>>> subprocess.Popen(['bash', 'whatever'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
Here's what the proces tree looks like when I run the above:
\_ python
\_ /bin/sh -c bash whatever
\_ bash
The whatever is actually passed in, but it's a parameter to the sh, not a parameter to the inner bash, so the command being run is effectively ['/bin/sh', '-c', 'bash', 'whatever'], which is quite different from ['/bin/sh', '-c', 'bash whatever']