How can I crash/kill a running app - android

I would like to crash my Android app by using command-line ADB tool during my app is running, is it possible to do? and how ?
(Basically, I want to test if my app persist the necessary informations when it is crashed. So, I would like to demo this by crash my app by some command-line tool, like ADB.)
***** Update ********
In other words, how to kill my app process by using ADB tool?

kill probably won't work.
adb shell am force-stop <your.app.package>

adb shell kill <PID>
Try the below command in abd shell.

You can kill your app from the shell:
adb -s YOURDEVICEID shell
top // find your app pid
kill -p YOUR_APP_PID

I can easily kill my app from Eclipse DDMS. Just stop the process , e.g. stop "com.my.app"

You can actually trigger a crash via the "crash" param to adb shell am
adb shell am crash <package name>
This is an obscure option but can be confirmed when emulator is running via: adb shell am help
Details: reference
crash [--user <USER_ID>] <PACKAGE|PID>
Induce a VM crash in the specified package or process
Wish this was easier to find I cannot find the original source where I found it but should be more obviously listed on docs page vs only shown when connected am help menu...
https://developer.android.com/studio/command-line/adb#am

App crashing at runtime is nothing but an Occurance of Exception, The place after which you want to crash your app, write a logic which causes for an Exception.

The best way to crash your app is to add an Exception to your package.
Like division by 0 - add it to your app code.

I don't know if you've got the answer yet. The answer is pretty much the same as #bas but instead of using , you need to you the package name
adb shell am force-stop <package name>
you can get the package name by using this command
adb shell pm list package -3
-3 means all 3rd party apps that are installed in device.

Related

Launching the installed android application in device via terminal

