3 minute read

0. Installation of ADB in Windows

Before started, we need to install ADB (Android Debug Bridge) on our PC. Visit this link to download and install the version meets your requirement.

It is most likely that you do not need to make any further action to use the adb command in command prompt. But if is not your case, you need to manually add the path of adb.exe file as an entry of system PATH variables. Unless you changed the installation path, adb.exe file resides in below path:

1
C:\Users\<your_account_name>\AppData\Local\Android\sdk\platform-tools

If everything goes well, you can check your adb is well installed by running the version-check command as follows:

1
2
3
C:\users\user>adb version
Android Debug Bridge version 1.0.36
Revision e02fe72a18c3-android

ADB is a versatile tool used during Android application development process, offering various devices to control/log any target device supported.

1. chmod

chmod command is used to set permission level for files and directories. It is abbreviation of change-mode.

adb shell ls -l [file-path] can be used to check permission settings of a file.

1
2
adb shell ls -l a.txt
-rwxrw-r-- 1 system install 2023-12-04 15:40 a.txt

For example, from above output we get to know following:

  • a.txt is file
    • If it were a directory, the very first character must have been d than dash(-) as above.
  • The owner of a.txt is system.
  • Owner of this file has three permissions: read(r), write(w), and execution(x).
  • install group can read and write with a.txt.
  • Other users than system or install only have permission for reading this file.

Consisely, there are three different agents called owner, group, and others. And for each of them can have a combination of three permissions (read, write, execute). Therefore, to fully describe the permission level for given path, there are 9 different permissions to be determined with one additional identifier to tell if the path points a file or a directory. Total 10 characters are hence used to detail permission status for a given path.

If we want to alter permission level of a file/directory, chmod command can be used with all these details. But we do not pass, full string like -rwxrwxrwx with chmod command. Rather, a three-digit-number, each digit of which corresponds to specific combination of permissions, is passed with chmod.

Using chmod - octal code example

Let’s turn to an example:

1
adb shell chmod 420 a.txt

In this example, file permission was newly set with integer 420. Each digit of 420 is resulted by interpreting desired combination of permissions as binary number, in the order of read-write-execute. So, if we are to render read permission only, it corresponds to binary number 100, therefore 4 when written in decimal number. Similarly, 2 in middle digit result from binary expression 010 and to denote that only write permission is given. The meaning of 0 in last digit is trivial, no permission for all levels. Finally, three digits are in order of owner-group-others. So adb shell chmod 420 meant to alter the permission of a.txt as follows:

  • Allow owner to have read/write permission.
  • Allow group to have write permission.
  • Allow other to have no permission.

With these you would understand that chmod 000 will disallow every permission for every agents and chmod 777 will allow every permission for all agents.

Let me show some more examples, and please try to solve a quiz which follows.

1
2
3
adb shell chmod 000 a.txt
adb shell ls -l a.txt
---------- 1 system install 2023-12-04 15:44 a.txt
1
2
3
adb shell chmod 263 a.txt
adb shell ls -l a.txt
--w-rw--wx 1 system install 2023-12-04 15:47 a.txt
1
2
3
adb shell chmod 777 a.txt
adb shell ls -l a.txt
-rwxrwxrwx 1 system install 2023-12-04 15:55 a.txt
Quiz 1

Quiz Text

2. chown

chown command is used to reassign owner/group for some file/directory. It is an abbreviation for change-owner.

Syntax is adb chown [new owner]:[new group] [target filename].

1
2
3
4
5
6
7
adb shell ls -l a.txt
-rwxrw-r-- 1 system install 2023-12-04 15:40 a.txt

(Change owner to bob and group to pop)

adb shell chown bob:pop a.txt
-rwxrw-r-- 1 bob pop 2023-12-04 15:40 a.txt

3. Other Useful adb Commands

Below listed commands are those have been useful through my work. You do not need to remember every single commands. Just get used to what you frequently use and build your own list.

  • Install apk
    1
    
    adb install path_of_apk_file
    
  • Uninstall apk
    1
    
    adb uninstall apk_name
    
  • Push a file to device
    1
    
    adb push filename.apk /target_directory_name
    
  • Pull a file out of device to PC
    1
    
    adb pull target_file_name
    
  • Get device log
    1
    2
    3
    4
    5
    
    adb logcat
    adb logcat -d Real-time update off
    adb logcat *:v Log priority higher than v(verbose)
    Log priorities:
    Verbose < Debug < Info < Warning < Error < Fatal < Silent
    
  • Reboot
    1
    
    adb reboot
    
  • Reboot in recovery mode
    1
    
    adb reboot recovery
    
  • Shut-down: If you add -p option in sending reboot, your device does not restart. It just shuts down.
    1
    
    adb reboot -p
    
  • Start service
    1
    
    adb shell am startservice -n SERVICE_NAME
    
  • Send an intent implicitly: Sending an intent implicitly means that you don’t specify the service to receive the intent you are sending out. -d option can be used to add extra data.
    1
    
    adb shell am broadcast -a android.intent.action.ACTION_NAME -d extra_data
    
  • Send an intent explicitly: In this case you specify the name of target package receiving the intent you broadcast. Target service name is given with -p option.
    1
    
    adb shell am broadcast -a android.intent.action.ACTION_NAME -p target_package_name
    
  • List all installed packages
    1
    
    adb shell pm list packages
    
  • List all installed packages, filtered with specific string
    1
    
    adb shell pm list packages | findstr "some-string"
    
  • Backup your device: This command will create backup.adb file.
    1
    
    adb backup -all
    
  • Restore from backup
    1
    
    adb restore "path_to_backup.adb_file"
    
  • Check PID (Process ID) of specific package
    1
    
    adb shell pidof PACKAGE_NAME
    
  • After securing pid of a process, you can use logcat but restrict the result to lines from the process with that specific pid:
    1
    
    adb logcat --pid=PID-YOU-FOUND
    
  • You can 1) find the PID of a package and 2) get specific logcat for that PID in a single line of command:
    1
    
    adb logcat --pid=$(adb shell pidof -s PACKAGE_NAME)
    
  • Get battery log
    1
    
    adb shell dumpsys battery
    
  • Get battery log (2)
    1
    
    adb shell dumpsys batterystats > PATH_TO_SAVE_DATA