< Android

Android/adb

Note
This wiki article has been created for non-rooted devices. For rooted devices please create another article or add a rooted section at bottom.

ADB stands for Article description::Android Debug Bridge[1], and it is a part of the Android Software Development Kit (SDK)[2]. It can be installed with dev-util/android-sdk-update-manager.

root #emerge --ask dev-util/android-sdk-update-manager

Installation

Install ADB and Fastboot

Fastboot is included with ADB inside the dev-util/android-tools package::

root #emerge --ask dev-util/android-tools

Enable USB debugging

Enable the USB Debugging option on the Android device under Settings > Developer options.

For Android 4.2 and later, Developer options is hidden by default; use the following stepsː

  1. On the device, go to Settings > About <device>.
  2. Tap the Build number seven times to make Settings > Developer options available enable.
  3. Go back to system settings and scroll down to bottom > Developer Options.
  4. Now hit Enable USB-Debugging.

Tips: You might also want to enable the Stay awake option, to prevent Device from sleeping while plugged into the USB port.

Detect devices

If the device is listed then connect to the android devices shell (the first time an authorized request on the device must be accepted when typing adb shell):

user $adb devices
List of devices attached 
8NH7N17B0XX9898        device

To run adb without root privileges then the unprivileged user account must be added to the plugdev group:

root #gpasswd -a <username> plugdev

Enter shell

user $adb shell

Multiple devices connected

When multiple devices are connected to the PC then the -s option will be needed to specify which device. If the device has not been specified, an error message message similar to the following will appearː

error: more than one device/emulator
user $adb devices
List of devices attached
9QZ7N11B0ZX8999        device
8NH7N17B0XX9898        device
user $adb -s <device̠-number> shell

Connect to ADB via WiFi

Set TCP port

To use adb over a WiFi connection instead of using the cable, type the below command with the USB plugged in first timeː

user $adb tcpip 5555
restarting in TCP mode port: 5555

To get the IP of the connected Android device (cabled connection):

user $adb shell ip route | awk '{print $9}'
192.168.1.51 

Connect

Once the IP address is known, use it to connect to the device:

user $adb connect 192.168.1.51ː5555
connected to 192.168.1.80:5555

Now control the device as usual via adb.

Warning
It is highly not recommended to flash, or perform any similar action to that can brick the device if the connection is lost, when connecting over WiFi.

Control daemon

Start ADB daemon

user $adb start-server

Kill ADB daemon

Sometimes it may be necessary to kill adb if the device is not showing up connect. This can occur when adb is running before connecting the device. In this case kill and restart the adb server:

user $adb kill-server

Reboot

System reboot

The device can be rebooted from adb:

user $adb reboot

Recovery mode

user $adb reboot recovery

Bootloader mode

user $adb reboot bootloader

File transfer

Push a file

user $adb push mypicture.png /storage/on/device

Push a folder

user $adb push myfolder /storage/on/device

Push all files in a folder

Files from the myfolder/ directory will be transferred into storage/on/device. Notice the trailing slash on the directory name is specifiied in the command:

user $adb push myfolder/ /storage/on/device

Pull a file

user $adb pull /storage/on/device/mypicture.png

Pull a folder

user $adb pull /storage/on/device /home/̩$(whoami)/android-folder/

Pull all files in a folder

Notice the trailing slash after the myfolder/ directory name:

user $adb push myfolder/ /storage/on/device

ADB properties

user $adb shell getprop

Set a property service

user $adb shell setprop key value

ADB service

Service syntax

user $adb shell service call <your_service_name> <number at which the function appears in your_service_name.aidl> <type of the argument like i32 or i64> <argument>

List all services

user $adb shell service list
iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
phone: [com.android.internal.telephony]

Exampleː Make a call via service

user $adb shell service call phone 1 s16 '+6512345678'

Exampleː Print IMEI via a call service

