Using the adb tools with Android. This tutorial describes how to use the adb tool to access your Android device or Android virtual device (AVD).
1. Starting an Android virtual device via the command line
It can be useful to start an emulator directly from the command line.
To start a virtual device from the command line:
emulator -avd avd_name
Current Android Studio has a bug. Workaround is to start the emulator from the $ANDROID_HOME/tools/emulator directory or to define an alias. See https://stackoverflow.com/questions/42554337/cannot-launch-avd-in-emulatorqt-library-not-found
|
For a list of AVD names, enter the following command:
emulator -list-avds
2. Accessing the device via the command line
You can access your Android device via the adb command on the command line. An Android virtual device can be freely accessed, a normal Android phone needs to get rooted for full access. For non-rooted devices, adb still access them, but you only have common user permissions.
The adb tools is located in the [android-sdks]/platform-tools directory. You should add this directory to your path to have direct access to this command.
2.1. Accesing your devices via adb
For rooted devices and emulated devices you need to request root
access.
adb root
The adb allows you to:
-
send commands to your Android device
-
pull and push files to it
-
gives shell access to the Android device
-
allows you to read information from your device, for example the current memory usage.
If you have several devices running you can issue commands to one individual device.
# Lists all devices
adb devices
#Result
List of devices attached
emulator-5554 attached
emulator-5555 attached
# Issue a command to a specific device
adb -s emulator-5554 shell
2.2. Shell access via adb
You can get shell access to your Android device via the following command.
adb shell
This will connect you to your device and give you Linux command line access to the underlying file system, e.g. ls, rm,, cd, mkdir, etc.
3. Using adb
3.1. Starting an activity via the command line
You may have to start adb with root mode before you can use it.
adb root
3.2. Copy files from and to your device
You can copy a file from and to your device via the following commands.
adb shell am start -n yourpackagename/.activityname
3.3. Copy files from and to your device
You can copy a file from and to your device via the following commands.
// assume the gesture file exists on your Android device
adb pull /sdcard/gestures ~/test
// now copy it back
adb push ~/test/gesture /sdcard/gestures2
4. Uninstall an application via adb
You can uninstall an android application via the shell. Switch the data/app directory (cd /data/app) and simply delete your android application.
You can also uninstall an app via adb with the package name.
adb uninstall <packagename>
5. Telnet to your Android device
Alternatively to adb you can also use telnet to connect to the device.
This allows you to simulate certain things, e.g. incoming call, change the network connectivity, set your current geocodes, etc.
Use "telnet localhost 5554" to connect to your simulated device.
To exit the console session, use the quit
or exit
command.
For example to change the power settings of your phone, to receive an sms and to get an incoming call make the following.
# connects to device
telnet localhost 5554
# set the power level
power status full
power status charging
# make a call to the device
gsm call 012041293123
# send a sms to the device
sms send 12345 Will be home soon
# set the geo location
geo fix 48 51
For more information on the emulator console please see Emulator Console manual.
6. Getting system information with command line tool dumpsys
6.1. adb dumpsys
The
adb dumpsys
command allows you to retain information about the Android system and
the running applications.
To get currently memory consumption of an application you can use the following command.
adb shell dumpsys meminfo <package.name>
6.2. Memory consumption overview with dumpsys
The
adb shell procrank
lists you all application in the order of their memory consumption.
This command does not work on real device. Use the
adb shell dumpsys meminfo
instead.
6.3. Information about scheduled tasks
To find which alarms are scheduled for your application, use the
adb shell dumpsys alarm
command and look for your package name. The output might be similar
to the following:
RTC #6: Alarm{434a1234 type 1 com.example}
type=1 whenElapsed=608198149 when=+12m13s122ms window=-1 repeatInterval=0 count=0
operation=PendingIntent{430cf612: PendingIntentRecord{*43bbf887* com.vogella startService}}
This shows the info that the alarm is scheduled for approx. 12 minutes.
To find out the information about the pending intent, run the
adb shell dumpsys activity intents
command
and look
for
the ID of the PendingIntentRecord (in this example
43bbf887):
* PendingIntentRecord{43bbf887 com.vogella startService}
uid=10042 packageName=com.vogella type=startService flags=0x0
requestIntent=act=MY_ACTION cmp=com.vogella/.MyService (has extras)
6.4. Battery information tasks
As of Android 5.0 you can also get information about the battery consumption of an application.
adb shell dumpsys batterystats --charged <package-name>
7. Android adb resources
If you need more assistance we offer Online Training and Onsite training as well as consulting