Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Android SDK on Windows Bash (WSL)
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-26" "build-tools;26.0.3"
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
@serhii-ilin

This comment has been minimized.

Copy link

@serhii-ilin serhii-ilin commented Jun 3, 2019

Are you able to run emulator from wsl ?

@vitorgja

This comment has been minimized.

Copy link

@vitorgja vitorgja commented Jun 29, 2019

Hi,
before first line you can include "sudo apt-get update". thank you

@bovesan

This comment has been minimized.

Copy link

@bovesan bovesan commented Aug 5, 2019

Thank you!

To avoid the user variable:
1 cd
12 export ANDROID_HOME=~/Android
15 printf "\n\nexport ANDROID_HOME=~/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc

EDIT: Turns out the Ubuntu repo gradle is way updated.
Instead of line 17 sudo apt-get install gradle, I had to do

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle 5.5.1
@rwrz

This comment has been minimized.

Copy link

@rwrz rwrz commented Aug 15, 2019

BIG TIP here, change the $PATH to the END, because if you have Android studio installed on your windows, you won't have issues with their PATHs...

export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH

I was having an EPERM error, that fixed with it.

@NobsterDev

This comment has been minimized.

Copy link

@NobsterDev NobsterDev commented Aug 21, 2019

Thanks! Works perfect.

@UppaJung

This comment has been minimized.

Copy link

@UppaJung UppaJung commented Oct 9, 2019

Super helpful starting point!

I found it useful to substitute $HOME for /home/<user>

I had to add a /bin after /tools to one of the updates of the .bashrc

printf "\n\nexport ANDROID_HOME=$HOME/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools/bin\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc

Since the android command is deprecated
android update sdk --no-ui should been replaced with sdkmanager --update

@Irikefe41

This comment has been minimized.

Copy link

@Irikefe41 Irikefe41 commented Oct 13, 2019

This guide is perfect ! Thanks, you just saved me hours of work

@alexksso

This comment has been minimized.

Copy link

@alexksso alexksso commented Nov 23, 2019

missing --install on the package installer commandline, otherwise it won't prompt the license
./sdkmanager --install "platform-tools" "platforms;android-26" "build-tools;26.0.3"

Also, as you'll likely want to interact with devices, you'll need adb from the platform tools actually running on windows (not wsl)
get them here and launch it from powershell with adb start-server
make sure adb versions match on both wsl and windows.

@KROSF

This comment has been minimized.

Copy link

@KROSF KROSF commented Nov 25, 2019

@alexksso can you share the steps to follow on windows 10

@alexksso

This comment has been minimized.

Copy link

@alexksso alexksso commented Nov 25, 2019

@alexksso can you share the steps to follow on windows 10

I literally just followed the steps above. Do not launch adb from WSL though, launch it from a powershell - the wsl adb will find the deamon and connect to it seamlessly.

@KROSF

This comment has been minimized.

Copy link

@KROSF KROSF commented Nov 27, 2019

@alexksso thanks.

@MarekJavurek

This comment has been minimized.

Copy link

@MarekJavurek MarekJavurek commented Nov 29, 2019

Thanks fir gist and comments guys!

@brombomb

This comment has been minimized.

Copy link

@brombomb brombomb commented Dec 14, 2019

needed to add zip to the packages to install.... final script all together looks like this....

cd ~
sudo apt-get install unzip zip
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 --install "platform-tools" "platforms;android-26" "build-tools;26.0.3"
export ANDROID_HOME=~/Android
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
printf "\n\nexport ANDROID_HOME=~/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc
sdkmanager --update
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle 5.5.1
gradle -v

# run on windows in powershell
# adb start-server
@daniele-pecora

This comment has been minimized.

Copy link

@daniele-pecora daniele-pecora commented Dec 16, 2019

Thanks for gist and comments guys!

@neilbeaton

This comment has been minimized.

Copy link

@neilbeaton neilbeaton commented Mar 10, 2020

This was great and helped me tremendously. One suggestion, can we perhaps incorporate the powershell section into the bash script. Something along the lines of ...

create power shell script with following content...

adb start-server

save as powershell script 'adbstart.ps1'

in bash script...

