Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Using Gradle In Your Command Line Android Project

TwitterFacebookRedditLinkedInHacker News

I’ve been developing mobile Android applications since 2011 and up until now I’ve been using nothing but Apache Ant for building them.

Now you’re probably like, it is 2015, why aren’t you using Gradle like the rest of the world? Well, I like Apache Ant, and I had no real reason to switch. However, I’ve decided this is the year I jump ship.

If you’re like me, a no IDE rebel coming from an Apache Ant world for Android development, this might help you out in making the switch to Gradle.

As a refresher, you’re probably familiar with creating your new Android applications from a terminal or command prompt like this:

android create project --activity [activityname] --package [packagename] --target [androidtarget] --path [projectpath]

Creating a new Android project with the Gradle template isn’t too different. We’ll be including the --gradle and --gradle-version tags in our create command.

So for example our android create project command will look like the following now:

android create project --activity [activityname] --package [packagename] --target [androidtarget] --path [projectpath] --gradle --gradle-version [gradleversion]

Time to look at a few other differences in our process. Navigate into the freshly created project using your terminal or command prompt because we’re going to try to build the project.

Coming from an Ant world, you’re pretty familiar that ant debug will compile and create a debug version of our binary file in the bin directory of our project. Instead we are going to run ./gradlew assemble from a terminal or ./gradlew.bat assemble from a command prompt. This will create a debug version of our binary file in the build/outputs/apk directory of our project.

This is where we can make our project better for collaboration. Instead of downloading a bunch of external libraries to include in your project, you can add them as dependencies in your build.gradle file:

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.firebase:firebase-client-android:2.2.3+'
}

The above will include the Firebase SDK in your project when you run ./gradlew assemble.

Let’s say you’re now ready to build your project for release. In other words you are ready to sign your APK file to be uploaded to Google Play.

First make sure you have your signing keystore created. If you’re unfamiliar with this, visit my previous tutorial on the topic. Go ahead and add the keystore to the root of your project and add the following to your gradle.properties file:

gradle.taskGraph.whenReady { taskGraph ->
    if(taskGraph.hasTask(':assembleRelease')) {
        android.signingConfigs.release.storePassword = new String(System.console().readPassword("\n\$ Enter keystore password: "))
        android.signingConfigs.release.keyPassword = new String(System.console().readPassword("\n\$ Enter key password: "))
    }
}

android {
    ...

    signingConfigs {
        release {
            storeFile file("release.keystore")
            storePassword ''
            keyAlias "keystore_alias_name"
            keyPassword ''
        }
        debug {

        }
    }

    buildTypes {
        release {
            runProguard false
            proguardFile getDefaultProguardFile('proguard-android.txt')
            signingConfig signingConfigs.release
        }
        debug {
            debuggable true
        }
    }

    ...
}

Now every time you run ./gradlew assembleRelease you will be prompted for your keystore and key passwords required for signing.

Conclusion

Gradle has far more depth than what I’ve touched upon. However, coming from an Apache Ant world, the information I provided should be enough to get you switched over to Gradle.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.