In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Gradle how to customize plug-ins, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
To create custom plugins in Gradle, Gradle offers three ways:
Use directly in the build.gradle script
Use in buildSrc
Use in standalone modules
Gradle plugins can be developed in IDEA or Android Studio. The only difference is that IDEA provides plugins developed by Gradle, which is more convenient to create files and directories. In Android Studio, developers need to manually create them (but in fact, these directories are not many, nor complex, and can be created manually. Moreover, creating them in AS is conducive to debugging plug-in dependencies and improving functions).
Use in build.gradle script
It is the simplest to use in the build.gradle script, but it is only suitable for very simple functions, and it is not convenient for other places to apply. For example, you can write it directly in the build.gradle of the app moudle. After sync, you can query MytestPlugin in the other corresponding to the right model.
Build.gradle in root directory
Double-click execution, you can directly execute this plugin in android studio terminal:
Screenshot after execution
Since it is relatively simple to customize directly in build.gradle, we will not focus on the explanation here.
2
Use in projects
Description of official website https://docs.gradle.org/4.0.2/userguide/organizing_build_logic.html#multiProjectBuildSrc
Please refer to (currently the latest gradle 4.0.2 content, lower versions are also compatible with this feature)
According to the official website description, the gradle plugin process used in the project here is as follows:
First create a standard Android project in Android Studio, and then create a new model named buildSrc in the root directory of the project. This directory is used to store custom plugins.
The procedure for operating the new model is as follows:
src/main Under the project file:
Remove java folder because java code is not used in this project
Add groovy folder, main code files are placed here
Add the resources folder for meta-data identifying gradle plug-ins
Modify src/build.gradle configuration content
Here is one of my actual projects:
Use gradle screenshots in projects
Among them, except for the buildSrc directory, the others are standard Android directories, and buildSrc is the default directory provided by Gradle to configure custom plug-ins in the project, and the directories to be created by Gradle are RootProject/src/main/groovy and RootProject/src/main/resources.
1, create buildSrc/build.gradle
First, configure the build.gradle file in the buildSrc directory. This configuration is relatively fixed. Use the official website example. The script is as follows:
2. Create Groovy Script
Next, in the groovy directory, create a Groovy class (similar to Java, you can have a package name, but Groovy class ends in. groovy, so the groovy file creation is new->file->custom.groovy), as shown in the figure:
MyPlugin is a new file with the full name MyPlugin.groovy
First create a subdirectory under groovy, here similar to java package, com/myPlugin directory, and then create MyPlugin.groovy file:
Note:
Groovy folder class, must be modified to.groovy
And then the IDEs will recognize it.
This plugin creates a Task named testPlugin and prints it in task. A plug-in is a class that inherits from the org.gradle.api.Plugin interface and overloads the void apply(Project) method, which will be passed to the instance of the project that uses the plug-in, which is an important context.
3. Create resources
The resources directory is a directory that identifies the entire plug-in. The structure under the directory is as follows:
This directory structure, like buildSrc, is the default directory for Gradle plugins and cannot be modified. After creating these directories, create the--plug-in name.properties file under the gradle-plugins directory, as shown in the figure above: myCustomPlugin.properties file
In this file, the code looks like this:
implementation-class=com.myPlugin.MyPlugin
The code above specifies the Groovy class that was created first.
Using plug-ins in the main project
In the build.gradle file of the main project, load the custom plug-in with the apply command. The script looks like this:
apply plugin: 'myCustomGradle'
The name of the plugin is the name--myCustomPlugin in myCustomPlugin.properties created earlier. In this way, the custom plugin is loaded.
After configuration, you can use custom plug-ins in the main project, execute gradlew testPlugin command in the terminal or search in gradle integration on the right, and double-click app-other:testPlugin task, as shown in the figure:
The plugin command is clickable
The results are as follows:
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.