头图

After you download Android Studio and install the SDK, if you want to perform operations such as opening Firebase debug mode , the relevant tutorial may tell you to enter " adb " in the terminal. If you do so, the following may appear:

command not found: adb (找不到命令: adb)

or:

adb : The term 'adb' is not recognized as the name of a cmdlet, function, script file, or operable program.
(adb: 无法将“adb” 项识别为 cmdlet、函数、脚本文件或可运行程序的名称)

If you subconsciously search for "how to install adb", you may find some instructions for installing through the package management tool of the operating system or downloading some compressed files from the mobile customization website. But these are not necessary! adb will be installed when you use Android Studio to install the Android SDK, and as long as you go through some manual steps, you can ensure that you always use the same latest tools as the IDE!

What is

adb stands for "Android Debug Bridge", it is a multifunctional command line debugging tool for Android platform. Generally speaking, it will be installed when you install the Android SDK using platform-tools in Android Studio, but if you want the operating system to know the location of adb, you need to do some settings.

First, open Android Studio and navigate to "Tools> SDK Manager" from the menu:

△ Android Studio 中的 Tools > SDK Manager

△ Tools> SDK Manager in Android Studio

At the top of the window, you can see the path where the IDE installs the Android SDK.

Now open your terminal, type cd and paste this path. You can do this on Windows, macOS, and Linux, but on Windows, you may need to press shift + insert to paste the path into the terminal:

Next enter cd platform-tools:

Then type ls (dir in Windows). You should see adb or adb.exe-depending on your operating system. Then you can enter ./adb and view the program output.

You can run adb now, but I have not seen a tutorial starting with "copy your SDK path, use cd to enter the platform-tools folder, and type ./ before adb." In order to get an experience that more Android developers like, you must update the PATH environment variable. The setting methods are different on different operating systems. I will list the setting methods on macOS and Windows below. On Linux, the steps may be different, but the instructions for macOS also apply in some cases.

macOS configuration

macOS (and Linux) users usually use zsh or bash for their shells. To temporarily add the platform-tools directory to the PATH, enter:

PATH="<path from the SDK manager>/platform-tools:$PATH"

Combining the previous example is:

PATH="/Users/martinpatrick/Library/Android/sdk/platform-tools:$PATH"

Now, the current terminal window is ready to type adb and execute commands at any time. But how to maintain this state?

Since Android Studio tends to install the Android SDK in your user directory, you need to edit the PATH for the user. Moreover, since it is a command line program, you only need to update the PATH for the terminal (Mac applications don't use this). To do this, you need to edit the .profile file in your home directory (if the file does not exist, you can use .bash_rc to get similar results). The file is hidden by default, so you may not be able to see it in the Finder. You can open this file in your favorite text editor:

nano ~/.profile

And add at the end of the file:

export ANDROID_SDK_ROOT="<your Android SDK path>"
export PATH="$ANDROID_SDK_ROOT/platform-tools:$PATH"

On my computer it is:

export ANDROID_SDK_ROOT="/Users/martinpatrick/Library/Android/sdk"
export PATH="$ANDROID_SDK_ROOT/platform-tools:$PATH"

Adding an additional ANDROID_SDK_ROOT statement is not a necessary operation, but it may also be helpful for some more complex advanced user tools (such as the Cocos 2D command line program).

This way, when you open a new terminal window and type adb, if you do all the steps right, you will see a help page listing the usage of adb.

Windows configuration

Windows is a little different, I will work entirely in PowerShell. As before, I can use the SDK Manager to find the SDK installation location:

And I can also change the directory to find my platform-tools:

To update the PATH in PowerShell, enter:

$env:PATH += ";<your Android SDK directory>\platform-tools"

The specific commands in this example are:

$env:PATH += ";C:\Users\pux19\AppData\Local\Android\Sdk\platform-tools"

To keep this PATH variable in multiple terminal windows (including the regular cmd.exe prompt if you need it), we can set this variable for the user in the GUI tool.

The easiest way to change this setting is to click on the Windows menu and search for "Edit the system environment variables" (I usually search for "environment variables"):

Then click "Environment Variables":

Double-click "Path" in the "User variables" section:

Then, click a new cell and paste the path of your platform-tools directory into the cell:

In this example, C:\Users\pux19\AppData\Local\Android\Sdk\platform-tools

After you click "OK" on all open windows, the new terminal window will respond to adb commands as you type. Microsoft usually recommends logging out and logging in again to keep this state, but unless you need to use PATH in GUI programs, this operation is not absolutely necessary.

summary

In this way, you can freely manage and debug mobile phones, tablets, and even set-top boxes from the command line. Please also note that many tools will be installed with their own SDK. Just follow the same steps and make some minor modifications to make any Android SDK your "default" tool. Remember, you can only add one path to PATH at a time. And if you decide to uninstall the development tools (or even Android Studio), you must manually update these paths.


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。