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
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Publishing the Plugin in a Local Directory

Your plugin is now ready! To publish it to your local directory, open the build.gradle file in your IntelliJ project and add the following:

uploadArchives {
  repositories.mavenDeployer {
    repository(url: uri('pluginOutput'))
  }
}

After adding the code, sync the Gradle changes again. This task will publish your plugin to your desired location. You can execute the task by either clicking on the green play button next to the task or executing the command ./gradlew -q uploadArchives in the terminal. Build and run and you’ll see the plugin directory in your desired location.

Next, you’ll use it in your Android Studio project. Fire up the ProjectTracker project again, and open the project-level build.gradle file. Inside repositories within the buildscript block, add:

maven {
  url uri('path/to/standalone/plugin/project')
}

And then in dependencies task add:

classpath group: 'com.raywenderlich', name: 'ProjectTrackerPlugin', version: '1.0.0'

Now, your project has access to the plugin files. To apply the plugin, open the module-level build.gradle file and add the following lines at the bottom of your Gradle file:

import com.raywenderlich.plugin.SaveDependency
apply plugin: SaveDependency

Finally, you can call the task in your module-level build.gradle file by adding the following at the bottom of the same file:

writeModuleDependencies {
  configuration = ["implementation"]
}

Now run ./gradlew clean to clean Gradle, and then run ./gradlew -q writeModuleDependencies and enjoy your plugin!

Party Android

Where to Go From Here?

You can download the final project using the Download Materials button at the top or bottom of this tutorial.

Phew! That was a lot of stuff you covered in this tutorial. Now, you can create your own Gradle plugin and use it in different projects.

And there’s still more to explore! To learn more about the Gradle plugin and what else you can do with it, check out Gradle’s official documentation.

If you’re new to the concept of Gradle and don’t know where to start, check out our Gradle Tutorial for Android: Getting Started.

If this tutorial helped you and you ended up creating an awesome plugin that you want to share with everyone, you can publish it on the Gradle plugin portal by following the official documentation on Publishing Plugins to the Gradle Plugin Portal.

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!