Skip to content

Instantly share code, notes, and snippets.

@zhy0
Last active February 3, 2024 08:19
Show Gist options
  • Star 89 You must be signed in to star a gist
  • Fork 32 You must be signed in to fork a gist
  • Save zhy0/66d4c5eb3bcfca54be2a0018c3058931 to your computer and use it in GitHub Desktop.
Save zhy0/66d4c5eb3bcfca54be2a0018c3058931 to your computer and use it in GitHub Desktop.
Install Android SDK on headless Ubuntu linux machine via command line, so that you can compile open source Android apps.
#!/bin/bash
# Thanks to https://gist.github.com/wenzhixin/43cf3ce909c24948c6e7
# Execute this script in your home directory. Lines 17 and 21 will prompt you for a y/n
# Install Oracle JDK 8
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install -y oracle-java8-installer
apt-get install -y unzip make # NDK stuff
# Get SDK tools (link from https://developer.android.com/studio/index.html#downloads)
wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar xf android-sdk*-linux.tgz
# Get NDK (https://developer.android.com/ndk/downloads/index.html)
wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip
uznip android-ndk*.zip
# Let it update itself and install some stuff
cd android-sdk-linux/tools
./android update sdk --no-ui
# Download every build-tools version that has ever existed
# This will save you time! Thank me later for this
./android update sdk --all --no-ui --filter $(seq -s, 27)
# If you need additional packages for your app, check available packages with:
# ./android list sdk --all
# install certain packages with:
# ./android update sdk --no-ui --all --filter 1,2,3,<...>,N
# where N is the number of the package in the list (see previous command)
# Add the directory containing executables in PATH so that they can be found
echo 'export ANDROID_HOME=$HOME/android-sdk-linux' >> ~/.bashrc
echo 'export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools' >> ~/.bashrc
source ~/.bashrc
# Make sure you can execute 32 bit executables if this is 64 bit machine, otherwise skip this
dpkg --add-architecture i386
apt-get update
apt-get install -y libc6:i386 libstdc++6:i386 zlib1g:i386
# Add some swap space, useful if you've got less than 2G of RAM
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
# Optionally run build system as daemon (speeds up build process)
mkdir ~/.gradle
echo 'org.gradle.daemon=true' >> ~/.gradle/gradle.properties
# See here: https://www.timroes.de/2013/09/12/speed-up-gradle/
@sebastianpfluegelmeier
Copy link

i corrected "uznip" to "unzip" in line 18, please "merge" my fork
https://gist.github.com/sebastianpfluegelmeier/79ed9d47f31a244aeeea96659c82c459

@anu-rock
Copy link

I think merging gist forks is not currently supported. I'd too like the author of this gist to update line 18:
https://gist.github.com/zhy0/66d4c5eb3bcfca54be2a0018c3058931#file-ubuntu-cli-install-android-sdk-sh-L18

@LuckyRepo
Copy link

Getting

Processing triggers for libc-bin (2.23-0ubuntu9) ...
fallocate: fallocate failed: Operation not supported
mkswap: error: swap area needs to be at least 40 KiB
swapon: /swapfile: read swap header failed

swapon command gives

(1:513)# swapon NAME TYPE SIZE USED PRIO /dev/sda2 partition 3.8G 93.8M -1

@atulmyself
Copy link

atulmyself commented Nov 15, 2017

good post its really helps to me please change the spelling of unzip "uznip android-ndk*.zip"

@thomas-gosnet
Copy link

thomas-gosnet commented Nov 16, 2017

ubuntu-cli-install-android-sdk.sh: 26: ubuntu-cli-install-android-sdk.sh: ./android: not found
ubuntu-cli-install-android-sdk.sh: 40: ubuntu-cli-install-android-sdk.sh: source: not found

after L22

swapon: /swapfile: swapon failed: Function not implemented

@NealEhardt
Copy link

Similar situation for me. After I run ./android update sdk --no-ui, the tools directory is empty! In newer versions of Android SDK, the android tool is deprecated. I wonder if that is related...

@abramovk
Copy link

Filter 1 not supported
Script dont work

@malikkurosaki
Copy link

./android update sdk --no-ui --no-https

for lite tricky , error pear

@pranavavva
Copy link

thank you so much!

@Vap0r1ze
Copy link

Vap0r1ze commented Feb 9, 2019

*Lines 22 and 26 prompt for y/n

@szepnapot
Copy link

Line 22, 26 prompt can be solved with echo y | android update sdk --no-ui

https://stackoverflow.com/a/27060392/5682956

@andrestest1125
Copy link

Package 'oracle-java8-installer' has no installation candidate

can be fixed with:

sudo apt-get install openjdk-8-jdk-headless

@biopsin
Copy link

biopsin commented Mar 11, 2022

How am I suppose to accept this "Warning: License for package Android SDK Platform 30 not accepted." via cli?

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