current position:Home>Android gradle custom plug-in implementation
Android gradle custom plug-in implementation
2023-01-25 11:29:40【Someone from Yungu Li】
Android之Gradle自定义插件实现
GradleBasic overview of plugin implementation
Gradle插件的三种方式,如下表所示:
插件实现方式 | 解释说明 |
---|---|
Build script 脚本 | 把插件写在build.gradle文件中,一般用于简单的逻辑,只在该build.grade文件可见 |
buildSrc目录 | 将插件源代码放在buildSrc/src/main中,Only visible to this item |
独立项目 | 一个独立的Java项目/模块,可以将文件包发布到仓库(Jecenter,Maven),使其他项目方便引入 |
The three methods are described below:
1、build script 脚本
Plugins implemented this way onlybuild.gradle文件可见,例如在app的build.gradle中,插件名PluginDemo实现方式:
apply plugin: PluginDemo //引入插件
//定义插件
class PluginDemo implements Plugin<Project> {
@Override
void apply(Project project) {
println 'The first way is to implement plugins'
}
}
运行gradlewwill print the above content
2、buildSrc目录
这种方式创建的Module是一个独特的,AndroidStudio会自动识别buildSrc插件模块.
打开AS——>File——>New Module——>选择Java or Kotlin Library
填写Library Name一定要是 buildSrc.Otherwise, the system cannot recognize the plug-in,After the creation is complete, the project is as follows:
另外注意 在setting.gradleTo remove the pairbuildSrc的配置,无需配置,如下图所示:
下面在src/main/The package file is below,创建Java插件类Plugin2Dmeo,实现如下:
package com.xiaomi.app;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class Plugin2Demo implements Plugin<Project> {
@Override
public void apply(Project project) {
System.out.println("The second way to implement plugins");
}
}
The next step is to configure the manifest file before it can be used,创建步骤如下:
在main下面创建resources/META-INF/gradle-plugins/com.xiaomi.plugin.properties文件,结构如下:
说明一下com.xiaomi.plugin.properties文件的含义
其中com.xiaomi.pluginIt is the name of the plugin to be introduced in our project,The name of the plugin can be chosen arbitrarily.
上面的.propertiesWhat should be filled in the content of the file
In fact, it is the plug-in class found from the entrance;内容如下:
implementation-class = com.xiaomi.app.Plugin2Demo
里面内容implementation-class,The configuration inside is the class name of the plugin,截图如下:
完成上面的步骤,We are just one step away from using the plugin!
在app.gradleImport the above plugins into the module,就可以:
apply plugin:'com.xiaomi.plugin'//这里引用的就是properties文件的名称
The name in the light blue box here isproperties的名称;
运行gradlew,It is found that the plug-in of the second method has been implemented;
怎么样学会了吗?The third method is implemented below.
3、独立项目(推荐,有用,This is how plugins are written)
Plug-ins implemented in this way can be published locally,jcenter仓库或者Maven仓库,Or your own company's code warehouse;
Its implementation process is similar to the second way,Only some steps are different,It requires you to publish;
The general process is divided into custom plug-ins and using plug-ins,The process of customizing the plugin is first:
自定义插件
一、创建一个java lib库 Feel free to go with a name yourself,不多说了,Similar to the above process
二,在这个插件的build.gradleAdd the following dependencies as wellclass类中实现Plugin接口
build.gradle,This file needs to manually add the dependencies of the plugin,This is different from the second one above
apply plugin: 'groovy'
dependencies {
implementation gradleApi()
}
javaThe class implementation is similar to the above
package com.huawei.plugin;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class MyPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
System.out.println("The third way is to implement pluginsmy plugin");
}
}
三、配置清单文件
The implementation of the manifest file is also similar to the second step above;
在main文件夹下面新建resources/META-INF/gradle-plugins,After building these three folders,We're going to build a suffix of .properties的文件,You can choose any name you want;此处是com.huawei.plugin.properties
The configuration in this file is:
implementation-class = com.huawei.plugin.MyPlugin
四,发布到仓库(Get your local repository here,Do not get the remote warehouse)
在插件的build.gradle添加如下脚本,A post is required here,后面才可以使用.
apply plugin: 'maven-publish'
publishing {
publications {
MyPlugin(MavenPublication) {
from components.java
groupId 'com.huawei'
artifactId 'plugin'
version '1.0.0'
}
}
}
单击 rebuild,进行编译,Then click on the right menu bar to openGadle
双击3的位置,Publish locally,After this is done, the plugin can be used;
使用插件
使用方式如下:
1.在主工程的build.gradle文件中添加如下:
buildscript {
repositories {
google()
jcenter()
mavenLocal() //默认路径在C:/Users/用户名/.m2/repository下面,(本机电脑)
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.huawei:plugin:1.0.0'//引入插件
}
}
2.在app项目的build.gradle中添加
apply plugin: 'com.huawei.plugin'
This is the third way to configure plug-insOK了!
我运行gradlew之后,The plugin works normally;
The above are the three ways to use the simple version of the plug-in summarized by me,It was good to see it in the end,Excuse me, bosses,动动小手,给个赞吧!
动动小手,点个赞再走
Additional extension classes for plugins,I will not expand here,afraid of making too much,Everyone sees it in a cloud of fog,需要的话,可以自己去扩展,此处就不展开了!
copyright notice
author[Someone from Yungu Li],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251120395800.html
The sidebar is recommended
- Based on VirtualBox centos7 virtual machine installation
- JVM process cache - Caffeine
- Code Caprice No24 | Backtracking Algorithm Theoretical Basis, 77. Combination
- The Road to Algorithm Practice——[String] Leetcode 824 Goat Latin
- Git summary - Prompt to submit or temporarily store changes when switching to a branch
- Productivity tools - [gitlab configuration] One-time configuration of GitLab account
- Echo+Vue+ElementUI management background source code
- Revel+Vue+ElementUI framework use and build tutorial
- Basic practice tutorial of Revel+Vue+ElementUI framework
- Go Web Development Revel+Vue+ElementUI Framework Practical Tutorial
guess what you like
Construction and deployment tutorial of Revel+Vue+ElementUI framework
8. Java loop advanced comprehensive exercises - infinite loop and jump control statement, every seven passes, square root, judging whether it is a prime number, guessing number games
Control statement + exception handling perfect lucky draw applet-java basics
9. Java Array Knowledge Encyclopedia
Lucky draw applet-java basics
Java basic implementation calculator applet
The front end of actual combat: copy write millet's official website on the first day
1. Linux application programming and network programming---file IO notes in Linux system
7. Linux application programming and network programming --- thread full solution Notes
[Front-end notes - CSS] 10. Cascading and inheritance + selector
Random recommended
- 8. Linux application programming and network programming---Linux network programming notes
- [Java|golang] 1828. Count the number of points in a circle
- Add environment variables under Linux system and will not overwrite the previous method of adding environment variables
- 2 · Linux application programming and network programming - file attributes notes
- Redis interview questions (classic 7 questions)
- [Front-end notes——CSS] 11. Box model + background and border
- C + + large Numbers together, according to a combined
- 3. Linux application programming and network programming --- get system information notes
- The use and principle of Kafka message queue
- 5. Linux application programming and network programming---signal notes in Linux
- 6 · Linux application programming and system programming - senior IO notes
- Java collection common interview questions (4)
- 072-JAVA project training: Imitation QQ instant messaging software series lecture 7 (explaining the realization of the chat interface and functions)
- Coordination center performance comparison: how zookeeper solves the load balancing problem
- 070-JAVA project training: imitation QQ instant messaging software series lecture five (explain user registration function)
- Ubuntu installation and configuration (brief)
- 073-JAVA project training: imitation QQ instant messaging software series lecture eight (explain query and add friend function)
- SQL injection classification and error injection EXP
- All basic commands in linux fail, showing that the command cannot be found
- 4. Linux application programming and network programming---Linux process full solution notes (difference between process and program)
- Linux system - basic IO
- Hanlp's understanding of user-defined dictionaries (java version)
- Brief description and configuration of Maven
- 071-JAVA project training: imitation QQ instant messaging software series lecture six (explaining the function of QQ main interface)
- 【Maximum LeetCode】January Algorithm Training (12) Linked List
- 【Max LeetCode】January Algorithm Training (13) Doubly Linked List
- [Big Data Management] Java implements Bloom filter
- [Maximum LeetCode] Algorithm training in January (14) stack
- [Machine Learning] Adaboost Integrated Algorithm
- [Big Data Management] Java implements cuckoo filter (CF)
- Chaozhou Xiangqiao: "Charming Ancient City, Cultural Sharing" Spring Festival Intangible Cultural Heritage Market Opens
- [Big data management] Java realizes the dictionary tree TireTree
- [Max LeetCode] January Algorithm Training (11) Matrix
- New Express (Web framework based on HTTP module encapsulation NodeJS)
- JavaScript error-prone questions (stack processing, call function, prototype chain questions)
- Space "travel", lion and crane dance, intangible cultural heritage experience...During the Spring Festival, Zhuhai Jinwan is so fun!
- Wine 8.0 official release: better support for running Windows applications on Linux and other systems
- Zhongke Sugon: Sugon's new computer "participates in" "The Wandering Earth 2"
- Linux actual combat notes finishing (1.24)
- Automatically execute the specified sql when the springBoot project starts