Installing apk using adb

What is adb?

Android Debug Bridge (adb) is a command-line tool that lets you communicate with an Android device. It facilitates a variety of device actions, such as installing and debugging apps. There are several other facilities that adb provides.

How adb works?

When you start an adb client it locates the connected devices (emulator or actual device connected through USB). Once the devices are located, you can run the adb commands to control those devices. adb is included in the Android SDK Platform-Tools package (if you’ve Android Studio installed). You can download this package with the SDK Manager, which installs it at android_sdk/platform-tools/. Or if you want the standalone Android SDK Platform-Tools package, you can download it here.

How to run adb commands?

Considering you have Android Studio installed in your Mac, you have two ways to run adb commands -

To be able to run adb commands directly from any directory in the terminal, perform the following steps -

  1. Run emacs ~/.bash_profile command on the terminal. This will open bash_profile.

  2. Add this line to the file  export PATH="$PATH:/Users/user-name/Library/Android/sdk/platform-tools". Make sure to replace user-name with the machines' username (Home directory name)

  3. Save and close the EMac editor with CTRL+X CTRL+C. This will ask you to save any unsaved changes. Type y to save the changes.

  4. Run source ~/.bash_profile to reload the bash_profile.

Here, we’re changing the global PATH variable to the platform_tools path. The system will use this path to run all the adb commands. You can now run any adb command in the terminal.

If you don’t want to set the global PATH variable, you must be in the paltform_tools folder on the terminal to run adb commands. The adb executable is generally found at ~/Library/Android/sdk/platform-tools

From here on, make sure to prefix all the adb commands with ./

For eg: ./adb devices

You may get an error saying adb command not found. The above setup should resolve this issue. If you still face the same problem, refer to this Stack Overflow link and follow the approaches mentioned by others.

How to install an apk on your device/emulator using adb command?

Run adb devices command on the terminal. This command prints the list of devices and emulators that are connected to your system. Here is the sample data printed by this command -

  • G070VM0583750P7T device

  • emulator-5554 device

You get the serial number of the device along with its status (offline/device/no-device). In the above example, G070VM0583750P7T is the serial number of the physical device connected to my system. You can also run adb devices -l to get more info about the connected devices.

Run adb install <apk_path> to install the apk on the connected device. Replace <apk_path> with the local path of your apk. If there are multiple devices connected to the system, run adb -s <serial_number> install <apk_path> to install the apk on the specific device. Replace <serial_number> with the serial number of the device where you want to install the apk.

For more information about adb, checkout the documentation here.