In my previous post I went over some basic terms used by Gradle and the Android build system, and over the flow of the build. With these understandings at hand, we are now ready to go over concrete steps and set up our build server, without Android Studio, or in fact without any GUI tools at all.

For the sake of this post I will assume you are setting up your new build environment on a Linux server, and you don’t have the Linux GUI at your service, only the command line. Of course, it can all be set up on a Windows or Mac machine just as well.

Let’s also assume that your user on the build machine is called “user”, and therefore your typical home directory would be /home/user.

Now let’s go step by step:

1. Download and install the JDK

First download the JDK you work with. I have downloaded JDK7 from Oracle’s site. If your Linux environment doesn’t support rpm – you will need to download the tar.gz version and extract it, for example to /home/user/.

2. Download and install Gradle

You can download Gradle’s latest release from here. The binary distribution will do, no need for the complete distribution. I chose to use Gradle 2.4.

By the way, There is the option of avoiding the explicit installation of Gradle if you are using the Gradle Wrapper, but let’s leave this topic aside. Personally I think that if you are setting up your build server, the better approach is to explicitly download the Gradle version you want. Once downloaded, extract the zip file, for example under /home/user.

3. Download and install the Android SDK

Download the latest Android SDK. You will need the “SDK Tools Only” option. You can find it here.

Extract the SDK.

4. Set up the environment

You will now need to set up some environment variables and set your path correctly. You can typically do that by editing your .bash_profile file in your home directory (or create one if missing), and define something similar to the following:

/home/user/.bash_profile:

export JAVA_HOME=/home/user/build/jdk1.7.0_79
export GRADLE_HOME=/home/user/build/gradle-2.4
export ANDROID_HOME=/home/user/android-sdk-linux
PATH="$GRADLE_HOME/bin:$PATH"

Of course you will set it up according to the location of the different tools as you installed them on your machine.

5. Update your Android SDK

As you may know, downloading the Android SDK is just the first step in using the Android SDK; you then typically need to launch the Android SDK Manager and download additional packages. This is also possible from the Linux command shell by changing directories into the tools directory under your Android SDK and executing the android command, similar to the following:

cd $ANDROID_HOME/tools
./android list sdk –all

The above command will list all available SDKs in a numbered list. Now you need to pick the SDKs you want to install. Let’s assume you need build-tools revision 22.0.1 (assuming that’s what you defined in your build.gradle using buildToolsVersion), and let’s assume the output of the ./android command listed it as:

5- Android SDK Build-tools, revision 22.0.1

You will then need to run the following command to download this build-tools version:

./android update sdk -u -a -t 5

Where ‘5’ is the number denoting the specific build-tools you wanted.

You will probably need additional packages, like:

  • – Android support repository
  • – Google repository (if you’re using Google Play services)

therefore download them too using the above command.

Anyway don’t worry. If you miss a package – your build will fail… and based on the error you’ll be able to figure out which additional packages you need.

6. Let’s bring up the source code…

Now fetch your Android project (with all its modules) from your source control server.

Let’s assume you’ve done that, and you now have your root project set up under /home/user/myapp.

Note that together with your source will come the various Gradle files – multiple build.gradle files, settings.gradle, gradle.properties, and that’s fine. These files need to be saved on your source control system, as they are correct and relevant to all environments and all the developers who use the source code.

(You may use gradle.properties to set up some JVM arguments. Take a look here.)

7. Let the party begin!

OK, you should be ready to go. Just cd into the directory of your root project, and run your build by calling one of Android’s tasks. For example:

gradle assembleDebug

“assembleDebug” would typically build the “debug” build-type of your app. If you have Product Flavors, you will need to call the task with the flavor name included. For example, if your flavor is called “trial”, you can build it by running:

gradle assembleTrialDebug

You could also call the “assemble” task – it will assemble all your flavors and build-types:

gradle assemble

If all goes well, you should now have a ready apk under your app module’s build/outputs directory.

That’s all for now guys. I hope I was able to shed some light on some of the processes going on in the Android build system, and helped you guys who wanted to set up a build system independent of the Android Studio IDE.

Happy building!