user $adb shell service call iphonesubinfo 1
Result: Parcel(
  0x00000000: 00000000 0000000f 00360038 00340034 '........8.6.2.0.'
  0x00000010: 00330036 00330030 00300035 00350032 '6.3.0.3.5.1.2.5.'
  0x00000020: 00370038 00000038                   '8.7.8...        ')

Convert the output to a readable formatː

user $adb shell service call iphonesubinfo 1| cut -d "'" -f2| grep -Eo '[0-9]'| xargs| sed 's/\ //g'
862063035125878

Imei 1:

user $service call iphonesubinfo 3 i32 1 | grep -oE '[0-9a-f]{8} ' | while read hex; do echo -ne "\u${hex:4:4}\u${hex:0:4}"; done; echo
862063035125878

Imei 2:

user $service call iphonesubinfo 3 i32 2 | grep -oE '[0-9a-f]{8} ' | while read hex; do echo -ne "\u${hex:4:4}\u${hex:0:4}"; done; echo
862063035125880

ADB package manager

List installed packages

user $adb shell pm list packages
package:com.android.email 
package:com.android.phone
package:com.android.shell
package:com.android.wallpaperbackup
.......

List enabled packages

user $adb shell pm list packages -e

List disabled packages

user $adb shell pm list packages -d

List third party packages installed by user

user $adb shell pm list packages -3

List users

user $adb shell pm list users
Users:
       UserInfo{0:Owner:13} running

List permission groups

user $adb shell pm list permission-groups
permission group:com.google.android.gms.permission.CAR_INFORMATION 
permission group:android.permission-group.LOCATION
permission group:android.permission-group.STORAGE
permission group:android.permission-group.MICROPHONE
.......

List features

user $adb shell pm list features
feature:android.hardware.camera
feature:android.hardware.camera.autofocus
feature:android.hardware.faketouch
feature:android.hardware.fingerprint
feature:android.hardware.nfc
feature:android.software.vr.mode
.......

Uninstall a package

user $pm uninstall --user 0 package.name

Exampleː

user $pm uninstall --user 0 com.facebook.orca
Success

Tip: Uninstalling several packages at once can be achieved by a loop:

user $for packages in com.package1 com.package2; do adb shell pm uninstall --user 0 $packages; done

Dumpsys

A tool that runs on Android devices and provides information about system services. To get a diagnostic output for all system services for the connected device, simply run adb shell dumpsys. However, this outputs far more information than typically needed. For more manageable output, specify the service to examine by including it in the command.

Dumpsys syntax

user $adb shell dumpsys [-t timeout] [--help] [-l] [--skip services] [service] [arguments] [-c] [-h]

Exampleː Print battery stats

user $adb shell dumpsys battery
  AC powered: false
  USB powered: true
  Wireless powered: false
  Max charging current: 500000
  Max charging voltage: 5000000
  Charge counter: 0
  status: 2
  health: 2
  present: true
  level: 45
  scale: 100
  voltage: 3826
  temperature: 240
  technology: Li-poly

List options

user $dumpsys -l

If above command does not work then useː

user $dumpsys | grep -a 'DUMP OF SERVICE'

Tips and tricks

Show network speed at top beside battery icon

user $settings put system show_network_speed_enabled 1

This is a good command to figure out how to start the application via am:

user $dumpsys window windows | grep 'mCurrentFocus'

Open any URL with the system's default browser

The following command opens this article with the default browser on the phone:

user $am start -a android.intent.action.VIEW -d https://wiki.gentoo.org/wiki/Android/adb

Enter a number to phone application without pressing on call

user $service call phone 1 s16 "+4612345678"

See example below:

user $pm list packages| sed -e "s/package://"| while read x;do cmd package resolve-activity --brief $x| tail -n 1M;done
Example Output:
com.skype.raider/.Main
com.google.android.youtube/.app.honeycomb.Shell$HomeActivity
com.huawei.camera/com.huawei.camera
com.microsoft.appmanager/.StartUpCoreActivity
com.android.mediacenter/.PageActivity

