Call adb monkey from Java Application - android

I'm making a Java application that I want to call the adb monkey tester for an android package. From the cmd, I was managed to call it but from the Java application, I wasn't. Any ideas? Find my code below.
Cmd command that works:
adb -s 079d35918ec8c84b shell monkey -p com.zte.bamachaye -v 1000
In my Java code, i have the code below, which when I run the process never finish. It stuck there and the app is still on the smartphone
ProcessBuilder pb2 = new ProcessBuilder("adb","-s", "079d35918ec8c84b","shell monkey","-p", packageName, "-v", "500");
Process pc2 = pb2.start();
pc2.waitFor();
Thank you in advance.

Related

run linux bash script on adb shell [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 1 year ago.
I'm trying to run linux shell script on adb shell. It's giving errors!
Here is the whole story:
I wrote a simple bash script hello.sh :
#!/bin/bash
function hello
{
echo "hello world!"
}
hello
running it as ./hello.sh produces the o/p
hello world!
Now I pushed the file to android device using
adb push hello.sh /data/folder_name
then ran following command to enter in adb shell
adb shell
In adb shell fired following commands
cd /data/folder_name
chmod 755 hello.sh
sh hello.sh
This is what I get on adb shell :
# sh hello.sh
sh hello.sh
function: not found
hello world!
hello: not found
#
What's happening here!
Or is there some different way to write function for adb shell script
I searched but didn't get proper solution
Please help.
Not sure about adb, but 'function' is not standard syntax. It is available in many shells, but the standard way to define a function is:
hello() { echo hello world; }
When invoked as sh, bash enters posix mode and it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.
The reserved word function is optional for bash, but I think is unknown to historical versions of sh.
Try to invoke the command as
bash /tmp/test.sh
You don't need to push the script to your phone - simply expand it in the shell itself like so and you save yourself time:
adb shell "$hello.sh"

Getting 'root" permission for Android App

I would like to know how can we get root permission from android app? Are there any app out there in android market?
I tried out the below line of code to list out files but nothing happened
Process process = Runtime.getRuntime().exec(new String[] { "su", "-", "root"});
I tried to give TEST_FACTORY permission in my manifest file but I got an error "permitted to system app"
How can I make my app system app?
I want help to get started with these stuff (make app if possible to get root permission) any help on this is very much appreciated. Thanks in advance :)
Theres a good answer here - ANDROID: How to gain root access in an Android application?
"As far as I know, you can only run command-line commands using root privileges. You can use this generic class I made that wraps the root access in your code: http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html"
First: note that you can only execute shell commands using su (= you can only use shell commands as root, not java code).
Second: Not sure if this applies to all su apps out there, but this is the help message of su on my phone:
Usage: su [options] [--] [-] [LOGIN] [--] [args...]
Options:
--daemon start the su daemon agent
-c, --command COMMAND pass COMMAND to the invoked shell
-h, --help display this help message and exit
-, -l, --login pretend the shell to be a login shell
-m, -p,
--preserve-environment do not change environment variables
-s, --shell SHELL use SHELL instead of the default /system/bin/sh
-u display the multiuser mode and exit
-v, --version display version number and exit
-V display version code and exit,
this is used almost exclusively by Superuser.apk
This means: you have to run su -c something (or su -c something - root, but rootis the default anyway). essentially this is equal to su on most Linux systems, except the daemon-thing, as there is no daemon ahndling su calls on regular linux systems.
If other su commands behave differently (which is possible), it's more secure to open a stream to a shell, execute su, evaluate it's return code, then proceed to execute other commands, finally execute exit.

Is it possible to call Monkey tests from MonkeyRunner Script

