How To Build a Gradle Plugin for Android in Kotlin

Mark Dappollone
The Startup
Published in
3 min readMay 12, 2020

Building Gradle plugins isn’t new, and Kotlin isn’t new, and Android isn’t new. By the same token, building Gradle plugins in Kotlin isn’t new, and neither is building Gradle Plugins for Android apps. But building Gradle plugins, in Kotlin, for Android? Well, there’s really just not that much information out there on that.

Before we get started, I’ll just mention that writing Android Gradle plugins is, in many ways, much easier if you write in Groovy. But what we wanted to do was easy, we wouldn’t be talking about it. So let’s just stick to the plan.

Ignore him.

The Android Part

So there are lots of resources out there on building plugins in Kotlin, and many are great, so if you’re just starting from scratch, maybe go and google that before continuing, because we’re going to skip right to the good stuff: How do you get access to the Android junk in inside your Kotlin Gradle plugin? Glad you asked.

Welcome…

First of all, what do we mean by “Android junk?” Well, when your gradle build script runs to build your Android app, the android application plugin is going to add a bunch of tasks to your build, and you’re going to configure those tasks in the android closure that the plugin uses for configuration. Your android closure in your build.gradle file might look something like this:

And if you’re creating your own plugin, you might want to know some of the stuff in that closure, or other stuff the AGP is doing. For groovy, this is all pretty easy and straightforward, because groovy doesn’t require you to explicitly import anything. But with Kotlin, you can’t get away with that. So, the first thing you need to do, is add a dependency in your Gradle plugin’s build.gradle file on the Android Gradle Plugin. So, in your plugin’s build.gradle script, you’ll need:

dependencies {
implementation gradleApi()
implementation 'com.squareup:javapoet:1.10.0'
implementation 'com.squareup:kotlinpoet:1.0.0-RC1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compileOnly("com.android.tools.build:gradle:3.6.3")
}

The key here is the last line. We’re using compileOnly, since, at runtime, this dependency will be provided by the build.script of the Android application that applies your plugin, in its build script. All we need is for your plugin to be aware of the AGP, so we can use the APIs it provides.

The Kotlin Part

Now that the dependency is included, our plugin can finally get access to that sweet Android junk:

If you want to get access to the android closure in your plugin, first you’ll need to get an instance of the build’s AppExtension. Do this using the line:

val appExtension = project.extensions.findByType(AppExtension::class.java)

Once you have the AppExtension instance, you can do all kinds of stuff, like interrogate the app variants, add tasks into the Android build lifecycle using the dependsOn() function, and even, as suggested by the example above, generate new source files and compile them into your project.

Writing an Android Gradle Plugin in Kotlin isn’t too difficult, once you know the tricks of the trade. For the complete source of the project all this came from, head over to https://github.com/Comcast/resourceprovider2 to see how we did it.

And if you haven’t already, also check out my article on The New ResourceProvider.

--

--