Running React Native in WSL with the emulator running directly in Windows

前端 未结 4 1473
庸人自扰
庸人自扰 2020-12-13 07:41

I haven\'t done Android development in a while, so my knowledge of modern Android development is spotty.

I\'m trying to learn React Native. I use WSL as my primary d

4条回答
  •  暖寄归人
    2020-12-13 08:28

    For those who struggle to make Windows 10 Android Studio work with WSL2 located react-native project.

    Inital setup

    You need to install on your Windows 10 Android Studio.

    Set user variable: ANDROID_HOME=C:\Users\\AppData\Local\Android\Sdk

    Add to system variable PATH: %ANDROID_HOME%\emulator %ANDROID_HOME%\platform-tools %ANDROID_HOME%\tools %ANDROID_HOME%\cmdline-tools\latest (I'm not sure if this one is necessary)

    Then go to your WSL2 and install:

    1. sudo apt-get install unzip
    2. get android studio Command Line Tools Only and unzip it into /home//Android
    3. install jdk 8 with sudo apt-get install openjdk-8-jdk
    4. add this to your .bashrc:
    export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
    export JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
    export PATH=$PATH:$JAVA_HOME/bin
    
    export ADB_SERVER_SOCKET=tcp::5037 (check your WSL adapter IP by running `ipconfig` in powershell.)
    
    export ANDROID_HOME=$HOME/Android
    export PATH=$PATH:$ANDROID_HOME/emulator
    export PATH=$PATH:$ANDROID_HOME/tools
    export PATH=$PATH:$ANDROID_HOME/tools/bin
    export PATH=$PATH:$ANDROID_HOME/platform-tools
    
    1. restart bash and run sdkmanager --sdk_root=${ANDROID_HOME} "platform-tools"
    2. run adb version && adb.exe version check if versions match

    Running on emulator

    1. run adb kill-server (ps) / adb.exe kill-server (wsl2 bash)
    2. run adb -a -P 5037 nodaemon server (ps) / adb.exe -a -P 5037 nodeamon server (wsl2 bash) - don't close terminal window!
    3. run emulator -avd (ps) / emulator.exe -avd - don't close terminal window!
    4. run adb kill-server (WSL2 bash)
    5. run adb devices (WSL2 bash) - you should now see your emulated device pick up it's id
    6. go to your react-native project directory in WSL2 and run react-native run-android --deviceId=
    7. you should be all set up now.

    Same flow stays for USB connected devices. The only thing that is changing is that instead of step 4 you connect your developer enabled phone to PC and you should get in a terminal window with running adb server log that new device is connected.

    NOTE: You can run all commands in PowerShell by aliases without .exe and path only if you have defined environmental variables on windows 10 and extended PATH system variable. If you want to use all commands from WSL2 bash you always need to add .exe when you want to execute something on windows side so WSL2 knows that it needs to reach to windows executables.

提交回复
热议问题