Skip to content

Instantly share code, notes, and snippets.

@iliiliiliili
Last active March 19, 2019 13:57
Show Gist options
  • Save iliiliiliili/61917ce2ed127104773712fcf54731c1 to your computer and use it in GitHub Desktop.
Save iliiliiliili/61917ce2ed127104773712fcf54731c1 to your computer and use it in GitHub Desktop.
Install android-sdk for cli ubuntu and run android emulator as daemon for selected android API level
#!/bin/bash
#################################
# run from home directory #
#################################
#If you want to run it on GCP, you need to create nested-vm
#It will prompt you on Java instalation and avd creation
#You may need to run source ~/.bashrc after
#Thanks to https://gist.github.com/xiaokangwang/4a0f19476d86213ef6544aa45b3d2808
# Download and install android-sdk
# Echo on
set -x
# 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 expect # NDK stuff
# Get SDK tools (link from https://developer.android.com/studio/index.html#downloads)
wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
mkdir android-sdk-linux
unzip sdk*.zip -d android-sdk-linux
# Get NDK (https://developer.android.com/ndk/downloads/index.html)
wget https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip
unzip android-ndk*.zip >> /dev/null
ACCEPT_LICENSES_URL=https://gist.githubusercontent.com/xiaokangwang/1489fd223d26581bfec92adb3cb0088e/raw/328eb6925099df5aae3e76790f8232f0fc378f8b/accept-licenses
ACCEPT_LICENSES_ITEM="android-sdk-license-bcbbd656|intel-android-sysimage-license-1ea702d1|android-sdk-license-2742d1c5"
# Let it update itself and install some stuff
cd android-sdk-linux/tools
curl -L -o accept-licenses $ACCEPT_LICENSES_URL
chmod +x accept-licenses
./accept-licenses "./android update sdk --use-sdk-wrapper --all --no-ui" $ACCEPT_LICENSES_ITEM >/dev/null
# Download every build-tools version that has ever existed
# This will save you time! Thank me later for this
#./accept-licenses "./android update sdk --use-sdk-wrapper --all --no-ui --filter 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27" $ACCEPT_LICENSES_ITEM
PACKAGE_PARSE_URL=https://gist.githubusercontent.com/xiaokangwang/06268fb23034ed94bc301880e862da09/raw/afd95cbbe2f8c1d9e7b0277b7c5ef39af756a6ee/parse.awk
reduceout=https://gist.githubusercontent.com/xiaokangwang/4684bdb5c3415b943f52aa4803386480/raw/b46dab1cc60f02c0d87f88f01e27157034218faa/out.awk
cd bin
curl -L -o parse.awk $PACKAGE_PARSE_URL
curl -L -o reduce.awk $reduceout
sudo apt-get install gawk
./sdkmanager --verbose --list |awk -f parse.awk > ~/package_to_install
readarray -t filenames < $HOME/package_to_install
cat $HOME/package_to_install
yes|./sdkmanager --verbose "${filenames[@]}" |awk -f reduce.awk
# 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
echo 'export NDK_HOME=$HOME/android-ndk-r15c' >> ~/.bashrc
echo 'export ANDROID_NDK_HOME=$NDK_HOME' >> ~/.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
#Configure avd
#IMPORTANT
mkdir ~/android-sdk-linux/platforms
read -p "Android API level [27]: " version
version=${version:-27}
echo $version
cd ~/android-sdk-linux/tools/bin/
yes|./sdkmanager --licenses
./sdkmanager "platform-tools"
#abi (https://stackoverflow.com/questions/48143362/avdmanager-error-invalid-tag-default-for-the-selected-package)
#Sometimes it needs abi parameter and sometimes not. One of these will do nothing and another will install system-image
./sdkmanager "system-images;android-${version};google_apis;x86"
./sdkmanager "system-images;android-${version};google_apis;x86" --abi google_apis/x86
./avdmanager create avd -n android${version} -k "system-images;android-${version};google_apis;x86"
apt-get install libpulse0:i386 -y
apt-get install pulseaudio -y
cd ~
cat <<EOT >> run-emulator.sh
cd ~/android-sdk-linux/emulator/
./emulator -avd android${version} -no-audio -no-window
EOT
chmod u+x run-emulator.sh
screen -dmS avd${version} ./run-emulator.sh
sleep 17
~/android-sdk-linux/platform-tools/adb devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment