Android gradle custom plug-in

Gradle's plug-ins are packaged in three ways:

Let's start with the plug-ins in the basic build script

To create a gradle plug-in, you need to create a class that implements the plugin interface. When the plug-in is applied to the project, gradle will create an instance of the plug-in class and call the apply () method of the instance. If necessary, the plug-in can also be used to configure the project

Make the plug-in configurable and add some simple extension properties to the project. For example, you can add a greeting extension object to the project, which allows us to configure the greeting object.

The file is build.gradle:

Create gradle module

There is no new option like gradle plugin in Android studio. How can we write and package the gradle plugin in Android studio?

As follows:

After defining your own gradle plug-in class, the next step is to tell gradle which is our custom plug-in class. Therefore, you need to create a new resources directory in the main directory, then a new meta-inf directory in the resources directory, and then a new gradle plugins directory in the meta-inf directory. Finally, create a new properties file in the gradle plugins directory. Pay attention to the name of this file. You can name it at will, but this name will be used later when using this plug-in. For example, if you name it com.sjq.gradle.properties and use a custom plug-in in other build.gradle files, you need to write:

Then specify your custom class in the com.hc.gradle.properties file

Here we still use the package name for easy memory.

Here, the results of the whole directory are shown in the following figure:

Because we need groovy and Maven for later packaging, we need to add the following code to build.gradle under our customized module:

We have customized the plug-in before. The next step is to package it into the Maven library. You can choose to package it to a local or remote server. Add the following code to build.gradle under our custom module Directory:

Among them, group and version will be used later, which we will talk about later. Although we have defined the packaging address and packaging related configuration, we still need to let the packaging task execute. Click the gradle tool on the right side of Android studio, as shown in the following figure:

You can see the uploadarchives task. Double click uploadarchives to package and upload! After execution, go to our Maven local warehouse to check:

Among them, the COM / sjq / greenplugin directories are specified by our group, myplugin is the name of the module, and 1.0.0 is the version number (specified by version).

The next step is to use custom plug-ins. Generally, custom plug-ins are used in the app module. Therefore, in the build.gradle file of the app module, you need to specify the local Maven address, the name of the custom plug-in and the dependent package name. In short, add the following code to the build.gradle file of the app module:

Enter the command line in the current directory:

Operation results:

We talked about how to customize the gradle plug-in and package it. There may be many steps. Sometimes, you may not need to package it, but just use it in this project, so you don't need to package this process.

The gradle plug-in developed for the current project is relatively simple. The steps mentioned before are very similar, but there are a few points to note:

The name of the newly created module must be buildsrc, and the resources directory is not required

The content of build.gradle is:

The content of custompluginb is

How to use the app module? Add it directly under build.gradle of app

At the command line:

The results are shown in the figure below:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>