At some point in my Monkeyrunner I want to launch random Monkey tests (The ones we can get through a command adb shell monkey -p my.package -v 500), So I added this line device.shell('monkey -p my.package -v 500') to my python script. But nothing happens, Any Ideas?
Your command should start the monkey tests. However, you need to pause the script execution during there random monkey tests by adding MonkeyRunner.sleep(no_of_seconds) statements. This will give time for the random tests to complete.
Yes normally it should start it, but it didn't.
Alternatively I imported os import os,
then I called monkey through this command os.system('adb shell monkey -p mypackage -v 500').
Sadly it relaunches the my application. Not the best outcome.

Executing a Root access based commands on Android application

I basically want to start a process which resides in /system/bin/... from the Application java command.
I tried all types of Runtime.process.exec() options
Tried the su and ouputStream combination as well, but nothing is able to start the application.
I am using the code in device and emulators, no luck in both.
I am able to run the same commands from ADB Shell successfully (as it has root permissions).
The device is rooted, I am able to see the # when I use adb to debug my device and it also can go into su via adb.
based on my searching I found that (after doing ps from adb shell)
I am able to run any command with lrwz--x--x permission such as (ls->toolbox, ps->toolbox) from the application layer but I am not able to execute any other commands which are not linked to the toolbox.
This doesn't execute ls:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os=new DataOutputStream(p.getOutputStream());
os.writeBytes("ls \n");
os.flush();
But this does execute ls:
Process p = Runtime.getRuntime().exec("ls");
I would really appreciate if I can get any help on this here! I am posting this after doing lots of research.
Rather than sending ls \n to the su command's standard input, try running:
su -c ls
The -c option to su(1) asks it to run a command with elevated privileges rather than starting an interactive shell.
Try creating a shell script file(eg: init.sh) and write following commands in it:
su
reboot
Now try executing this shell script by
Runtime.getRuntime().exec("sh init.sh");
If this restarted your droid then you can write your required code in init.sh file & run it.

How do I stop the monkey madness?

I'm using the monkey tool to run a test of my Android application. For example, I might do a run like the following:
adb shell monkey -p com.myapp -v 10000
However, if I change my mind and need to cancel the test, there doesn't seem to be a way to do so that doesn't involve waiting multiple minutes for the damned monkey to finish most or all of its run.
Killing the adb shell process on my mac doesn't solve the problem. Killing the com.myapp process on my phone using ddms doesn't work. Unplugging my phone doesn't work.
How do I cancel the monkey madness?
You can kill the monkey process just doing this:
$ adb shell ps | awk '/com\.android\.commands\.monkey/ { system("adb shell kill " $2) }'
[Nitpick] You're confusing monkeyrunner with monkey.
The monkeyrunner tool is not related to the UI/Application Exerciser
Monkey, also known as the monkey tool. The monkey tool runs in an adb
shell directly on the device or emulator and generates pseudo-random
streams of user and system events. In comparison, the monkeyrunner
tool controls devices and emulators from a workstation by sending
specific commands and events from an API.
[/Nitpick]
On my Android 2.2 device when I start monkey, I see a process started in DDMS by the name "?" (just a question mark). When I killed that process, the monkey madness stopped.
adb shell
ps | grep monkey
kill process_id
adb shell kill $(adb shell pgrep monkey)
kudo to #deadfish
For what it's worth, I use Android Studio 3.1.4 on a Mac in 2018 and I had to alter the accepted answer like so:
./adb shell ps | awk '/com\.android\.commands\.monkey/ { system("./adb shell kill " $2) }'
Hope that help prevent some hair-pulling and pencil snapping out there!
Also... when it comes to the monkey, always be sure to pin your app!!! Otherwise you might accidentally send all your selfies to a random email in China like I did. ¯\_(ツ)_/¯
Kill the monkey by shell will cause a small problem, the IActivityController in ActivityTaskManagerService will not be set to null, which it should. And the ActivityManager.isUserAMonkey() still return true.
If monkey stop automatically, it will reset the Controller properly:
Monkey.java{
private int run(String[] args) {
...
try {
mAm.setActivityController(null, true);
mNetworkMonitor.unregister(mAm);
}
...
}
}

Categories

Resources