Android/adb

From Gentoo Wiki
Jump to: navigation, search

This WIKI has been created for non-rooted devices, for rooted devices please create another wiki OR add a rooted section at bottom

About ADB

ADB stands for 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 & Fastboot

Fastboot will get installed under same package as adb

Install dev-util/android-tools:

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

Enable USB debugging

Enable the USB Debugging option under Settings > Developer options.

For Android 4.2 and newer, 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 your device is listed then you can connect to your android devices shell (first time you must accept authorized request on your device when you typing adb shell)

user $adb devices
List of devices attached 
8NH7N17B0XX9898        device

If you want to run adb without root privileges then you must add your user in to plugdev group:

user $gpasswd -a <username> plugdev

Enter shell

user $adb shell

Multiple devices connected

If you have multiple devices connected to your PC then you must use -s for specify which device otherwise you will see a message similar toː

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

If you prefer to use adb over a WiFi connection instead of using the cable you can type below command with the USB plugged in first timeː

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

Print IP address

To get your IP of your connected android device you can typeː

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

Connect

Now you should be able to connect to your deviceː

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

Now control your device as usual via adb.

Warning
It is not recommended to flash your device over adb via WiFi or similar things that can harm your device if connection will be lost.

Control daemon

Start ADB daemon

user $adb start-server

Stop ADB daemon

user $adb stop-server

Kill ADB deamon

user $adb kill-server

Sometimes it might be necessary to kill adb if you your device will not be shown after you have connected the device if it's already running before you connecting the device, then just start the daemon again after you killed adb daemon.

Reboot

System reboot

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 myfolder will be transferred into storage/on/device Notice the trial slash

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 trial slash

user $adb push myfolder /storage/on/device

ADB properties

Print 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

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

Tips For uninstall several packages at once you must do it in 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 your connected device, simply run adb shell dumpsys. However, this outputs far more information than you would typically want. For more manageable output, specify the service you want 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 $adb shell dumpsys -l

If above command does not work then you can useː

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

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