Back to Blog Home
← all posts

Lightning Fast Android Builds with Gradle

September 16, 2015 — by Todor Totev

NativeScript 1.3 introduces two significant improvements to your development workflow for targeting iOS and Android:

  1. Integration with CocoaPods for iOS development

  2. Switch to Gradle for developing {N} projects when targeting Android

In this blog post, I’ll be covering our switch to Gradle when targeting Android and how to update your development environment to support this change.

In previous versions of NativeScript, the CLI used Apache Ant for to build your projects when targeting Android. We felt we needed to switch to something else because we were finding Ant to be a bit cumbersome and lacked the flexibility we wanted. The fact that Android Studio was dropping support for Ant also compelled us to explore other options. We looked around and ultimately decided on Gradle.

The Gradle integration is a huge step for the {N} project and provides a number of advantages over Ant. I’d recommend reading Benjamin Muschko’s article that compares Gradle against Ant/Maven in great detail. For developers targeting NativeScript 1.3, the switch to Gradle improves build times. In NativeScript 1.2, building the “Hello World” project with Ant took 24s on my machine. With Gradle and NativeScript 1.3, we got incremental builds out of the box and we can that same project builds in about 5s, a near 5x performance improvement. Gradle also offers seamless integration with current and future versions of native libraries.

Now your code can consume Google and other vendor libraries which depend on it. Last but not least, our Telerik UI for NativeScript will come with its Android native bits packed in Gradle-friendly AAR format.

Such a huge change moved some stuff around. To work comfortable with our new release, you need to check and adjust the configuration of your machine. First of all, update your Android SDK. Fire up the Android SDK Manager (tip: just type `android` in the command line), make sure to check everything off the list below. Keep in mind the specific versions, they are important:

  • Install SDK 21 or 22

  • Install Android SDK Build-tools 22.0.0 or later

  • Install Android Support Repository

  • Install Android Support Library 21.0.0

  • Check that the ANDROID_HOME environment variable is correctly set to the root of your Android SDK installation. For Windows, go to Start Menu/Screen and search for `environment`, then select "Edit environment variables for your account". For Mac and Linux, edit your ~/.bash_profile file and add `export ANDROID_HOME=<full path to Android SDK root directory>`

  • Download the Gradle binary distribution and extract it.) and extract it. Add its bin directory to your PATH. Or if you are using Mac and Homebrew, simply do `brew install gradle`. For Windows users, we recommend Chocolatey: `choco install gradle`. Yes that’s right you need to brew your own chocolatey if you want to be cross-platform :)

  • Install Java Development Kit (JDK) - note that JRE is no longer sufficient. Also, make sure to set JAVA_HOME to its root directory and add its bin directory to PATH.

  • If you changed your environment variables, restart your working console to pick up the changes.

Quite many settings to keep in mind, right? We've thought of that too, and our helpful doctor command (tip: run `tns doctor`) verifies your machine configuration and tells you what changes you need.

If you, like me, enjoy working in the command line, this one-liner should help you configure Android SDK (and it will add some Android Emulator images to help you develop easier):

echo yes | android update sdk --filter
tools,platform-tools,android-22,android-17,build-tools-22.0.1,sys-img-x86-android-22,extra-android-m2repository,extra-google-m2repository,extra-android-support
--all --no-ui

Mac users who use Homebrew, you should run the following to set your environment correctly:

echo "export JAVA_HOME=$(/usr/libexec/java_home)" >>
~/.bash_profile
echo "export ANDROID_HOME=/usr/local/opt/android-sdk" >>
~/.bash_profile

Finally, an important notice. Starting with the 1.3 release, you can no longer create Ant-based projects. We made sure that building and developing your existing projects work fine in this release. But from our 1.5 release onward (early November 2015), we plan to entirely remove the support for Ant-based projects. We feel that with Google and other vendors moving to Android Studio and Gradle, this is the path forward. Yet, we want you to have plenty of time to update your existing code to the new tooling.

To upgrade your projects, please refer to our online docs. If you are developing plugins with Android native code which uses Eclipse project, you have more work to do. You need to import your Eclipse project into Android Studio and migrate it to Gradle based build producing AAR files. After that, you must change the plugin to include the generated aar file. Finally, in projects which depend on your plugin, remove the old and add the new one by using `tns plugin remove <your plugin> && tns plugin add <your plugin>`.

We hope that you'll enjoy using the {N} CLI 1.3 as much as we enjoyed creating it. Have wonderful time building the best apps around!