Skip to content

Instantly share code, notes, and snippets.

@lboulard
Last active July 12, 2022 07:35
Show Gist options
  • Save lboulard/28379f5b4bf3c1cf32422c539a8d7c7d to your computer and use it in GitHub Desktop.
Save lboulard/28379f5b4bf3c1cf32422c539a8d7c7d to your computer and use it in GitHub Desktop.
Command line ANDROID SDK in Linux

Android SDK will be installed in $HOME/.local/share/android/sdk.

On page https://developer.android.com/studio/index.html#downloads, got to section "Get just the command line tools" at page end and download Linux package. With last edition of this document, this is commandlinetools-linux-8092744_latest.zip.

export ANDROID_SDK_ROOT=$HOME/.local/share/android/sdk
export ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk-bundle
mkdir -p $ANDROID_SDK_ROOT
wget -P "$HOME/Downloads" https://dl.google.com/android/repository/commandlinetools-linux-8092744_latest.zip
unzip "$HOME/Downloads/commandlinetools-linux-8092744_latest.zip" -d $ANDROID_SDK_ROOT

Now install minimal packages in order to further build a project.

# Avoid annoying warning from sdkmanager
mkdir $HOME/.android
touch $HOME/.android/repositories.cfg

# Install OpenJDK 8 if not already done
sudo apt install openjdk-8-jre-headless
# Verify that OpenJDK 8 is default for system
update-alternatives --display java
# If OpenJDK 11 is installed and set as default,
# you have to change system default to OpenJDK 8 with command:
sudo update-alternatives --config java
# Or, without prompt, listing choices
update-alternatives --list java
sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

# To restore java binary to auto default, use
sudo update-alternatives --auto java

# Accept all licences
yes | $ANDROID_SDK_ROOT/tools/bin/sdkmanager --licenses
# Update to lastest tools version
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --update --verbose
# Install NDK because gradle will never install by itself for project
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --verbose ndk-bundle

Inside a Android Project, ANDROID_SDK_ROOT and ANDROID_NDK_HOME have to defined each time to build with gradle.

export ANDROID_SDK_ROOT=$HOME/.local/share/android/sdk
export ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk-bundle
gradlew :app:build
# gradle binaries and packages are saved in $HOME/.gradle

HTTP proxy support

There is no way to put proxy configuration in a file. Proxy settings must always be given on command line.

Defines a variable SDKMAN_OPTS and always run sdkmanager [...] $SDKMAN_OPTS.

SDKMAN_OPTS="--proxy=http --proxy_host=127.0.0.1 --proxy_port=8123"
export SDKMAN_OPTS
yes | $ANDROID_SDK_ROOT/tools/bin/sdkmanager --licenses $SDKMAN_OPTS
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --update --verbose $SDKMAN_OPTS
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --verbose ndk-bundle $SDKMAN_OPTS

Fo zsh shell:

export SDKMAN_OPTS="--proxy=http --proxy_host=127.0.0.1 --proxy_port=8123"
yes | $ANDROID_SDK_ROOT/tools/bin/sdkmanager --licenses ${=SDKMAN_OPTS}
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --update --verbose ${=SDKMAN_OPTS}
$ANDROID_SDK_ROOT/tools/bin/sdkmanager --verbose ndk-bundle ${=SDKMAN_OPTS}
# Put this file in $HOME/.gradle/gradle.properties
# Prevent daemon from running after build
org.gradle.daemon=false
# Limit number of workers to reduce resources usage
org.gradle.workers.max=2
#systemProp.http.proxyHost=www.somehost.org
#systemProp.http.proxyPort=8080
#systemProp.http.proxyUser=userid
#systemProp.http.proxyPassword=password
#systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
#systemProp.https.proxyHost=www.somehost.org
#systemProp.https.proxyPort=8080
#systemProp.https.proxyUser=userid
#systemProp.https.proxyPassword=password
#systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment