My shell-script is not running on android - android

I am trying to run the following simple shell-script on android:
#!/system/bin/sh
echo "Hello World!"
I named the file "test", and place it in "/system/bin/" .. I change the permission to 755 and the group to shell ..
now when I try to run the script: test, it shows:
sh: test: No such file or directory
I can see the file in there and when I run bash test or sh test it works ..
what is the problem ?

Do not call it “test” because that’s a shell built-in command. Shells will call internal builtins in preference over external utilities.
Rename it to /system/bin/testx and call it as “testx” and see whether that works.
Other common pitfalls on android: 「#!/system/bin/sh」 and most directories are mounted “noexec”. But both of these do not apply to your script if you put it to /system/bin/ anyway.

Related

Enable Xposed modules from shell

Is there an API to enable an Xposed module in Android from the shell (using ADB) and not through the device's UI.
This is constantly a bother for automation, when we need to install our module on a clean test emulator. This is currently the only step that we are required to do manually.
A simple google search + overview of the XPosed documentation didn't yield anything worth while.
As you already know, this is approach disfavored for end-users, but for testing, you have to echo the path of the apk to Xposed config file:
Pre-Lollipop:
adb shell "echo '/data/app/com.xyz.example-1.apk' >> /data/data/de.robv.android.xposed.installer/conf/modules.list"
Lollipop and newer:
adb shell "echo '/data/app/com.xyz.example-1 OR -2/base.apk' >> /data/data/de.robv.android.xposed.installer/conf/modules.list"
For these commands you need to have your emulator support root adb, type
adb root
into the command line. If your emulator doesn't support rooted/insecure adbd, you can also add a su -c before the echo to get root rights.
EDIT: the easiest way to find which number you have to use in directory name would be what #brendan suggested.
This worked for me on KitKat:
(1) Update shared_pres xml file:
If you look at the /data/data/de.robv.android.xposed.installer/shared_prefs/ directory. You will see an enabled_modules.xml file.
In my case I was only working with a single module so I would just overwrite the file. If you have several modules you may wish to do an edit/update.
I would have a local enabled_modules.xml file that looked like this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<map>
<int name="com.companyname.xposedmodule" value="1" />
</map>
...where com.companyname.xposedmodule is the name of your module.
Then post build you can execute a simple:
adb push enabled_modules.xml /data/data/de.robv.android.xposed.installer/shared_prefs/
(2) Update modules.list config file:
You also need to do what #Maxr1998 suggested. I scripted it this way:
adb shell "[ -f /data/app/com.companyname.xposedmodule-1.apk ] && echo '/data/app/com.companyname.xposedmodule-1.apk' >> /data/data/de.robv.android.xposed.installer/conf/modules.list
adb shell "[ -f /data/app/com.companyname.xposedmodule-2.apk ] && echo '/data/app/com.companyname.xposedmodule-2.apk' >> /data/data/de.robv.android.xposed.installer/conf/modules.list

Create empty file with android init script

Is it possible to create empty file in android source code with init rc script like we create folder.
mkdir /tmp creates folder but
touch /tmp/test doesn't do anything.
pls help
the 'write' or 'copy' command works.
but please make ensure the commands are added after the filesystems are mounted, for example, at the end of "on post-fs-data"
on post-fs-data
...
write /data/non-empty-file 0
...
copy /dev/null /data/empty-file

Libgit2 running on Android unable to perform clone operation - returns "failed to set permissions" error

I have just built Libgit2 (v0.20.0) for Android (target SDK version 18, debugging on a rooted device running Cyanogenmod 10.1.2, Android 4.2.2) and a simple function like getting the version number of Libgit2 works fine through the JNI. But when I use the git_clone function it stops right after the objects/info folder is created and returns this error:
Error -1 cloning repository - Failed to set permissions on '/storage/sdcard0/it/ptt/.git/objects/info': Operation not permitted
I have given the application the WRITE_EXTERNAL_STORAGE permission but I guess it still can't chmod unless owner of the file. When I use adb shell to check out the permission mode of the info folder I get:
d---rwxr-x system sdcard_rw 2014-05-15 09:31 info
And by using pwd.h functions I get the username that the c code (that is calling git_clone) is under to be u0_a92. How am I suppose to get pass this I suppose very Android related issue? Is there a simple way to stop Libgit2 from calling p_chmod or can I give it permissions to do so?
I ended up defining p_chmod as a method always returning true to get passed the error. In the bash script I use to build libgit2 I inserted the following lines that leaves the source files in an unmodified condition after building for android:
LIBGIT2_POSIX_PATH="$LIBGIT2_SOURCE_PATH/src/posix.h"
LIBGIT2_POSIX_BACKUP_PATH="$LIBGIT2_SOURCE_PATH/src/posix_original.h"
printf "#include \"always_success.h\"\nint always_success() { return 0; }" > "$LIBGIT2_SOURCE_PATH/src/always_success.c"
printf "int always_success();" > "$LIBGIT2_SOURCE_PATH/src/always_success.h"
cp $LIBGIT2_POSIX_PATH "$LIBGIT2_POSIX_BACKUP_PATH"
sed -i "s/^#define\sp_chmod(p, m).*$/#include \"always_success.h\"\n#define p_chmod(p, m) always_success()\nextern int always_success();\n/" $LIBGIT2_POSIX_PATH
# run the build process with cmake ...
# restore chmod manipulated source header
mv $LIBGIT2_POSIX_BACKUP_PATH $LIBGIT2_POSIX_PATH
There is probably a cleaner way to solve this but at least now I dont get that error anymore. Thanks to Carlos for his help!
UPDATE
Running adb shell mount | grep sdcard I could see that the sdcard which I am trying to clone the repository into uses the vfat file system which according to this forum thread doesn't support unix-style permissions.

