How to use Android command line tools to build a .apk file – Ubuntu

Using command line tools to build an .apk file in Ubuntu OS seems a very complex task. It may be harder if you don’t follow the right steps. So lets start.

Before doing the following steps make sure you’ve downloaded the JDK. Use the following command for that.

apt install openjdk-17-jdk openjdk-17-jre

1. First go to https://developer.android.com/studio page and scroll to “Command line tools only”. Get a download link of commandlinetools-linux-version.zip.

Now download your file on Ubuntu using :-

wget https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip

2. Now create a directory “android_sdk” on the root.

3. Go to the directory where you where you downloaded the file and unzip that file using follwing method command.

unzip commandlinetools-linux-10406996_latest.zip -d ~/android_sdk

4. When you will unzip, your directory structure inside android_sdk will be cmdline-tools/bin but it should be cmdline-tools/latest/bin . So do “mkdir latest” and move your bin and lib to latest directory.

5. Now we need to set PATH. Just past the following to .bashrc file.

open .bashrc file with :-

nano ~/.bashrc

And paste following content at the last in the .bashrc file.

export ANDROID_SDK_ROOT=$HOME/android-sdk
export PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools

7. You close your current terminal and open new one for the working of sdkmanager. If you are using VPS just do ssh login again.

6. Now Accept License and install platform tools :-

sdkmanager --licenses
sdkmanager platform-tools

To build apk, go to root directory of Android project and run “./gradlew build”

For the release build just run “./gradlew assembleRelease”

Note that if your are using latest command line tools then sdkmanager may not work with older Java versions such as Java 8,11 etc.