powershell.exe -File "c:\users<username>\Desktop\adbstart.ps1"

@italomlp

This comment has been minimized.

Copy link

@italomlp italomlp commented Mar 21, 2020

This gist still works \o/! My two cents: if you already installed adb through apt-get, uninstall it to the system recognize the installation from here.

@sushant-at-nitor

This comment has been minimized.

Copy link

@sushant-at-nitor sushant-at-nitor commented Apr 1, 2020

Now this is doing the wonders for me - return '/mnt/c/Android/adb.exe' from ./node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/getAdbPath.js

Also you can alias your adb as alias adb='adb.exe'

@ArnyminerZ

This comment has been minimized.

Copy link

@ArnyminerZ ArnyminerZ commented Apr 18, 2020

It can be a good practise to use cd $HOME in the first line.

@gusflopes

This comment has been minimized.

Copy link

@gusflopes gusflopes commented May 13, 2020

Not sure what I'm doing wrong, but have you managed to get this working on WSL 2 with the project saved on WSL Drive?

@angelod1as

This comment has been minimized.

Copy link

@angelod1as angelod1as commented Jun 20, 2020

I'm running react-native. When I run react-native run-android it doesn't find the device. When I run adb.exe devices, it shows my running device. I already created an alias adb="adb.exe". What am I doing wrong?

@adrach

This comment has been minimized.

Copy link

@adrach adrach commented Jul 2, 2020

this is amazing, thx!

@Potolski

This comment has been minimized.

Copy link

@Potolski Potolski commented Jul 5, 2020

THANK YOU SO MUCH!!!!!!!

@ankitbansal82

This comment has been minimized.

Copy link

@ankitbansal82 ankitbansal82 commented Jul 5, 2020

Thanks this helped me setup android sdk toolchain on WSL 1 Ubuntu distribution.

Which means I can now run flutter on WSL and even use tools like fastlane which don't work naturally on windows but they can on WSL.

@theodoromatheus1830

This comment has been minimized.

Copy link

@theodoromatheus1830 theodoromatheus1830 commented Jul 8, 2020

Thank you so much!!!

@andrewzolotukhin

This comment has been minimized.

Copy link

@andrewzolotukhin andrewzolotukhin commented Jul 30, 2020

Thanks, works fine. But I'd suggest to add quoutes for exports, sometimes PATH contains spaces (if it points to something like "Program Files"):

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-26" "build-tools;26.0.3"
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

@deva-gopalani

This comment has been minimized.

Copy link

@deva-gopalani deva-gopalani commented Aug 25, 2020

When I run the command
./sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3"

I am getting an error saying

s;android-26" "build-tools;26.0.3"
Warning: java.net.SocketException: Resource temporarily unavailable (connect failed)
Warning: Failed to download any source lists!
Warning: File /home/dmgopalani/.android/repositories.cfg could not be loaded.
Warning: Failed to find package platform-tools

What is the possible issue? Could someone help me with this?

@codewired

This comment has been minimized.

Copy link

@codewired codewired commented Aug 29, 2020

@deva-gopalani, Add install after ./sdkmanager, the full command is below
=> ./sdkmanager --install "platform-tools" "platforms;android-26" "build-tools;26.0.3"

@NOPROD

This comment has been minimized.

Copy link

@NOPROD NOPROD commented Aug 30, 2020

@deva-gopalani

first solution :
mkdir -p /root/.android && touch /root/.android/repositories.cfg

second :
If when you do java -version

if you have openjdk version "11.0.8" 2020-07-14 or java > 8

you need to change your java version :

sudo update-alternatives --config java

select : /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

and try again
./sdkmanager --install "platform-tools" "platforms;android-26" "build-tools;26.0.3"

@jenipharachel

This comment has been minimized.

Copy link

@jenipharachel jenipharachel commented Nov 18, 2020

@NOPROD I want to install the latest version of java and android studio with the android version 29 instead of 6. Any idea on how I can achieve this?

@jenipharachel

This comment has been minimized.

Copy link

@jenipharachel jenipharachel commented Nov 18, 2020

I followed the exact steps but downloaded android 29 for platform and build tools version as 29.2.0 for react-native and I cant detect my physical device in adb devices. Any help will be much appreciated

