How to build an Android app from source

Regain the control over your apps!

Have you ever wanted to build an app from its source? Maybe you were curious about the new features added in the git repo, but the release date is unknown? BUILD IT! :-)

DISCLAIMER: this guide is meant to be used on Debian/Ubuntu and their derived distros. Probably can work also on other distro, but I haven't tested so can't assure this. Oh and yeah, the usual things, I'm not responsible...blah blah blah...thermonuclear war, cows on trees, and so on, you know... ;-p


First, let's install some tools we are going to use (just the first time):

  • Install Java JDK, Git & unzip

JDK (Java Development Kit) is necessary to build Android apps, and we're going to use the headless version which is a smaller and lighter version. Git is a program used to sync the app repos with your PC. And unzip, which should be already installed, will help us unpack the Android SDK zip file.

$ sudo apt-get install openjdk-8-jdk-headless git unzip
  • Download Android SDK

Android SDK is required for apps to get their dependencies. Again we don't need the whole Android Studio program, but just the command line tools. The link in the below command is the current version, but to check if a newer version is available go HERE.

$ wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip -O sdk.zip

Wget is a common and useful utility to download everything. The -O option, followed by a name, defines the output filename.

  • Extract it
$ unzip sdk.zip -d android_sdk/
  • Accept its licenses
$ cd android_sdk/tools/bin/
$ ./sdkmanager --licenses

...and say YES to every questions.

  • Last touch: To make things work, we need to set up ANDROID_HOME environment variable. To do that add this line to end of your .bashrc file
export ANDROID_HOME=/home/<username>/<pathto>/android_sdk

Then run this command to reload the changes

$ source .bashrc

".bashrc" is a file placed in you user home folder, which contains some variables, some aliases used by the system.


Once we're done with that, let's set up your signature! You just need to do it the first time.

  • Create Keystore
$ keytool -genkey -v -keystore YOURALIAS.keystore -keyalg RSA -keysize 4096 -validity 10000 -alias YOURALIAS

This command should give you a warning, let's fix it with the next command!

  • Move keystore to PKCS12
$ keytool -importkeystore -srckeystore YOURALIAS.keystore -destkeystore YOURALIAS.keystore -deststoretype pkcs12
  • Now you have created a keystore file. Keep it safe and remember the password. You will need it to sign the apps you build in future

Build your first app:

  • Clone the app repo (for example Tusky)
$ git clone https://github.com/tuskyapp/Tusky.git
  • Moving to the cloned folder
$ cd Tusky
  • Build!
$ ./gradlew build
  • Wait

    zzZzzZzz about 10 minutes...but depends how much powerful is your pc!

  • You don't want to get all those bad warning while installing the app, right? So let's sign your newly built app! It should in a path like this:

$ cd app/build/outputs/apk/<flavor>/release/
$ /home/<username>/<pathto>/android_sdk/build-tools/28.0.3/apksigner sign --ks <pathto>/YOURALIAS.keystore --ks-key-alias YOURALIAS FILENAME.apk

and insert your password.

And you're done! Move your built and signed app to your phone, and install it! If you've already installed the same app, for example from F-Droid, you should uninstall that first.


Extra step

Add an alias for apksigner at the end of your .bashrc file, in order to avoid to write all that string above, every time you sign something:

alias apksigner=/home/<username>/<pathto>/android_sdk/build-tools/28.0.3/apksigner sign --ks <pathto>/YOURALIAS.keystore --ks-key-alias YOURALIAS

And again launch:

$ source .bashrc

After this you can sign your apps while in any directory with just this command

$ apksigner FILENAME.apk

Note

If the app you want to build supports it (for example Fedilab https://gitlab.com/tom79/mastalab.git), you can cut down the building time by launching this command:

$ ./gradlew assembleFdroid

This option will build just the FDroid flavor, and not all the other variants.


Big thanks to Kasun who is the real author of this guide (I just added some text here and there).