What I have:
I have application installed in device
I have a Mac system and I am using terminal
I am connected to device via terminal
What I am trying to do:
I am trying to launch the installed application via terminal ( I
don't want to reinstall the app and run )
I need to find the app via package name and run it
What I have tried:
admins-MacBook-Pro:platform-tools devrath$ ./adb shell monkey -p com.cnx.dictionary -c android.intent.category.LAUNCHER 1
Error I am getting:
** No activities found to run, monkey aborted.
To find the package, use adb shell pm list packages. There will more than likely be a lot of packages listed. If you know a part of the package name, you can use grep to limit your results. If you were looking for the Facebook package name, you could use adb shell pm list packages | grep facebook and it would only show results with facebook in it.
From there, you need you find the class to start.
adb shell
dumpsys package | grep -Eo "^[[:space:]]+[0-9a-f]+[[:space:]]+com.packagename/[^[:space:]]+" | grep -oE "[^[:space:]]+$" (make sure you change the "com.packagename" part to the name of your package.
Find the class you want to use from the output list. Most apps will have a class of .SplashActivity, .HomeActivity, or .Main but you will have to find the one for your app
When you have that, use am start -n com.packagename/.Class where your package name replaces com.packagename and the class you chose replaces .Class.
One thing to note. In step 1, we used the command adb shell so we are in the of the android device issuing the commands after. If you are issuing the am start command at the main terminal screen, you will have to add adb shell before am start.
I used the zebra site a while back to get this info initially so i left it here for your reference as well.

ADB command to resume android app to foreground without knowing activity

I want to bring a background application to the foreground by its package name without knowing the current activity.
You can achieve this by calling driver.activateApp(package-name); see this for more details
Yes. It's possible to run application specifying package name only using monkey tool by follow this pattern:
adb shell monkey -p your.app.package.name -c android.intent.category.LAUNCHER 1
Command is used to run app using monkey tool which generates random input for application. The last part of command is integer which specify the number of generated random input for app. In this case the number is 1, which in fact is used to launch the app (icon click).
More simplify version of adb command:
android.intent.category.LAUNCHER is default as it is part of Andriod OS. so You can simplify it
adb shell monkey -p your.app.package.name 1
Hope it will help you.

How to close android application completely using adb or appium or any command line+python

I have tried to close android application using adb command and appium server but it is minimizing the app, not closing. I used below command for closing.
adb shell am force-stop com.package.name
AND if i try this command to kill process then it says,
adb shell kill 21860
Error:Operation omitted
Please let me know any way or command line to close android app completely, not run in background.
Thanks in advance!
I don't want to see in the background application list like below image.
adb shell am force-stop <PACKAGE-NAME>
Force stopS everything associated with <PACKAGE-NAME>
should work since honeycomb
Why not just:
un-installing app (it will stop any service or running process of it):
adb -s <udid> uninstall <appPackage>
and then installing it again:
adb -s <udid> install <apkPath>
Note:
In my case it is not working, because my app need to be registered each time re-installed, and working around it with 3rd app (e.g. Appium) is taking too long.

How can I launch and execute Vending.apk on the Android Emulator from adb

I've been trying to install Vending.apk into my emulator w/o success. It says it already exists and fails to replace it when I use adb install -r. The icon does not show up on the screen so I can't tap it to launch the Google Play marketplace.
Therefore, I thought I could run it from my PC (MacOSX) using adb like this:
adb shell am start -a android.intent.action.MAIN -n com.android.vending/.Vending
I constructed the above from examples that work in this article:
How to run (not only install) an android application using .apk file?
And I unzipped the AndroidManifest.xml file using info from this method to see if I could discover the activity name, but no luck:
aapt dump xmltree <apk-file> AndroidManifest.xml
I guess I need to know the exact command to execute the vending apk because I can't seem to find the correct Activity class. adb shell am start keeps giving me error type 3, Activity class does not exist.
Thanks
You can try this:
adb shell am start -n com.android.vending/com.google.android.finsky.activities.MainActivity

Stopping an Android app from console

Is it possible to stop an Android app from the console? Something like:
adb stop com.my.app.package
It would speed up our testing process so much. Right now we uninstall/install the app each time to make sure the manual test cases start with a clean state.
The clean way of stopping the app is:
adb shell am force-stop com.my.app.id
This way you don't have to figure out the process ID.
Edit: Long after I wrote this post and it was accepted as the answer, the am force-stop command was implemented by the Android team, as mentioned in this answer.
Alternatively: Rather than just stopping the app, since you mention wanting a "clean slate" for each test run, you can use adb shell pm clear com.my.app.package, which will stop the app process and clear out all the stored data for that app.
If you're on Linux:
adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell kill
That will only work for devices/emulators where you have root immediately upon running a shell. That can probably be refined slightly to call su beforehand.
Otherwise, you can do (manually, or I suppose scripted):
pc $ adb -d shell
android $ su
android # ps
android # kill <process id from ps output>
First, put the app into the background (press the device's home button)
Then....in a terminal....
adb shell am kill com.your.package
you can use the following from the device console: pm disable com.my.app.package which will kill it. Then use pm enable com.my.app.package so that you can launch it again.
If you have access to the application package, then you can install with the -r option and it will kill the process if it is currently running as a side effect. Like this:
adb -d install -r MyApp.apk ; adb -d shell am start -a android.intent.action.MAIN -n com.MyCompany.MyApp/.MyActivity
The -r option preserves the data currently associated with the app. However, if you want a clean slate like you mention you might not want to use that option.
If you target a non-rooted device and/or have services in you APK that you don't want to stop as well, the other solutions won't work.
To solve this problem, I've resorted to a broadcast message receiver I've added to my activity in order to stop it.
public class TestActivity extends Activity {
private static final String STOP_COMMAND = "com.example.TestActivity.STOP";
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
TestActivity.this.finish();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//other stuff...
registerReceiver(broadcastReceiver, new IntentFilter(STOP_COMMAND));
}
}
That way, you can issue this adb command to stop your activity:
adb shell am broadcast -a com.example.TestActivity.STOP
The "stop" command is implemented as force-stop; stops background app from running. If it's in foreground, it'll stop also: eg.
adb shell am force-stop com.android.providers.telephony
Clearing of packages also deletes their data eg.
adb shell pm clear com.android.providers.telephony
will delete all your sms
Be careful which one you choose.
adb shell killall -9 com.your.package.name
according to MAC "mandatory access control"
you probably have the permission to kill process
which is not started by root
have fun!
If all you are looking for is killing a package
pkill package_name
should work
I tried all answers here on Linux nothing worked for debugging on unrooted device API Level 23,
so i found an Alternative for debugging
From Developer Options -> Apps section -> check Do Not keep activities
that way when ever you put the app in background it gets killed
P.S remember to uncheck it after you finished debugging
In eclipse go to the DDMS perspective and in the devices tab click the process you want to kill under the device you want to kill it on. You then just need to press the stop button and it should kill the process.
I'm not sure how you'd do this from the command line tool but there must be a way. Maybe you do it through the adb shell...
pkill NAMEofAPP
Non rooted marshmallow, termux & terminal emulator.

Categories

Resources