Skip to content

Instantly share code, notes, and snippets.

@vheidari
Last active October 4, 2023 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vheidari/5cbd82ac9c876c727cb1b8ebe3a3ce5c to your computer and use it in GitHub Desktop.
Save vheidari/5cbd82ac9c876c727cb1b8ebe3a3ce5c to your computer and use it in GitHub Desktop.
If you need to learn how to use ADB this quick guide help you to learn how you can use it.

A quick tour inside your Android smatrphone with ADB

In this quick guide, we will learn what ADB (Android Debug Bridge) is and explore its options through a simple example. it should provid enough to understanding of how it works ;)

If you aren't familiar with it, this tool allows us to explore the inner workings of our Android smartphone, like a hacker (not literally, of course), without requiring root access. it enable various task such as installing and debugging applications, transferring files, and executing shell commands on the device.

How to install and use it ?

To install ADB tools, we have two approaches that you can choose from and follow through.

Install ADB through APT/Manually

apt

To install it on a Linux machine like Ubuntu, you can use this simple command in the Terminal to install it through `apt``:

sudo apt-get install adb

If you don't like installing it through apt, you can install it manually.

manually

If you want to install it manually, use the link below. If the link doesn't work, you can search for "Android SDK Platform-Tools for Linux" on Google:

https://developer.android.com/tools/releases/platform-tools

In the SDK Platform Tools pages, you can search for the Download section and click on this link: Download SDK Platform-Tools for Linux to download it.

Once you have downloaded the appropriate package, you should unzip it. To unzip the file, you can use this command:

unzip platform-tools_r34.0.1-linux.zip
## or use this 
tar -xvf platform-tools_r34.0.1-linux.zip

Note: Be aware that you must install tar and unzip commands before using them on your system.

How to use ADB

Before using the adb tool, you should follow these steps on your Android phone to enable working with adb:.

Go ahead through this steps :

  1. Go to Settings
  2. Find Build Number and tap it seven times to unhide the developer options menu.
  3. Go Back to the Settings menus and tap on the developer options
  4. Enable Develper options, and in the DEBUGGING section, enable USB Debugging
  5. Connect your phone to your laptop or PC using a USB cable.
  6. When you connect the USB cable, you should get a popup where you need to select Media Trandsofrm Protocol (MTP)

If you have completed all the steps mentioned above, your smartphone is now ready to work. You can open a new terminal on your Linux system and type:

adb devices ls
# or 
adb devices

The preceding command shows a list of devices that are connected to your machine through the USB port. To connect to your device through shell, you can use the following command:

adb shell

You can verify that you are connected to your phone, and then you can run commands through the shell. For example, if you want to view a list of packages installed on your phone, you can use the following command

pm list packages

The pm command displays a list of packages that are installed on my smartphone. When I run the above command, the result should be something like the example shown in the box below:

package:com.tv.twitch.android.app-1
package:com.twitter.android-2
package:org.mozilla.firefox-2
...

Through the pm command, we can manage all packages on an Android smartphone, including tasks such as installing, uninstalling, and listing packages.

When using the shell, you can run familiar commands that you are used to in your Linux system, such as ls, cp, and more. To see a list of these commands, I recommend reading the "Issue shell commands" section of the Android Bebug Bridge documentation.

This document explains that many of the shell commands are provided by toybox.

A Usefull example :

Question: Have you ever decided to backup an Android app on your mobile phone, or send its APK file to your friend, or just get its APK file to analyze it on your desktop?

If your answer is yes, let's continue with this guide to learn together how to do it using ADB.

So far, we have learned how to work with ADB shell to get a list of packages on our Android smartphone. Now, we want to learn how we can backup an installed APK file using ADB.

To backup an APK file, we need to connect to the shell and then use the pm, grep, and cp commands. But how can we do this?

Let's take a look at these instructions:

adb shell
pm list packages -f -3 | grep whatsapp
cp /data/app/com.whatsapp.w4b-2/base.apk /sdcard/whatsapp-app.apk
exit

adb shell

This command helps us connect to an Android phone and run shell commands.

pm list packages -f -3 | grep whatsapp

The command pm list packages -f -3 displays a list of all installed apps along with their full paths. The -f -3 switch options help us find the full paths of APK files. Additionally, the grep whatsapp command filters the output stream to show all installed WhatsApp apps on my smartphone.

When we run the command pm list packages -f -3 | grep whatsapp through the shell, we should get something like this:

/data/app/com.whatsapp.w4b-2/base.apk
/data/app/com.whatsapp-2/base.apk 

Additionally, I would like to mention that I have installed two WhatsApp apps on my phone. As a result, you will see two full paths for my WhatsApp in the output!

cp /data/app/com.whatsapp.w4b-2/base.apk /sdcard/whatsapp-app.apk

Okay, now we have found the location of the WhatsApp app APK on our Android phone. To access these APK files, we can use the cp command to make a copy of them. In the above command, we are copying the WhatsApp APK file from /data/app/com.whatsapp.w4b-2 to /sdcard.

Obviously, you can't directly access these apps in the path /data/app/com.whatsapp.w4b-2. For that reason, we are simply making a copy of them to /sdcard.

exit

The exit command closes the shell and returns to the Linux Terminal.

But you might ask the question, where can you find the 'whatsapp-app.apk' file?

If you have a Samsung smartphone, you can use the My Files app. Navigate to 'Internal Storage' and locate the 'whatsapp-app.apk' file.

If you have a Huawei smartphone, the process is similar to a Samsung smartphone. You can use the Files app and navigate to Internal Storage to find the whatsapp-app.apk' file.

Once you have found the APK file, you can send it to anyone through social media apps, email, or download it to your desktop.

Download/Upload APK/TEXT file on our Android/Linux

n the previous section, you learned how to create a backup of an Android app. Now, let's proceed with downloading the 'whatsapp-app.apk' APK file from /sdcard using adb.

To download or upload a file to an Android phone using adb, we can utilize the pull and push commands, respectively.

  • pull - This command is used to download a file from Android to Linux.
  • push - This command is used to upload a file from Linux to Android.

Consider the following example:

mkdir DownloadApps && cd "$_"
adb pull /sdcard/whatsapp-app.apk ./

In the above example, we first create a directory named DownloadApps and navigate to it using the cd "$_" command. Then, we use the adb pull command with the specified options to download the 'whatsapp-app.apk' file to the DownloadApps directory.

To upload a file to an Android phone, you can follow these instructions. In the following example, we use the push command instead of the pull command:

echo "A Sample test !" > uploadMeToAndroid.txt
adb push ./uploadMeToAndroid.txt /sdcard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment