Gradle Plugin Tutorial for Android: Getting Started

Learn how to create a Gradle plugin within your existing Android app, or as a standalone project that you can publish and use in any Android project. By Bhavesh Misri.

Leave a rating/review
Download materials
Save for later
Share

Wouldn’t it be awesome if — by typing just one command or clicking a button — Android Studio automatically built a signed APK for you, uploaded it to Google Play Store, and updated you on your favorite platform? While you could achieve all of this by writing custom tasks for each function, what if you want to achieve the same features among multiple modules or projects? This is where writing a custom Gradle plugin comes in handy.

In this tutorial, you’ll be writing your own Gradle plugin. This plugin will change the name of the APK, move the APK to your desired location, and create a text file in which it’ll add all the dependencies for all the APKs you’ve created through the plugin.

In the process, you’ll learn about the following topics:

  • Gradle tasks
  • Gradle plugins
  • Different ways to package a plugin
  • Publishing a plugin locally

For an introduction to the Gradle build system, check out Gradle Tutorial for Android: Getting Started.

Note: This tutorial assumes you’re familiar with Kotlin and Android development. If you’re a beginner, check out our Beginning Android Development with Kotlin tutorial first.

For an introduction to the Gradle build system, check out Gradle Tutorial for Android: Getting Started.

Getting Started

Download the materials using the Download Materials button at the top or the bottom of this page. Extract and open the starter project within the directory named ProjectTracker in Android Studio.

Build and run. You’ll see something like this:

ProjectTracker app

This is a pretty simple and straightforward app. When a user enters anything in EditText and taps the Enter Value Button, the text will appear on the screen. That’s it!

You don’t have to worry about what’s happening in the app, as you’ll be working in the buildSrc directory and with the build.gradle files.

But before you jump into the code and get your hands dirty, it’s important to understand what Gradle is and how you can create plugins with it.

What Is Gradle?

Gradle is an open-source build automation system that helps you manipulate the build process and its logic. For example, when you build an app, it’s Gradle that compiles all the code and creates an APK for you. In this tutorial, you’ll manipulate this build process and customize it to your needs.

Gradle Tasks

Gradle has tasks, and each one of them represents a single atomic piece of work for a build. You can either create a custom task, or you can create a plugin that can consist of multiple tasks.

To see the list of all the tasks, run ./gradlew tasks in the terminal from the project’s root directory. This will print all the tasks:

Gradle tasks

You can write your own task by going into the module-level build.gradle file. This is the one found inside the app directory. Open it and add the following after plugins block:

task printSomething() {
  doLast {
    println("Welcome to gradle world!")   
  }
}

To execute the task you created, in the terminal, run ./gradlew -q printSomething. The -q command-line option suppresses Gradle’s log messages so that only the output of the tasks is shown:

$ ./gradlew -q printSomething
Welcome to gradle world!

The doLast method is important here, as it tells Gradle to only execute the action when the task is called. Without it, you’ll be executing the action at the configuration time on every build. In fact, there’s also a doFirst method that can be overridden. So, if you have:

task printSomething() {
  println("Before doFirst!")

  doFirst {
    println("In doFirst!")   
  }

  println("In between doFirst and doLast!")

  doLast {
    println("In doLast!")   
  }

  print("After doLast!")
}

Then after running ./gradlew printSomething, the output would be:

$ ./gradlew printSomething

> Configure project :
Before doFirst!
In between doFirst and doLast!
After doLast!
> Task :printSomething
In doFirst!
In doLast!

BUILD SUCCESSFUL in 8s
1 actionable task: 1 executed

The benefit of having doFirst and doLast is it allows for extending tasks and performing functionality at specific parts of the task’s lifecycle.

Packaging the Plugin

So far, you know what Gradle is, what Gradle tasks are, and how to see all the Gradle tasks. You also created your own task and executed it. The next thing on the list is the Gradle plugin. A Gradle plugin is nothing more than a combination of tasks that you’d like to execute together. There are a few ways you can write your Gradle plugin, so this next section will go through each of them.

Build script
Put the source of the plugin directly into the build.gradle file. One big advantage of this approach is that the class is automatically compiled and added to the classpath of the build script without you configuring anything. The disadvantage is that the plugin cannot be used in another module.

buildSrc project
Another approach is to place the source of the plugin in the rootProjectDir/buildSrc/src/main/java directory. The benefit of doing this is that the plugin is usable throughout the project and not confined to one module. However, you need to add a few more files to make it work with this approach, and the plugin isn’t usable outside the project.

Standalone project
You can also create a separate project for the plugin. The benefit is that after building the project, a JAR file is generated, and this may be used in any of your Android Studio projects. The disadvantage of this approach is that you need to use a separate IDE like IntelliJ — and not Android Studio — to create a Gradle plugin project.

In these examples, you’ll start with the plugin in the build script, which will keep things simple. Then you’ll look at creating a buildSrc project and finally a standalone project.

Creating a Plugin in the Build Script

Delete the printSomething task you created earlier, as you’ll be creating a plugin that will perform the same task.

To create a Gradle plugin in the build script, add the following lines at the end of the build.gradle file:

class PrintClass implements Plugin<Project> {
  @Override
  void apply(Project project) {
    project.task("printSomething") {
      doLast {
        println("Welcome from gradle world!")
      }
    }
  }
}

You’re creating a class that implements the Plugin class, which is a generic class, and it takes Project as a type parameter. This will call the plugin class’s apply method, and the method will have the Project type as a parameter. You can use this to configure the project however you need to. Inside apply, write your task using the project parameter.

Your plugin is ready, but you haven’t yet applied it to your project. For that, write the following at the end of your build.gradle file:

apply plugin: PrintClass

Now you can execute ./gradlew -q printSomething in the terminal and see the same output.

Congrats! You created your first plugin, and you can even call the task from the terminal :]. But this plugin only prints a sentence, and it doesn’t configure your project in any way. On top of that, you can only use it in one module. Undo the changes made in this section and read ahead to learn how to create a plugin using the buildSrc directory.