Gradle plugins
Gradle plugins allows you to
- extend the Gradle model (e.g. add new DSL elements that can be configured)
- configure the project according to conventions (e.g. add new tasks or configure sensible defaults)
- Apply specific configuration (e.g. add organizational repositories or enforce standards)
There are two general types of plugins in Gradle, binary plugins and script plugins.
There are also special binary core plugins which are part of gradle distribution. They provide short names, such as 'java' for the core JavaPlugin.
Most gradle plugins are resolved from public Gradle Plugin Portal.
applying binary plugins
You can apply binary plugin to the target (project/subprojects) within plugin block by its id or by short name for core plugins. Usually plugins are specified at begin of build.gradle file.
plugins {
// «plugin id» - for core plugins
// id(«plugin id») - for core or plugins already available to the build script
// for binary Gradle plugins that need to be resolved
// id(«plugin id») version «plugin version» [apply «false»]
java
kotlin("jvm") version "1.4.10"
id("com.jfrog.bintray") version "0.4.1"
}
plugins {
// for core or plugins already available to the build script
// id «plugin id»
// for binary Gradle plugins that need to be resolved
// id «plugin id» version «plugin version» [apply «false»]
id 'java'
id "org.jetbrains.kotlin.jvm" version "1.4.10"
id 'com.jfrog.bintray' version '0.4.1'
}
If you want to apply plugins in other blocks like allprojects, you must use the apply method. In this case you specify short name or class of plugin.
apply(plugin = "java") // by short name
apply<JavaPlugin>() // by type
apply plugin: 'java' // by short name
apply plugin: JavaPlugin // by type
Binary plugins that have been published as external jar files can be added to a project by adding the plugin to the build script classpath and then applying the plugin.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.jfrog.bintray.gradle:gradle-bintray-plugin:0.4.1")
}
}
apply(plugin = "com.jfrog.bintray")
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.4.1'
}
}
apply plugin: 'com.jfrog.bintray'
applying script plugins
apply(from = "other.gradle.kts")
apply from: 'other.gradle'
plugin management
Not all plugins are available at the Gradle Plugin Portal, for example android plugin. You can use buildscript block, but there is other way.
The pluginManagement block may only appear in either the settings.gradle file, where it must be the first block in the file, or in an initialization script.
// settings.gradle
pluginManagement {
val helloPluginVersion: String by settings
plugins {
// resolve plugin
id("com.example.hello") version "${helloPluginVersion}"
}
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.example") {
useModule("com.example:sample-plugins:1.0.0")
}
// if (requested.id.id == "com.android.application") {
// useModule("com.android.tools.build:gradle:${requested.version}")
// }
}
}
repositories {
gradlePluginPortal()
maven(url = "./maven-repo")
ivy(url = "./ivy-repo")
google()
}
}
// build.gradle.kts
plugins {
id("com.example.hello")
}
// gradle.properties
helloPluginVersion=1.0.0
// settings.gradle
pluginManagement {
plugins {
id 'com.example.hello' version "${helloPluginVersion}"
}
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.example') {
useModule('com.example:sample-plugins:1.0.0')
}
}
}
repositories {
gradlePluginPortal()
maven { url './maven-repo'}
ivy { url './ivy-repo'}
google()
}
}
//build.gradle
plugins {
id 'com.example.hello'
}
//gradle.properties
helloPluginVersion=1.0.0
plugin version
Sometimes version of plugin does not correspond to the gradle version. It may happen when you open old project on IDE with last updates. For example, you can get message like
Gradle sync failed: This version of the
Android Support plugin for IntelliJ IDEA
(or Android Studio) cannot open this project,
please retry with version x.x or newer.
To resolve problem you must specify valid version.
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0' // was 3.5.1
...
}
}
apply plugin: 'com.android.application'