Now you can run any of above lines with am

user $am com.android.mediacenter/.PageActivity

Take a photo without open camera application

user $am start -a android.media.action.IMAGE_CAPTURE

Take a photo by open camera application

user $am start -a android.media.action.IMAGE_CAPTURE"

For take the photo when the photo app is running enter

user $input keyevent 27

Debug an application by simulating 10000 touches

user $monkey -p com.example.myapp -v 10000

Allow GPS to trace your position

user $settings put secure location_providers_allowed gps

Disallow GPS tracing

user $settings put secure location_providers_allowed gps ' '

Allow installing applications outside play store

user $settings put secure install_non_market_apps 1

Extract a backup (.ab) file on PC

user $( printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" ; tail -c +25 backup.ab ) | tar xfvz -

Allow taking photo via fingerprint

user $settings put secure fp_take_photo 0

Open power settings

user $am start -a com.android.settings/.Settings\$PowerUsageSummaryActivity

Add a contact via am

user $am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name 'wuseman puzeman' -e phone 123456789

Open contacts application

user $am start -a android.intent.action.VIEW content://contacts/people/

Allow notifications to be viewed at home screen when its locked

user $settings put secure lock_screen_allow_private_notifications 1

Enable owners message at lock screen

user $settings put secure lock_screen_owner_info_enabled 0

Swipe down notification center via input

user $input swipe 0 0 0 300

Open developer settings

user $am start -a com.android.settings.APPLICATION_DEVELOPMENT_SETTINGS

Simulate pressing home button

user $am start -W -c android.intent.category.HOME -a android.intent.action.MAIN

Get bluetooth mac-addr

user $settings get secure bluetooth_address

Enable accessibility

user $content insert --uri content://settings/secure --bind name:s:accessibility_enabled --bind value:s:1

Enable on accessibility injection accessibility

user $content insert --uri content://settings/secure --bind name:s:accessibility_script_injection, --bind value:s:1

Enable lock feature

user $content insert --uri content://settings/secure --bind name:s:lock_function_val --bind value:s:0

Allow us to use volume buttons

user $content insert --uri content://settings/secure --bind name:s:volume_controller_service_component --bind value:s:1

Disable lockscreen, no reason for keep this 1 while hacking

user $adb shell content insert --uri content://settings/secure --bind name:s:lockscreen.disabled --bind value:s:0

Enable max brightness (Default: 72)

user $content insert --uri content://settings/secure --bind name:s:brightness_pms_marker_screen --bind value:s:255

HTC backup

With a non rooted device, the only things you can backup locally is what the couple device/android will let you do. That is pretty much the same files you can copy with the already mentioned software. With the Android 4.x devices, a nice solution to make such a partial backup is the so-called "adb backup".

user $adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|nosystem] [<packages...>]

where:

  • -f : the path of the *.ab file that will be saved on your computer. This file is a compressed file that contains an archive of the data/apks from your device.
  • -apk|-noapk : indicates if the *.apk files should be backed up (default is -noapk)
  • -shared|-noshared: enable/disable backup of the device's shared storage / SD card contents (default is -noshared)
  • -all : indicates that you want the entire system backed up. you can use the packages filter to just backup specific packages, or use -all for a full system backup.
  • -system|-nosystem: indicates if all the system applications and data are included when backing up. (default is -system)
  • <packages> : this is where you can list specific packages to backup. Use these if you want to back up only specific applications. If using -all, you do not need to specify packages.

To backup the phone into ~/HTC_backup

user $cd ~
user $mkdir HTC_backup
user $adb devices

will start the daemon and show you the devices on the USB.

user $adb backup -apk -shared -all -system -f ~/HTC_backup/backup<date_of_the_day>.ab

will backup every thing the device will let you to backup.

user $adb restore ~/HTC_backup/backup<date_of_the_day>.ab

will restore the backup into the device.

To stop the daemon:

user $adb kill-server

References

This article is issued from Gentoo. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.