Setting up Android SDK environment on Linux

  1. Install the Android command line tools. Go to Android Developer and download the tools that match your operating system.

    ../../_images/android-command-line-tool.png

    You can use wget in the command line to download the tool as a compressed package, or download it from the browser and extract it yourself.

    You can copy the download link from the above Android Developer website. Then use the following command.

    wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip?hl=zh-cn
    mkdir -p Android/cmdline-tools
    unzip commandlinetools-linux-11076708_latest.zip?hl=zh-cn -d Android/cmdline-tools
    mv Android/cmdline-tools/cmdline-tools Android/cmdline-tools/latest
    
  2. Configure the relevant environment.

    Install Java. (If you have already installed and configured Java, you need to check if your JDK is compatible with the command line tools. If the versions are compatible, you can skip the following steps regarding Java environment configuration.)

    We use JDK-17 to run the Android emulator.
    sudo apt install openjdk-17-jdk
    

    Open your .bashrc file.

    sudo nano ~/.bashrc
    

    Add the following content at the end of the file.

    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
    export PATH=$PATH:$JAVA_HOME/bin
    export ANDROID_HOME=$HOME/Android
    export PATH="$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/tools/bin:$ANDROID_HOME/cmdline-tools/latest:$ANDROID_HOME/platform-tools:$PATH"
    
    Ensure that the PATH configuration matches the path where you store the relevant tools.

    Finally, reload the .bashrc file to apply the changes immediately to the current terminal session.

    source ~/.bashrc
    
  3. Verify if sdkmanager is successfully installed. .. code-block:: bash

    sdkmanager –update sdkmanager –list sdkmanager –licenses

    If you receive the following information, the installation was successful.

    ../../_images/sdkmanager-licenses.png

    You can learn more from the Common Commands of sdkmanager.

Common Issues with WSL

1. WSL Dependencies

Please upgrade your Windows to Win11 and use WSL 2. This will resolve most WSL issues in Win10 and WSL 1.

2. WSL PATH Settings

By default, WSL shares environment variables from the Windows system. Sometimes this can lead to incorrect behavior. You may find that the actual executable file you are using is not the one found through the which command. The root cause of this issue is that the which command in WSL can only find executable files in the WSL PATH. However, if you configure an executable file (e.g., python3) in both Windows PATH and WSL PATH, and the Windows PATH is set before the WSL PATH (PATH=$Windows_PATH:$WSL_PATH). Then the actual executable file you are using is the one from Windows_PATH. But the one you find through which is from WSL_PATH.

To resolve this issue, you can follow these suggestions:

  • Place your environment PATH at the front when setting up PATH

    Use PATH=New_PATH:$PATH instead of PATH=$PATH:New_PATH. This is a good practice that prioritizes your latest settings and ensures they always work.

  • Disable the sharing of environment variables

    # WSL bash
    sudo vim /etc/wsl.conf
    
    # 添加以下内容
    [interop]
    appendWindowsPath = false
    
    # 在 PowerShell 中重启 WSL
    wsl --shutdown
    

3. CPU Hardware Acceleration Issues

Error

该用户没有使用 KVM (/dev/kvm) 的权限,错误:x86 模拟目前需要硬件加速!

../../_images/issues1.png

Please follow the first solution in Stack Overflow - Android Studio: /dev/kvm device permission denied. Then log out and log back into Linux.

sudo adduser $USER kvm
sudo chown $USER -R /dev/kvm