How to kill application process? - android

In my Xamarin.Android application, I have code that handles restoring state after process death (when the OS kills the application to free memory). I want to test this code but I do not know how to simulate when my application process gets killed. I am trying to do it through the terminal. From my platform-tools folder:
drake#Drakes-MacBook-Pro platform-tools % ./adb shell
generic_x86:/ $ am kill com.mycompany.myapp
But this is not working. And I think it is because it is not targeting the OS in my emulator. I was expecting to see "Pixel_C_API_30" instead of "generic_x86". I am on a Mac.

You can use the adb device command to list the device.
If you have multiple devices, you can use -s to specify the device XXX like this: adb -s XXX shell

Related

Which Android Emulator image do contain 'su'?

How to know which Emulator image will contain su?
I can see that often it only contains su or google play
Run adb root and you get a root shell ... otherwise su needs to be manually installed.
However, when already having a root shell available, installing it isn't much of a problem.
All images are rooted, but SDK apps requesting escalation of privileges do rely upon su.
This question here generally duplicates: How to get root access on Android emulator?
adb shell scripting must:
start the emulator
run adb root
run adb shell
remount system partition
adb push the su binary
chmod to set permissions
exit
Or to answer the question:
start the emulator
and check if the file exists, eg. with adb shell stat /usr/bin/su
Where stat gives this response, when it's not installed:
stat: '/usr/bin/su': No such file or directory
One could even loop all Android images installed in $ANDROID_SDK_HOME...
there's no "one click" solution, but adb can be fully automated with Bash or Batch. And one wouldn't even have to run the emulator, but can mount QCOW2 as a nbd network block device.

How to launch adb shell in Android Studio?

I am using Android Studio 2020.3.1.
I want to launch a adb shell from within Android Studio.
I have the Terminal tab at the very bottom of the IDE.
But I can only open "Local" terminals.
Any ideas where I can launch a "Remote" adb shell?
Within you local terminal, you can easily start an adb shell with the command adb shell
Locate adb if it's not already in your executable paths environment variable. The location largely dependent on the OS you use and where you install the Android SDK. In general it's in the ${ANDROID_SDK}/platform-tools/ directory.
Execute adb devices. This will list the connected adb capable devices. If you are not running any emulators and you only connect your phone then your phone would show up (if not then you may need to treat some permission steps depending on your operating system). Let's say the ID of your device is XYZ.
Execute adb -s XYZ shell and you'll be in a shell on your device.

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.

Running two application/script with a reboot in between without manual Intervention

I have two c application running on android
say helloWorld and helloHell . I need to run both but with a reboot in between.
I have adb pushed the applications to a folder in android file system and created two scripts :
helloworld.sh and helloHell.sh
inside helloworld.sh :
./helloworld
reboot
inside helloHell.sh :
./helloHell
reboot
I have written a shell script on the host PC like this :
hell-world.sh
1->adb shell sh helloWorld.sh
2->some calculated delay
3->adb shell sh helloHell.sh
But the problem with this is after running first script (1->) the command hangs(may be due to reboot inside the helloworld.sh) and I have to manually do CTRL+C , and then manually run the second script(2->) once the reboot is over.
I even tried killing after the first script execution(1->) by doing something like this
PID =$!
kill -9 $PID
but invain,
My aim is to run both the applications with a reboot in between with no user intervention in between.The problem here is after the reboot no new command after that will be executed and have to terminate it manually.
Remove reboot from script file & try this
adb shell sh helloWorld.sh
adb shell reboot
# add some delay here
adb shell sh helloHell.sh

Android ADB stop application command like "force-stop" for non rooted device

I'm trying to stop application on Android 2.3.7 device. But in this version of Android I can't use "force-stop" command. Do you know any other ways to close application on non rooted device?
The first way
Requires root
Use kill:
adb shell ps => Will list all running processes on the device and their process ids
adb shell kill <PID> => Instead of <PID> use process id of your application
The second way
In Eclipse open DDMS perspective.
In Devices view you will find all running processes.
Choose the process and click on Stop.
The third way
It will kill only background process of an application.
adb shell am kill [options] <PACKAGE> => Kill all processes associated with (the app's package name). This command kills only processes that are safe to kill and that will not impact the user experience.
Options are:
--user <USER_ID> | all | current: Specify user whose processes to kill; all users if not specified.
The fourth way
Requires root
adb shell pm disable <PACKAGE> => Disable the given package or component (written as "package/class").
The fifth way
Note that run-as is only supported for apps that are signed with debug keys.
run-as <package-name> kill <pid>
The sixth way
Introduced in Honeycomb
adb shell am force-stop <PACKAGE> => Force stop everything associated with (the app's package name).
P.S.: I know that the sixth method didn't work for you, but I think that it's important to add this method to the list, so everyone will know it.
If you have a rooted device you can use kill command
Connect to your device with adb:
adb shell
Once the session is established, you have to escalade privileges:
su
Then
ps
will list running processes. Note down the PID of the process you want to terminate. Then get rid of it
kill PID
If you want to kill the Sticky Service,the following command NOT WORKING:
adb shell am force-stop <PACKAGE>
adb shell kill <PID>
The following command is WORKING:
adb shell pm disable <PACKAGE>
If you want to restart the app,you must run command below first:
adb shell pm enable <PACKAGE>
To kill from the application, you can do:
android.os.Process.killProcess(android.os.Process.myPid());

Categories

Resources