The Startup
Published in

The Startup

Gradle for Android

Must-know facts about Gradle files in android studio


Gradle is more of a toolbox than a subject to learn completely. In this article, we will look at some common uses of Gradle in android.

First lets look at some basic Gradle overview like creating a task, importing a file into android studio and running it, uses of plugin, etc. Then we will look at common uses of Gradle in android.

Gradle Overview:

  1. files created when I import a file into android studio

new->import project-> file with custom tasks

.gradle dir.:- where gradle stores information for incremental build support- so it has tasks inputs and outputs
idea dir.:- gradle stores its model of your project
build dir.:- this is where build outputs go, if generated any
gradle dir.:- wrapper jar and wrapper properties are stored

root folder- has the build.gradle file with all the tasks
- has .iml file which is android studios project model
- gradlew & gradlew.bat - are wrapper scripts
- file- this is how android studio can figure out where android sdk is on your machine

2.The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

Applying the Application plugin also implicitly applies the Java plugin. The main source set is effectively the “application”.

Applying the Application plugin also implicitly applies the Distribution plugin. A main distribution is created that packages up the application, including code dependencies and generated start scripts.

To use the application plugin, include the following in your build script:

build.gradle file: plugins {
id 'application' // fetched from jcenter() repository

Common uses of Gradle in android

The android plugin can handle a huge range of use cases, but also has a sensible enough defaults to make simple builds straight forward. In addition to handling the actual business of handling and packaging your app, android plugin provides a bunch of different configuration and customization options for us to mess with.
Of these available capabilities we’ll be covering main things android developers will commonly wanting to do in their Gradle android build.

1. build variants - It is used to build slightly different versions of the app such as debug version, release version, paid version, free version. Android plugin makes it easy to share code and other common resources between these variants. Checkout the article for details about building an app with multiple flavors.

2. Android plugin allows us to manage dependencies on per variant basis. Check out the topic: Per-variant dependency Configuration in the article here.

3. Android plugin can rewrite resources and manifest entries on per variant bases: Check out topic: Configuration Generated Tasks in the article here.

4. key signing: Gradle generates a key when we build and deploy the app. In debug flavor Gradle signs a key automatically, but when we are publishing the released flavor of the app we need to create a key store and sign the apk.

Signed apk is required by the google play store when we publish the app. This helps google maintain security by making sure no changes are made to the app after the apk is signed, users know that the app is from the publisher and fraud/ third party intrusion can be controlled.

5. Progarding: The Android plugin can help reduce the size of your app by stripping out unused code and resources. It does this with the help of a tool called Proguard.

Proguard can do a lot of interesting stuff, including obfuscating your code by
giving all your classes and methods meaningless names. That’s mostly of
interest if you’re really worried about your code being reverse-engineered. In this project we’ve configured two new build types, one of which has turned on
minification(minification is the process of removing unnecessary elements and rewriting code to reduce file size) and resource shrinking, and one of which has not. There’s a lot more you can do to optimize your APK using Proguard. The following resources are a good place to start.

Check out Udacity’s video:

6. Testing: Testing are of 2 types- unit tests and instrumentation tests. former are performed on the JVM and are hence faster, but less precise. Later are done on the android device hence slower, but more precise. Gradle can help create different flavors of the app or libraries to be reused. Hence each module or flavour need to have testing. For example, when I have free and paid flavours of the app, I will have test free, AndroidTestFree, testPaid, and AndroidTestPaid folders to create appropriate test. checkout a short video here.

7. Multidex Support: We can have hundreds of classes each with hundreds of methods. There’s nothing stopping a large project from getting this big, especially if you’ve got a lot of big dependencies. When compiled we will get an error like: trouble writing output: Too many method references: 70936; max is 65536.

Reason being Android virtual machine doesn’t actually run Java byte code, it runs Dalvik byte code, and there’s a build step after Java compilation where the Java byte code is turned into Dalvik byte code. This step is called Dexing. Part of this process is compiling a table of every method in the application, which is indexed with two bytes. That means we’re limited to 65k methods.

Fortunately, we can ask Gradle to simply break up this table into multiple
tables simply by setting multiDexEnabled true. If we add that line to defaultConfig in build.gradle of the module and compile again, error is gone.

src: Udacity

8. Multi-Project Builds and Creating a Library: We can come across situation where the app needs to have multiple projects like when an app might have multiple games/ apps inbuilt and we need to reuse the code in different parts of the app. To avoid boiler plate code we can use libraries. So basically our app will have a root directory and all modules are under it. Each module is independent of the other. If we want to use a module we implement its dependency it in the build.gradle file of the module that wants to use the other module. Checkout the GitHub code to understand it better and check out the documentation to see how to create a module.

Multi-Project Builds

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. JAR file can be used by other projects that are not android or java as well, but AAR files can only be used by android projects and they have their own build.gradle and manifest files. You can find more information about gradle multi-project builds here.


This article only talks high level about each topic. However links are provided for future reading. Gradle understanding will help us build our projects in a efficient way.

This knowledge is what I learnt in my course at Udacity.






Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Android: Best practice for RecyclerView with HeaderView

AsyncTask is deprecated, now what? —  part 2

How to force the Unreal Engine Android project to use AndroidX?

Open PDF file above Android Oreo

A/B Testing In Android

OnCollisionEnter Vs. OnTriggerEnter — When to use them?

Cool Kotlin 1.4 new features!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nikitha Gullapalli

Nikitha Gullapalli

Mobile app developer

More from Medium

Should Kotlin be the preferred choice for Android App Development?

What is a Google Android Instant App and why you should use it as a developer?

Google Instant App — Why should you use it?

How I learned to automate Android apps with Android Espresso

cloud based Espresso testing

The 6 Factors You Should Consider Before Hiring an Android App Developer

Before Hiring an Android App Developer