How to order test cases for Spoon Automated Testing in Android?

I have added a suite() method to order my tests the way I want them and thus when I run it through Android JUnit they are executed accordingly. But then I noticed that when I use the Spoon execution, the one using cmd, my test cases are executed alphabetically which is the default order.
Why does this happen and how would you counter it without renaming my test cases?
I have the same issue as you; I require a specific order that my test need to be ran in. The app I am testing is too complicated to run in an unpredictable order. My solution was this:
Add this to your build.gradle:
spoon {
if (project.hasProperty('spoonClassName')){
className = project.spoonClassName
}
}
Now, you can execute a specific class with a command like so:
gradle spoon -PspoonClassName=< com.your.pakage.ClassName>
Next, create a file at the root of your Android project: runAllTests.sh
Edit your .sh to look like this:
#!/bin/sh
date +%b-%dT%H.%M > timestamp.out
sites="$HOME"/path/to/project/root
timestamp="$(cat "$sites"/timestamp.out)"
result_folder="$sites"/results
destdir="$result_folder/Results-$timestamp"
mkdir -p "$destdir"
echo "Directory created: ${destdir##*/}"
<---------- Here you start running the test --------------->
echo "Starting Master Setup"
gradle spoon -PspoonClassName=com.espresso.test.MasterSetup
cp -r "$sites"/app/build/spoon "$destdir"/MasterSetup
echo "Results saved to MasterSetup"
echo "Starting WorkoutSchedule"
gradle spoon -PspoonClassName=com.espresso.test.WorkoutSchedule
cp -f "$sites"/app/build/spoon "$destdir"/WorkoutSchedule
echo "Results saved to WorkoutSchedule"
echo "Starting Setting.test"
gradle spoon -PspoonClassName=com.espresso.test.Settings
cp -r "$sites"/app/build/spoon "$destdir"/Settings
echo "Results saved to Settings"
Then, give the script permissions
cd to the script
type chmod u+x runAllTest.sh
You're set. Now just cd to your root, then to execute your test, type . runAllTest.sh.
So, what this does:
First, it creates a timestamp.out. I use this so I can save my results to a file over and over without previous results being overwritten. You do not need this part.
Next, it creates a result folder in the root of your project if it is not already there.
Then, it will make a folder inside the results folder named Results-SOME-DATE.
Lastly, each test will run, saving the results to the normal spot on your project. (Inside build/spoon) Once test are complete it will copy the results to the results folder, and name each test result appropriately so it is easy to see all your tests ran.
NOTE: This script was wrote for MAC. If your on windows or anything else, this script may need modifications.
Additionally: You will find it is inconvenient to open in to each folder to get the index.html opened. So I wrote this script to add to your bash_profile:
function open-results () {
# the browser to open up `index.html' in.
browser='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
# let the user know what directory we're looking in
printf "looking in %s" "$(pwd)"
echo ...
for paths in $(find ./ -name 'debug' -type d); do
for files in $(find "$paths" -name 'index.html'); do
open -a "$browser" "$files"
done
done
echo done
}
Now, cd in terminal to the Results-SOME-DATE, and type open-results. Again, this was written for terminal. You may need to modify depending on your OS. But the structure should be the same
I hope this helps.
The jUnit testing philosophy is that test cases should not depend on each other so order shouldn't be important. That's why you're finding it hard to do. You might want to consider using the "setUp" method to create initial conditions for your test cases rather than having them build on each other.

Android apitrace failed to run

I would like to use the apitrace project on android. I followed the instructions from the readme file.
But get no trace where created.
I run this command
adb shell TRACE_FILE=/data/test.trace LD_PRELOAD=/data/egltrace.so am start -n APP_NAME
How can I make it work?
I tried following the instructions in Dalvik.markdown of the original distribution of apitrace, but without success.
The instructions say to set two properties: wrap._process_name_ and debug.apitrace.procname. The former has to be set, according to those instructions, to LD_PRELOAD=/data/egltrace.so. When launching the application I still wouldn't get any trace generated nor any apitrace-related message in the logcat.
I had more success by putting the LD_PRELOAD instruction in a script and using that as the wrapper. This is the script that I use, called /data/apitrace.sh:
LD_PRELOAD=/data/egltrace.so exec $#
You can also set the TRACE_FILE environment variable to specify the path to which the trace file should be written to. Otherwise it will be _/data/app_process.trace_. For example:
TRACE_FILE=/data/apitraces/mytrace.trace LD_PRELOAD=/data/egltrace.so exec $#
I believe apitrace takes care of adding numbers to the filename to prevent overwriting existing ones. So you'll have mytrace.trace, mytrace.1.trace, and so on.
So with this script in place I set the properties like so:
adb shell setprop wrap._process_name_ /data/apitrace.sh
adb shell setprop debug.apitrace.procname _process_name_
And then I launch the application. I see in the logcat something like the following:
I/dalvikvm( 5980): Exec: /system/bin/sh -c /data/apitrace.sh /system/bin/app_process /system/bin --application '--nice-name=_process_name_' com.android.internal.os.WrapperInit 25 17 'android.app.ActivityThread'
D/apitrace( 5991): apitrace: loaded
D/apitrace( 5991): apitrace[5991]: enabled for _process_name_
D/apitrace( 5991): apitrace: tracing to /data/app_process.trace
I'm using CyanogenMod 10.1.3, which is based on Android 4.2.2.

Categories

Resources