了解如何设置 Android 模拟器以在虚拟 Android 设备上测试你的应用。
如果你没有可用于测试的 Android 设备,我们建议使用 Android Studio 附带的默认模拟器。如果你在设置时遇到任何问题,请按照本指南中的步骤操作。
¥If you don't have an Android device available to test with, we recommend using the default emulator that comes with Android Studio. If you run into any problems setting it up, follow the steps in this guide.
Use a package manager such as Homebrew to install the following dependency.
1
Install Watchman using a tool such as Homebrew:
-
brew install watchman
2
Install OpenJDK distribution called Azul Zulu using Homebrew. This distribution offers JDKs for both Apple Silicon and Intel Macs.
Run the following commands in a terminal:
-
brew install --cask zulu@17
After you install the JDK, add the JAVA_HOME
environment variable in ~/.bash_profile (or ~/.zshrc if you use Zsh):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
Use a package manager such as Chocolatey to install the following dependencies.
Install Java SE Development Kit (JDK):
-
choco install -y microsoft-openjdk17
1
Follow instructions from the Watchman documentation to compile and install it from the source.
2
Install Java SE Development Kit (JDK):
You can download and install OpenJDK from AdoptOpenJDK or your system packager.
1
Download and install Android Studio.
2
Open the Android Studio app, click More Actions and select SDK Manager.
3
Open Android Studio, go to Settings > Languages & Frameworks > Android SDK. From the SDK Platforms tab, select the latest Android version (API level).
Then, click on the SDK Tools tab and make sure you have at least one version of the Android SDK Build-Tools and Android Emulator installed.
4
Copy or remember the path listed in the box that says Android SDK Location.
5
Click Apply and OK to install the Android SDK and related build tools.
6
If you are on macOS or Linux, add an environment variable pointing to the Android SDK location in ~/.bash_profile (or ~/.zshrc if you use Zsh). For example: export ANDROID_HOME=/your/path/here
.
Add the following lines to your /.zprofile or ~/.zshrc (if you are using bash, then ~/.bash_profile or ~/.bashrc) config file:
-
export ANDROID_HOME=$HOME/Library/Android/sdk
-
export PATH=$PATH:$ANDROID_HOME/emulator
-
export PATH=$PATH:$ANDROID_HOME/platform-tools
7
Reload the path environment variables in your current shell:
# for zsh
-
source $HOME/.zshrc
# for bash
-
source $HOME/.bashrc
8
Finally, make sure that you can run adb
from your terminal.
1
Download Android Studio.
2
Open Android Studio Setup. Under Select components to install, select Android Studio and Android Virtual Device. Then, click Next.
3
In the Android Studio Setup Wizard, under Install Type, select Standard and click Next.
4
The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click Next after you have verified.
5
In the next window, accept licenses for all available components.
6
After the tools installation is complete, configure the ANDROID_HOME
environment variable. Go to Windows Control Panel > User Accounts > User Accounts (again) > Change my environment variables and click New to create a new ANDROID_HOME
user variable. The value of this variable will point to the path to your Android SDK:
7
To verify that the new environment variable is loaded, open PowerShell, and copy and paste the following command:
-
Get-ChildItem -Path Env:
The command will output all user environment variables. In this list, see if ANDROID_HOME
has been added.
8
To add platform-tools to the Path, go to Windows Control Panel > User Accounts > User Accounts (again) > Change my environment variables > Path > Edit > New and add the path to the platform-tools to the list as shown below:
9
Finally, make sure that you can run adb
from the PowerShell. For example, run the adb --version
to see which version of the adb
your system is running.
1
On the Android Studio main screen, click More Actions, then Virtual Device Manager in the dropdown.
2
Click the Create device button.
3
Under Select Hardware, choose the type of hardware you'd like to emulate. We recommend testing against a variety of devices, but if you're unsure where to start, the newest device in the Pixel line could be a good choice.
4
Select an OS version to load on the emulator (probably one of the system images in the Recommended tab), and download the image.
5
Change any other settings you'd like, and press Finish to create the emulator. You can now run this emulator anytime by pressing the Play button in the AVD Manager window.
¥Troubleshooting
adb
版本¥Multiple adb
versions
系统上有多个 adb
版本可能会导致以下错误:
¥Having multiple adb
versions on your system can result in the following error:
-
adb server version (xx) doesn't match this client (xx); killing...
这是因为你系统上的 adb
版本与 Android SDK 平台工具上的 adb
版本不同。
¥This is because the adb
version on your system is different from the adb
version on the Android SDK platform-tools.
1
打开终端并检查系统上的 adb
版本:
¥Open the terminal and check the adb
version on the system:
-
adb version
2
并从 Android SDK platform-tool 目录:
¥And from the Android SDK platform-tool directory:
-
cd ~/Library/Android/sdk/platform-tools
-
./adb version
3
将 adb
从 Android SDK 目录复制到 usr/bin
目录:
¥Copy adb
from Android SDK directory to usr/bin
directory:
-
sudo cp ~/Library/Android/sdk/platform-tools/adb /usr/bin