Explain in detail how to use Android studio to develop gradle plug-in
reason
First, explain why there is this article. Some time ago, the technology of plug-in and hot repair was very hot. Nuwa hot repair tool nuwagradle, Ctrip dynamic loading technology dynamic APK, and small, which hopes to be the lightest plug-in framework. One thing the three apps have in common is that they make a lot of use of gradle, a powerful construction tool. In addition to Ctrip's framework, the other two have released independent gradle plug-ins to provide automatic construction plug-ins or generate hot fixes. Therefore, it is very meaningful to learn the writing of gradle plug-in.
Plug in type
There are generally several types of gradle plug-ins:
Gradle related syntax
This article will not explain the grammar related to gradle in detail. If you want to learn something related to gradle, please check gradle for Android
Gradle plug-in development
Gradle plug-ins are developed using groovy, which is actually Java compatible. In fact, in addition to developing Android apps, Android studio is fully competent to develop gradle plug-ins. Let's talk about how to develop them.
Open the build.gradle file under module and enter
Now let's create a new file under the package name, named pluginimpl.groovy. Note that there is a groovy suffix, and then enter it. Note that the package name is replaced with your own package name.
Then create a new properties file in the resources / meta inf / gradle plugins directory. Note that the name of the file is that you only use the name of the plug-in, which is named plugin.test.properties, and enter it in it
Note that the package name needs to be replaced with your own package name.
In this way, the simplest gradle plug-in is completed. There is a task called testtask. After executing the task, a text will be output, just as we output HelloWorld at the beginning.
Publish to local warehouse
Next, we need to publish the plug-in to the Maven central warehouse. We just publish the plug-in to the local warehouse, and add the published code to the buidl.gradle file under the module project.
The above definitions of group and version will be used. As part of the coordinates of Maven library, group will be used as the groupid of the coordinates, version will be used as the version of the coordinates, and the artifact ID of the coordinates is composed of the module name. We let it take an alias modulename. Then, the directory of Maven local warehouse is the repo directory under the current project directory.
At this time, the gradle toolbar on the right side will have one more task under the module
Click the task of upload archives, and a repo directory will be added under the project, where the gradle plug-in is stored.
The directory is like the above figure. The specific directory structure is related to your package name. Time is my module name.
After publishing to the local Maven repository, we use it and add it to the gradle. Build file under the Android project called app
The name in quotation marks after the apply plugin is the file name of the plugin.test.properties file mentioned above. The contents in the quotation marks behind the class path are jointly determined by the group, version and modulename defined in grade above, which is the same as Maven.
Synchronize the gradle, and a testtask will appear under the other category under the app on the right. Double click to execute the task, and the console will output the string we just entered
Publish to jcenter warehouse
Next, we publish it to the jcenter central warehouse.
Add in the build.gradle file under the root directory of the project.
Create a new bindray.gradle file under the project root path, and enter
Modify the corresponding descriptive text to your own information, especially the first series of def definitions, and then add bintray to the gradle.properties file_ User and bintray_ KEY。
Apply the grade file in your module
There will be several more tasks on the toolbar of the gradle on the right
After that, we first run the install task under other, and then execute the bindrayupload task. If there is no accident, we will upload it. Then don't forget to add to jcenter in the background. After successfully adding to jcenter, the word link to jcenter will appear
Wait patiently for the success message of add to center, and then you can directly refer to the definition of Maven in the gradle file under module
Add before
The final content is as follows
It's that simple. Run it again to test whether it is successful.
Best practices
The source of the best practice is from multidex. Why? Recently, when the number of methods exceeds, if you choose multidex, the compilation process will be much slower. In order to detect which step is time-consuming, you need to write a plug-in to count the execution time of each task. Therefore, there is such a best practice.
Create a new timelistener.groovy file in the pluginimpl peer directory. input
Then change the apply method in the pluginimpl file to
After completion, package and publish to jcenter (). After that, as long as you reference the plug-in, you will count the execution time of each task. For example, when running the app, you will output the following information.
At the end of the best practice, I will promote this plug-in. I have released it to the jcenter warehouse. If you want to use it, you can add the following code
Transfer parameters
The above is a small test. Next, we need to obtain custom parameters.
First, create a new module according to the above steps. Create a new pluginextension.groovy and enter
Then we want to pass in nested parameters, create a new pluginnestextension. Groovy, and enter
Then create a new customtask.groovy, inherit the defaulttask class, and annotate the implemented method with @ taskaction annotation
Just get the parameters, and then do the simplest output operation. Use ${project. Pluginext. Param1} and ${project. Pluginext. Nestext. Nestparam1} to get the external parameters.
Don't forget to create a new properties file in the meta inf / gradle plugins directory to specify the interface implementation class of the plug-in.
Copy the newly created pluginimpl.groovy to the package and modify the apply method
After publishing the plug-in to the local maven, reference it.
Define external parameters. Here we define Param1, param2, nestparam1 and nestparam2. In addition, param3 and nestparam3 remain default.
Synchronize gradle and execute customtask.
The above code is very simple and can be seen without explanation, so I won't explain it anymore.
Source code
Finally, the source code of this article: gradleplugin_ jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.