Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jason-s-yu/30375db45c1f71c1259e042d216e4bd3 to your computer and use it in GitHub Desktop.
Save jason-s-yu/30375db45c1f71c1259e042d216e4bd3 to your computer and use it in GitHub Desktop.

Install Android SDK CLI Ubuntu 20.04 WSL2 (Work in Progress)

Install Java 8

sudo apt update

sudo apt install openjdk-8-jdk-headless

Android SDK

wget https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip
mkdir -p Android/cmdline-tools
unzip commandlinetools-linux-8512546_latest.zip -d Android/cmdline-tools
mv Android/cmdline-tools/cmdline-tools Android/cmdline-tools/latest

export ANDROID_HOME=$HOME/Android
# Make sure emulator path comes before tools. Had trouble on Ubuntu with emulator from /tools being loaded
# instead of the one from /emulator
export PATH="$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/cmdline-tools/latest:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH"

# don't need this anymore
# sdkmanager --sdk_root=${ANDROID_HOME} "tools"

sdkmanager --update
sdkmanager --list
# find current version
sdkmanager --list | grep build-tools
# change the version below with whatever version is downloaded
sdkmanager "build-tools;30.0.3" "platform-tools" "platforms;android-30" "tools"
sdkmanager --licenses

sudo apt install gradle

Please find the latest version of Android SDK at: https://developer.android.com/studio/#downloads (scroll to the bottom of the page under the section "Command Line Tools only"

Used a combinaton of these gists:

Background on the update to command line tools from android sdk: https://stackoverflow.com/a/61176718

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

@joaop221
Copy link

joaop221 commented Nov 2, 2021

I recommend to add the update command and the unzip installation in this steps:

sudo apt update && sudo apt install openjdk-8-jdk-headless unzip

@jramiresbrito
Copy link

Thank you! It really helped

@alindafortunate
Copy link

Hello Team, i followed the steps above but i am getting this error

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/prefs/AndroidLocationsProvider has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Any support

@YoanAncelly
Copy link

Thank you ! That was very helpful

@devnshankar
Copy link

devnshankar commented Jan 7, 2024

I installed the following in my WSL2 ubuntu environment

commandlinetools-linux-10406996_latest.zip

I tried with the openjdk 8 and 11 both

8 issue

sdkmanager --update
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/sdklib/tool/sdkmanager/SdkManagerCli has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:621)

11 issue

sdkmanager --update
Error: LinkageError occurred while loading main class com.android.sdklib.tool.sdkmanager.SdkManagerCli
        java.lang.UnsupportedClassVersionError: com/android/sdklib/tool/sdkmanager/SdkManagerCli has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0

I tried different methods of installing jdk from other sources but still the issue persists.

UPDATE #1

JDK 17 worked out fine
everything works just fine but the issue is when i try to run a react native project from wsl with all this setup it does open up the expo go application on my phone for debuging but it doesn't run the application that is something which i am figuring right now.

And btw i used adb tcp server on port 5555 on my windows machine and then adb connected to the phone's ip address with the port 5555 and it worked just fine, connection was established

UPDATE #2

As long as it works I'm fine with it🥴

I tried every possible way but still I could not configure the wsl appropriately for android development.
So I followed a different approach

  1. Installed vcxsrv on my main windows11 machine
  2. Installed OpenJdk 17 (There seems to be a problem with openjdk as they are not recomended for android dev for less optimization, you can use oracle ones)
  3. Installed the latest android studio on wsl natively, the vcxsrv helped with the GUI and the rest of the steps are same as windows
  4. Installed android sdk tools on wsl
  5. I was using OhMyZsh with Ubuntu so I added some env variables to the .zshrc file
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
export ANDROID_HOME=~/Android/Sdk
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export STUDIO_JDK=$JAVA_HOME
  1. As Wsl is virtualized hence using an emulator from within wsl is posisble yet it's pretty slow, so slow that it will drive you crazy
  2. So i went for Mobile debugging
  3. Then finally initiated the adb server in the main machine windows 11 using tcp and retrieved the ip address of my mobile
  4. Connected the adb server to proxy from the debugging mobile using adb connect ip:port
  5. Initiated my react-native application and went for a development build instead of expo go as there seems to be some kind of problem with it which I have still not figured out yet.
  6. Let the build do it's stuff and wait a moment as it might take 10 to 15 mins for the first build and almost 5 seconds for the bulids afterwards which is blazingly fast compared to windows maybe it was optimized for unix based OSs
  7. Now that you have GUI functionality you can do native android application development with the android studio you just have to go to the bin folder of the android studio and manually start the .sh file or you can add it to the env for convinience.

There you have it 8 hours of pain finally paid off I hope it helps other lost souls.💀

@Kulunkulu
Copy link

Is this still working in Aug15th, 2024?
I've read so many threads and right now my head is spinning

@Kulunkulu
Copy link

@devnshankar
Do you have a working config for Windows 10?
if Windows 11 is the only way, can you please be more detailed on how to do it in Windows 11

@devnshankar
Copy link

@devnshankar Do you have a working config for Windows 10? if Windows 11 is the only way, can you please be more detailed on how to do it in Windows 11

yes it does, well i am pretty sure it does because i have since then migrated to Ubuntu as my primary driver for dev and i'd say it's far better compared to windows 11 I'd suggest use java 17 as it is the stable version most of them opt for. in my current system with ubuntu i use oracle's jdk 17 and it's way more performant.

hope it helps.

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