Great Way to Use Android Gradle with Plugins

Burak Demir
turkcell
3 min readJan 27, 2022

--

We admit that Gradle is intimidating for most developers. If we get a gradle-side error then there is a possibility that day will be our cursed day. There is a way to deal with this elephant, and some developers are doing it very well. Especially the Groovy language used in Gradle and the lack of IDE support increase the problem.

Kotlin & buildSrc Module as a Savior

Newer Kotlin DSL support provides a pleasant way to deal with Gradle. With Kotlin DSL support enabled us to use IDE and Kotlin features that made gradle pretty cool.

I researched the buildSrc folder to find out how to further improve the build processes.

Gradle files get longer over time, and also become more and more complex. Projects using multi modules may end up with boilerplate in their Gradle files. We want to try different way of making Gradle useful.

Let’s move on to practice

To keep the simple, Let’s see on Helloworld that startup project. Once you understand the logic, you can experience it in all your projects.

Let’s see how the gradle file looks at startup.

With this approach, we reduced it to 3 lines of code. The larger the gradle file, the effect will be greater.

Yes this is our final app gradle file.

Step 1: Enable the Gradle DSL Plugin

Create a folder named buildSrc In your project-level root folder. After that create a file inside buildSrc naming it build.gradle.kts open newly created file to add below code for enable the Kotlin-DSL plugin.

Sync now and Kotlin DSL is enabled in our project.

Step 2: Implement Your Custom Gradle Plugin

Before implement your custom gradle plugin class, i created BaseGradlePlugin class that extends Plugin<Project>.

I included extensions in the BaseGradlePlugin because I couldn’t access it directly some reason. Anyway that looks like this.

After that implement custom plugin named KtsAppModulePlugin class. We have override function that apply() called when plugin will be applied to your project. However, a question comes to mind here.

Which id will be used to add this plugin to our project?

Create a file like

buildSrc/src/main/resources/META-INF/gradle-plugins/com.demir.gradleKts.properties

Because my custom plugin class is in the folder called plugins I told it where to find it.

Step 3: Move your app gradle code into your Plugin class

configureAndroid() -> android {} block that extension function derived from the our BaseGradlePlugin classs. Also we can still override this property by calling the android block in own app gradle file.

configureDependencies() -> dependencies {} the block that will add our dependencies.

configurePlugins() -> We need to add the existing application plugins into our gradle file before our own plugin. But rather than adding plugins into app gradle file we can include it in our own plugin. That’s exactly what we’re doing here.

Step 4: Apply Your Custom Plugin Into App Gradle File

Now we can give our own custom plugin to our app gradle. So we have a much more manageable structure when we want to change or create new plugins. It will facilitate management especially in multi-module architectures

In the last case, if we look at our app gradle file again.

That’s all

Until next time, manage easily your gradle with plugins.

Do you think is it a good way to use a plugins to manage with this method? If you used a different method like this situation, I would be glad to see it in the comment section.

--

--