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.
To install ADB tools, we have two approaches that you can choose from and follow through.
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 adbIf 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-toolsIn 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.zipNote: Be aware that you must install tar and unzip commands before using them on your system.
Before using the adb tool, you should follow these steps on your Android phone to enable working with adb:.
Go ahead through this steps :
- Go to Settings
- Find Build Number and tap it seven times to unhide the developer options menu.
- Go Back to the Settings menus and tap on the developer options
- Enable Develper options, and in the DEBUGGING section, enable USB Debugging
- Connect your phone to your laptop or PC using a
USB cable. - 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 devicesThe 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 shellYou 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 packagesThe 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.
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
exitThis command helps us connect to an Android phone and run shell commands.
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!
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.
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.
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
dope