Posted on

Android Studio Gradle Plugin pt1


android studio gradle plugin

Designed by Freepik

In the previous post I created simple Android Studio plugin which displays quote of the day on IDE launch. Those are more single user use and not declared anywhere in project, which means that they’re not automatically installed for other users. Today let’s look at creating Gradle plugin.

If you’re not familiar with what Gradle plugin is or want to read more about Gradle – check this post as well.

Setup

First of all you’ll need IDEA Community. And install Groovy. Then create new Gradle project with Groovy selected

android studio gradle plugin

android studio gradle plugin

Then create this kind of hierarchy: src/main/groovy/com/myhexaville/demoplugin,  src/main/resources

android studio gradle plugin

In build.gradle add

Plugin

In groovy/com.my… create new Groovy class

and create new file in resources/META-INF/gradle-plugins called com.myhexaville.demoplugin.properties with one line inside

android studio gradle plugin

The name of this file is important, it’s used in apply plugin line. This one will need this line in our Android project

If you name your file demo.properties, then you’ll need

Build

Those lines in build.gradle that we added gave us uploadArchives task

android studio gradle plugin

run it and you’ll get repo directory generated

android studio gradle plugin

Now you might get an error in compileGroovy task and not getting those files without any error message to track the issue.

android studio gradle plugin

Create a simple Groovy class with print method and run it, you’ll get a better error message

android studio gradle plugin

Which means that I need to replace dependency line build.gradle file to this

sync project and everything will work.

Add To Android Project

Now let’s copy this repo directory to any Android project you want, right into the root directory. And add those lines to project’s build.gradle file

repo line is name of directory. And classpath consists of three parts : com.myhexaville – which is name of group in our plugin’s build.gradle file. demoplugin  – name of artifact, which we declared while creating project, it’s in settings.gradle. And 1.0 which is a version

As you see it has nothing to do with packages inside src directory. Then add this line to module’s build.gradle

This is a name of .properties file, one again. Now when you call build Gradle task from Gradle toolbar you’ll see Hello message first in Run tab.

 

That’s it for now, don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you thing they will benefit from it!