@anhdo9797

This comment has been minimized.

Copy link

@anhdo9797 anhdo9797 commented Jan 3, 2021

@NOPROD I want to install the latest version of java and android studio with the android version 29 instead of 6. Any idea on how I can achieve this?

change line 11 form:
./sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3"
to
./sdkmanager "platform-tools" "platforms;android-29" "build-tools;29.02 follow version platforms;android and build-tools at https://reactnative.dev/docs/environment-setup

@briones-gabriel

This comment has been minimized.

Copy link

@briones-gabriel briones-gabriel commented Jan 20, 2021

sudo update-alternatives --config java

Solved my problem, thank you!

@leandrocrs

This comment has been minimized.

Copy link

@leandrocrs leandrocrs commented Mar 31, 2021

@BenHunt-io

This comment has been minimized.

Copy link

@BenHunt-io BenHunt-io commented Apr 9, 2021

If downloading the up-to-date cmdline-tools from the Android Studio website, you'll need to change the directory structure in order for you to be able to run the ./sdkmanager.

/home/ben/cmdline-tools/bin -> /home/ben/Android/cmdline-tools/latest/bin
worked for me.

https://stackoverflow.com/questions/65262340/cmdline-tools-could-not-determine-sdk-root

@AditechGH

This comment has been minimized.

Copy link

@AditechGH AditechGH commented Jun 4, 2021

I got the below exception when I run ./sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3". Any Idea how I can resolve this?

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more

@alyleite

This comment has been minimized.

Copy link

@alyleite alyleite commented Jun 4, 2021

I got the below exception when I run ./sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3". Any Idea how I can resolve this?

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more

Install jdk 8

@Download

This comment has been minimized.

Copy link

@Download Download commented Jun 24, 2021

printf "\n\nexport ANDROID_HOME=/home//Android\nexport PATH=$PATH:$ANDROID_HOME/tools\nexport PATH=$PATH:$ANDROID_HOME/platform-tools" >> ~/.bashrc

Be careful! When you run this command multiple times it will end up making .bashrc larger and larger.
Ideally this cmd would contain some if to test whether .bashrc was already updated before.

@b6873

This comment has been minimized.

Copy link

@b6873 b6873 commented Jul 31, 2021

Help me

@b6873

This comment has been minimized.

Copy link

@b6873 b6873 commented Jul 31, 2021

i'm pool and we need a moneys please help me i'm myanmar please help My country😭

@unicornlab-brylleg

This comment has been minimized.

Copy link

@unicornlab-brylleg unicornlab-brylleg commented Aug 6, 2021

Thank you all for this! My version of the scripts after looking into all the comments...

sudo apt-get update
cd
sudo apt-get install unzip zip
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
### check latest platform and build-tools versions (29, 29.0.2)
./sdkmanager --install "platform-tools" "platforms;android-29" "build-tools;29.0.2"
export ANDROID_HOME=~/Android
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
printf "\n\nexport ANDROID_HOME=~/Android\nexport PATH=\$PATH:\$ANDROID_HOME/tools\nexport PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc
./sdkmanager --update
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
### check latest grade version
sdk install gradle 5.5.1
gradle -v

### run "adb start-server" in powershell
@juanmartinez-viamericas

This comment has been minimized.

Copy link

@juanmartinez-viamericas juanmartinez-viamericas commented Aug 10, 2021

Excellent script thanks for sharing. I think you can update it by replacing /home/<user>/ with $HOME. Example:

export ANDROID_HOME="$HOME/Android"

And ANDROID_HOME environment variable was deprecated. It's recommended write as:

export ANDROID_HOME="$HOME/Android"
export ANDROID_SDK_ROOT="$ANDROID_SDK_ROOT"
@luiscarbonell

This comment has been minimized.

Copy link

@luiscarbonell luiscarbonell commented Oct 12, 2021

THIS WORKS!!!! FINALLY!!!

lol

Jokes aside, I tried 6+ different things which led to dozens of StackOverflow Questions, GitHub Threads, etc. and all of them failed except this beautiful gem!

@jjvillavicencio Thank you! (Gracias!) @unicornlab-brylleg Thank you for the update!

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