Skip to content

Instantly share code, notes, and snippets.

@fedme
Last active May 7, 2023 16:21
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save fedme/fd42caec2e5a7e93e12943376373b7d0 to your computer and use it in GitHub Desktop.
Save fedme/fd42caec2e5a7e93e12943376373b7d0 to your computer and use it in GitHub Desktop.
Ionic Android Development on WSL (Windows Subsystem for Linux)

Ionic Android Development on WSL

Installing the required software

Execute the following commands to install Node, npm, git, Java, Ionic, Cordova and Angular CLI:

cd ~
sudo apt update
sudo apt upgrade

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm i -g ionic cordova @nestjs/cli @angular/cli

sudo apt install git
git config --global user.email "you@yourmail.com"
git config --global user.name "Your Name"
git config --global core.autocrlf input

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt install oracle-java8-set-default

NOTE: Node can be installed via NVM (node version manager) on ZSH as described here: http://www.boekhoff.info/how-to-install-nodejs-and-npm-using-nvm-and-zsh-nvm/

Downloading the Android SDK Tools

To download the Android SDK tools in your home folder execute the following commands:

cd ~
sudo mkdir android-sdk
cd android-sdk
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip

Note: you can get an updated Android SDK link from https://developer.android.com/studio/#downloads

Adding the Android SDK to your PATH

Add the following to your .profile (or .zshenv if you use zsh):

# Add Android SKD to PATH
export ANDROID_HOME=$HOME/android-sdk
PATH="$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH"
export PATH

Installing the required Android SDK components

Execute the following commands to install some basic Android SDK components.

Note that you can install any component listed with sdkmanager --list by running sdkmanager "packagename". Always run sdkmanager --licenses to accept all the licenses after installing something.

sdkmanager --update
sdkmanager --list
sdkmanager "build-tools;26.0.2" "platform-tools" "platforms;android-26" "tools"
sdkmanager --licenses

Install Gradle

Install gradle with the following command:

sudo apt install gradle

ADB Alias

We can default to Windows's adb.exe (which can read USB) instead of the Linux one.

Create a ~/.aliases file with the following content:

alias adb='adb.exe'

ZSH

If you use ZSH, add the following to your .zshrc

source $HOME/.aliases

Bash

If you use bash, add the following to your .bashrc

if [ -f ~/.aliases ]; then
    . ~/.aliases
fi
@hanzhangyu
Copy link

@fdmnio

Since WSL does not use a Linux kernel, it cannot be a full replacement for a Linux-based operating system. Most obviously, any feature of Linux that Windows does not support, such as many file systems, Linux Containers and KVM, will be missing.

Every this goes well until I run cordova emulate android

ERROR: resizing partition resize2fs failed with exit code 1
emulator: WARNING: encryption is off
emulator: ERROR: x86_64 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: /dev/kvm is not found: VT disabled in BIOS or KVM kernel module not loaded

Do you have some idea, Thx in advance!

@marckassay
Copy link

sudo mkdir android-sdk

This line, you sudo mkdir command and then later executed sdkmanager without sudo. For whatever reason I can only execute sdkmanager locally, even after adding $ANDROID_HOME/tools/bin to $PATH and hash -r followed by exiting bash. So I had to do this to make it work:

sudo ./sdkmanager ...

Why is that?

Yesterday I attempted to install, but couldn't due to licenses agreement issue:

Warning: File /home/marckassay/.android/repositories.cfg could not be loaded.

Even after manually creating this .cfg file and accepting the license agreements as recommended by some. However, following your instructions and how I had to execute sdkmanager locally, I don't have this issue. Interestingly, the .cfg file is in the following directory: /root/.android/repositories.cfg

I assume yesterday's issue that I had, is related to permissions and/or location of that .cfg file. Thanks for the gist!

@marckassay
Copy link

@marckassay

I'm now able to execute sdkmanager in my development working directory (MyApp). In pseudocode, I did at least the following to make it work:

cd ~
sudo chown -R $(whoami) [ .android |  android-sdk | development/MyApp ]
sudo chmod -R 755 [ .android |  android-sdk | development/MyApp ]
sudo mv /root/.android/repositories.cfg .android

@miloshavlicek
Copy link

You can call mkdir android-sdk without sudo

@proverbian
Copy link

Thank you for this! :) 👍

@arcs-
Copy link

arcs- commented Feb 17, 2021

Thank you so much for this, you are my hero!

The adb alias didn't work for me, i had to create a symlink. If anyone else faces this issue, I fixed it like this $ ln -s /mnt/c/......./platform-tools/adb.exe adb

@Noext
Copy link

Noext commented Mar 29, 2021

does not work any more, wsl cant parse the windows output of adb.exe
2021-03-29T15:52:29.550Z native-run:android:utils:adb:parseAdbDevices adb devices output line does not match expected regex: 'fb0d46c5 device product:RMX1993EEA model:RMX1993 device:RMX1993L1 transport_id:2\r'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment