Signup/Sign In

Introduction to Gradle for Android Studio

In Android Studio, Gradle is used for building our android application projects, hence playing the role of a build system. Before Android Studio, in Eclipse we used to compile and build the applications using command line tool which was soon taken over by GUI based steps to build and run Android Applications in eclipse using ANT. Every android application development tool has to compile resources, java source code, external libraries and combine them into a final APK.

Gradle is a build system, which is responsible for code compilation, testing, deployment and conversion of the code into .dex files and hence running the app on the device.

As Android Studio comes with Gradle system pre-installed, there is no need to install additional runtime softwares to build our project. Whenever you click on Run button in android studio, a gradle task automatically triggers and starts building the project and after gradle completes its task, app starts running in AVD or in the connected device.

A build system like Gradle is not a compiler, linker etc, but it controls and supervises the operation of compilation, linking of files, running test cases, and eventually bundling the code into an apk file for your Android Application.

There are two build.gradle files for every android studio project of which, one is for application and other is for project level(module level) build files.

The build process works as shown in the below diagram.

Conversion of Android Code to APK with Gradle


In the build process, the compiler takes the source code, resources, external libraries JAR files and AndroidManifest.xml(which contains the meta-data about the application) and convert them into .dex(Dalvik Executable files) files, which includes bytecode. That bytecode is supported by all android devices to run your app. Then APK Manager combines the .dex files and all other resources into single apk file. APK Packager signs debug or release apk using respective debug or release keystore.

Debug apk is generally used for testing purpose or we can say that it is used at development stage only. When your app is complete with desired features and you are ready to publish your application for external use then you require a Release apk signed using a release keystore.

Now lets shed some light on the gradle files.

setting.gradle

The setting.gradle(Gradle setting) file is used to specify all the modules used in your app.

build.gradle (project level)

The Top level (module) build.gradle file is project level build file, which defines build configurations at project level. This file applies configurations to all the modules in android application project.

build.gradle (application level)

The Application level build.gradle file is located in each module of the android project. This file includes your package name as applicationID, version name(apk version), version code, minimum and target sdk for a specific application module. When you are including external libraries(not the jar files) then you need to mention it in the app level gradle file to include them in your project as dependencies of the application.

Note: If a certain application is developed in variations for individual modules like, Smart Phone, Tablet or TV then separate gradle files must to be created for all.

You can even start your gradle system through command line tool. Following commands are used for it:

  • ./gradlew build - (build project)
  • ./gradlew clean build - (build project complete scratch)
  • ./gradlew clean build - (run the test)
  • ./gradlew wrapper - (to see all the available tasks)

Android Build Process


ART - Android Runtime

The Dalvik Virtual Machine is dead. Yes, Google stopped using it in 2014, although you will find most of the Android tutorials online, still not updated, but please be informed that Dalvik Virtual Machine is not used in Android anymore.

The new runtime is known as ART or Android Runtime which is very well compatible with its predecessor Dalvik, but do comes in with a lot of new features like:

  • Ahead-of-Time compilation
  • Improved Garbage collection
  • Improved Debugging and diagnostics.