Personal wechat public number: Practice, welcome to pay attention to exchange.

Gradle: Groovy and Gradle tasks. Gradle: Groovy and Gradle tasks

  • First introduction to Gradle series
  • Gradle: Groovy Basics
  • Gradle series build script basics
  • Gradle series understanding Gradle tasks
  • Gradle plugin for Gradle series
  • Gradle series of Java Gradle plug-ins
  • Gradle series of Android Gradle plugin
  • Gradle series Android Gradle base configuration
  • Android Gradle advanced Configuration
  • The Gradle series builds Maven private server libraries from scratch

Gradle plugins are built with many common plugins. Gradle plugins can help us improve development efficiency in certain scenarios. We can extend existing plugins to achieve more functions. For example, The Android Gradle plug-in is based on the built-in Java plug-in to achieve.

  1. What plug-ins do
  2. How to apply a plug-in
  3. Custom plug-in

What plug-ins do

The Gradle plugin is useful in the following aspects:

  1. Add tasks to a project to test, compile, and package the project.
  2. Add dependencies to a project, which can be used to configure dependencies needed during the project build process.
  3. You can add new extension attributes and methods to the existing object types in the Project, which can facilitate the configuration and construction optimization of the Project. For example, Android {} in the Android Project construction is an extension added by Android Gradle plug-in for the Project object.
  4. For example, using Java Gradle plug-in, you can specify the location of the source code in the SRC /main/ Java directory. When compiling, you can compile the Java source code files in the specified directory.

How to apply a plug-in

Before using a plug-in, use the Project Apply method to apply the plug-in, which is divided into binary plug-ins and script plug-ins.

Use of binary plug-ins

A binary Plugin is a Plugin that implements the org.gradle.api.plugin interface. Each Java Gradle Plugin has a Plugin ID. You can use a Java Plugin as follows:

apply plugin : 'java'
Copy the code

Java is the Plugin ID of the Java plugin. Gradle has a unique plugin ID for each core plugin. The corresponding specific Java type here is org. Gradle. API. Plugins. JavaPlugin, use Java plug-in so you can use the following ways:

apply.plugin:org.gradle.api.plugins.JavaPlugin
//org.gradle.api.plugins are imported by default
apply.plugin:JavaPlugin
Copy the code

Binary plug-ins are generally packaged and released in a Jar. For example, when customized plug-ins are released, the Plugin ID must be specified. The Plugin ID must be unique, and the application package name can be used to ensure the uniqueness of the Plugin ID.

Use of script plug-ins

Scripting plug-ins use is actually the use of a script file, use the script came in when the script to load the plug-in, use scripting plug-ins to the from keyword is used, the back of the script file can be local or script files on the network, the following definition of a script, we in the build. Gradle file using it, Details are as follows:

/ / version. Gradle file
ext{
    versionName = "1.0"
    versionCode = 1
}
Copy the code

This script file will be used in the build file as follows:

/ / build. Gradle file
apply from: 'version.gradle'

task taskVersion{
    doLast{
        println "Version is ${versionName}, version number is ${versionCode}"}}Copy the code

The result of the above code is as follows:

PS E:\Gradle\study\GradlePlugin> gradle taskVersion

> Task :TaskVersion version is1.0The version number is1


BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
Copy the code

Obviously, with the apply from reference to plug-in scripts, the use of the script plug-in can fragment the script files used by the build, separate the script files into a single script file with distinct responsibilities, and then use them directly with the Apply from keyword. For example, you can define some tool methods and the version numbers used by each dependency into a single Gradle file, so that you can call and manage the version numbers of each dependency in a unified manner.

The use of the apply method

Project.apply() accepts three different arguments, as follows:

// Closure as a parameter
void apply(Closure closure);
A ObjectConfigurationAction / / configuration
void apply(Action<? super ObjectConfigurationAction> action);
//Map as a parametervoid apply(Map<String, ? > options);Copy the code

To configure a plug-in, use the following three methods:

//Map as a parameter
apply plugin:'java'
// Closure as a parameter
apply{
    plugin 'java'
}
A ObjectConfigurationAction / / configuration
apply(new Action<ObjectConfigurationAction>() {

    @Override
    void execute(ObjectConfigurationAction objectConfigurationAction) {
        objectConfigurationAction.plugin('java')}})Copy the code
Use plug-ins published by third parties

Most of the time you need a third-party plugin to build your project, and you need to configure the classpath in buildScript {} to use it. Buildgradle {} : classpath = classpath = classpath = classpath;

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.0.1'}}Copy the code

The buildScript {} block is used to configure project build dependencies before the project is built. Once these dependencies are configured, the plugin can be used as follows:

// You must first configure the appropriate classpath in buildScript {}
apply plugin: 'com.android.application'
Copy the code
Use plugins DSL to apply the plug-in

The plugins DSL is a new way to use plugins. It can only be used with Gradle 2.1 and above.

/ / plugins DSL mode
plugins{
    id 'java'
}
/ / if a third party plug-in hosting to https://plugins.gradle.org/, need not in
// Classpath is configured in buildScript
plugins{
    id "Plugin id" version 'Plug-in Version number'
}
Copy the code

So much for using plug-ins.

Custom plug-in

Most of the time, a custom plug-in is needed to complete the construction of some projects. The custom plug-in must implement the Plugin interface, and the apply method in the interface will be executed when the plug-in is applied, so that the method can be implemented in the relevant operations. The following uses Android Studio to develop a simple plug-in. The plugin creates a task where Android Studio is used to create a Groovy project and then develop the relevant code.

Create a Module in Android Studio, delete files other than SRC /main and build.gradle, and create a.groovy file to implement the Plugin interface.

package com.manu.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project

/** * Custom plug-in */
class MPlugin implements Plugin<Project> {

    @Override
    void apply(Project target) {
        target.task('taskPlugin')  {
            doLast {
                println "Custom plug-in Creation Task"}}}}Copy the code

Create the resources/ meta-INF /gradle-plugins directory under SRC /main. Then create a properties file with the plugin ID as follows:

// Specify the plug-in implementation class
implementation-class=com.manu.plugin.MPlugin
Copy the code

The build.gradle file corresponds to the following configuration reference:

apply plugin: 'groovy'

dependencies {
    //gradle sdk
    compile gradleApi()
    //groovy sdk
    compile localGroovy()
}

Copy the code

It can be said that a simple plug-in is defined. For convenience and utility, the plug-in project will generate jar package, and then can be used in other projects. Here is a screenshot of the customized plug-in project directory:

Finally, use the plug-in in a build project, copy the plug-in to a project, such as the liBS folder in the project, specify the classpath of the plug-in, and use the Apply method to use the plug-in:

apply plugin: 'com.manu.plugin'

buildscript{
    dependencies{
        classpath files('libs/plugin.jar')}}Copy the code

The result of the above code is as follows:

PS E:\Gradle\study\GradlePlugin> gradle taskPlugin

> Task :TaskPlugin Create a customized task. BUILD SUCCESSFULin 2s
1 actionable task: 1 executed
Copy the code

Because the plugin creates the task taskPlugin, you can use this task. The basic use of the plugin is described above, and we will continue to learn how to use the Java Gradle plugin. You can follow the official account: Jzman-blog to communicate and learn together.