使用VS code 和WSL开发android

目前开发主要时在Windows+AS开发不过有些部署相关的工作还是需要linux环境,所以重复之前wsl的挫折,又开始折腾wsl2.之前使用wsl 有报出比较多的问题,比如memory容易爆掉, 文件file mode在挂在盘上会发生变化导致git使用不方便等。建议可以使用下wsl2或者是使用无界面virtualbox, 透过ssh链接

vs in virtual box

https://code.visualstudio.com/docs/remote/ssh

remote ssh

设置ssh 的pub key,可以免掉输入密码的麻烦

其它参考wsl

WSL

install wsl

Enable the WSL subsystem

  • Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

download the release archive and run command with install
refer to https://damsteen.nl/blog/2018/08/29/installing-wsl-manually-on-non-system-drive
download the ubuntu release archive https://aka.ms/wsl-ubuntu-1804
query the registered ubuntu release
wslconfig.exe /l all
uninstall wsl
wsconfig.exe
wslconfig.exe /l all

C:\Users\users>wslconfig.exe /l /all
适用于 Linux 的 Windows 子系统:
Ubuntu-18.04 (默认)`
wslconfig.exe /u Ubuntu-18.04

install visual code

visual code with wsl
https://code.visualstudio.com/docs/remote/wsl

visual code with cmake

sudo apt install cmake
install plugin(you can refer to the article in step 1)

Android KIT

https://developer.android.com/studio/projects/install-ndk

install Android SDK

cd /home/<user>/
sudo apt-get install unzip
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip -d Android
rm sdk-tools-linux-4333796.zip
sudo apt-get install -y lib32z1 openjdk-8-jdk
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
printf "\n\nexport JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64\nexport PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrc
cd Android/tools/bin
./sdkmanager "platform-tools" "platforms;android-27" "build-tools;30.0.0-rc2"
export ANDROID_HOME=/home/<user>/Android
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools
printf "\n\nexport ANDROID_HOME=/home/<user>/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc
android update sdk --no-ui
sudo apt-get install gradle
gradle -v
adb start-server
install ndk
kiah@CD-DZ0200879:~/android/tools/bin$ ./sdkmanager ndk;17.2.4988734
install cmake

verify your IDE and wsl

https://github.com/nesc1/win_code_wsl_test

tool kits 设置

https://vector-of-bool.github.io/docs/vscode-cmake-tools/getting_started.html#building-your-project

[
  {
    "name": "Android Clang",
    "toolchainFile": "/home/kiah/android/ndk/17.2.4988734/build/cmake/android.toolchain.cmake",
    "compilers": {
      "C": "/home/kiah/android/ndk/17.2.4988734/toolchains/llvm/prebuilt/linux-x86_64/bin/clang",
      "CXX": "/home/kiah/android/ndk/17.2.4988734/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++"
    }
  }  
]

https://code.visualstudio.com/docs/editor/variables-reference

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "options": {
            },
            "args": [],
            "command": [
                "ANDROID_NDK_HOME=${env:ANDROID_NDK_HOME} ${workspaceFolder}/build_all.sh"
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Cmake",
            "type": "shell",
            "options": { 
                "env":{
                    "VIDEO_PROJECT": "oppo/sm8250",
                    "BUILD_NUMBER": "1",
                },
                "shell": {
                    "executable": "bash",
                    "args": [ "-c" ]
                }
            },
            "args": [
            ],
            "command": [
                "mkdir -p ${workspaceFolder}/output &&",
                "cd ${workspaceFolder}/output &&",
                "cmake ",
                "-DCMAKE_TOOLCHAIN_FILE=${env:ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake",
                "-DANDROID_NDK=${env:ANDROID_NDK_HOME} ",
                "-DANDROID_ABI=arm64_v8a               ",
                "-DANDROID_TOOLCHAIN=clang             ",
                "-DANDROID_PLATFORM=android-26         ",
                "-D__ARM_NEON__=1                      ",
                "-DANDROID_ARM_NEON=1                  ",
                "-DCURRENT_MODULE_NAME=oppo/sm8250",
                "-DMODULE_LIB_NAME=AncHumanRetain",
                "-isystem /home/kiah/android/ndk/17.2.4988734/sources/android/support/include",
                "-isystem /home/kiah/android/ndk/17.2.4988734/sources/cxx-stl/llvm-libc++abi/include",
                "-isystem /home/kiah/android/ndk/17.2.4988734/sysroot/usr/include/aarch64-linux-android",
                " .. &&",
                "make -j11"
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },

    ]
}

限制WSL 内存使用

vmmem 爆host 内存解法
在C:\Users\%Username% 下面,创建一个.wslconfig 文件,文件中写以下内容

[wsl2]
memory=4GB # Limits VM memory in WSL 2 to 4 GB
swap=0
localhostForwarding=true

其它

About: kiah