Creating your own plugins
If you have a collection of Gradle tasks that you want to reuse in several projects, it makes sense to extract these tasks into a custom plugin. This makes it possible to reuse build logic yourself, and to share it with others.
Plugins can be written in Groovy, but also in other languages that make use of the JVM, such as Java and Scala. In fact, big parts of the Android plugin for Gradle are written in Java in combination with Groovy.
Creating a simple plugin
To extract build logic that is already stored in your build configuration file, you can create a plugin within the build.gradle
file. This is the easiest way to get started with custom plugins.
To create a plugin, create a new class that implements the Plugin
interface. We will use the code we wrote previously in this chapter, which dynamically creates run
tasks. Our plugin class looks like this:
class RunPlugin implements Plugin<Project> { void apply(Project project) { project.android.applicationVariants...