自定義Gradle-Plugin 插件
官方文檔給出了詳細(xì)的實現(xiàn)步驟,筆者 將參考官方文檔:通過自定義插件實現(xiàn)lint文件輸出鳖宾,本文按照以下三個方面進(jìn)行講解:
- 插件基礎(chǔ)介紹
- 三種插件的打包方式
- 實例Demo
插件基礎(chǔ)介紹
官方介紹:A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds. Gradle allows you to implement your own plugins, so you can reuse your build logic, and share it with others.
You can implement a Gradle plugin in any language you like, provided the implementation ends up compiled as bytecode. In our examples, we are going to use Groovy as the implementation language. Groovy, Java or Kotlin are all good choices as the language to use to implement a plugin, as the Gradle API has been designed to work well with these languages. In general, a plugin implemented using Java or Kotlin, which are statically typed, will perform better than the same plugin implemented using Groovy.
大體意思:插件打包了可重用的構(gòu)建邏輯芹啥,可以適用不同的項目和構(gòu)建悄泥。
Gradle 提供了很多官方插件,用于支持Java劝堪、Groovy等工程的構(gòu)建和打包冀自。同時也提供了自定義插件機(jī)制揉稚,讓每個人都可以通過插件來實現(xiàn)特定的構(gòu)建邏輯,并可以把這些邏輯打包起來熬粗,分享給其他人搀玖。
您可以實現(xiàn)一個Gradle插件用你喜歡任何語言,提供了實現(xiàn)最終編譯成字節(jié)碼。官方例子中,使用Groovy實現(xiàn)語言驻呐。Groovy巷怜、Java或Kotlin都是不錯的選擇。深入理解可以參考上一篇文章:Android中Gradle深入理解
Gradle自定義插件三種方式:
There are several places where you can put the source for the plugin.
Build script
You can include the source for the plugin directly in the build script. This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.
buildSrc project
You can put the source for the plugin in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.
See Organizing Gradle Projects for more details about the buildSrc project.
Standalone project
You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some plugins, or bundle several related task classes into a single library. Or some combination of the two.
大體意思總結(jié)為下3點:
1.build.gradle腳本中直接使用暴氏。這種方式就是直接在Android Studio app moudle的build.gradle 中進(jìn)行插件的編寫延塑,優(yōu)點就是不用再上傳插件到maven或者其它地方,項目就可以直接使用答渔;缺點也是很明顯关带,就是只能在自己的項目中使用,不能復(fù)用沼撕,這個不是我們今天要說的宋雏。
buildSrc中使用。這種方式需要在項目中新建一個moudle命名為buildSrc务豺,這個目錄就用來存放自定義插件磨总。然后在src/main中建立兩個目錄,一個就是存放代碼的groovy目錄笼沥,一個是存放自定義插件名稱的resources目錄蚪燕。這種定義方式也是只能在我們項目中進(jìn)行使用,不好復(fù)用奔浅。
3.獨立Module中使用馆纳。這種方式就是完全獨立開發(fā)一個Module,可以隨便用汹桦。
Build Script
把插件寫在build.gradle 文件中鲁驶,一般用于簡單的邏輯,只在改build.gradle 文件中可見舞骆,這里直接貼出Demo代碼:
/**
* 插件入口類
*/
class TestPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
//do something
}
}
apply plugin: TestPlugin
buildSrc
Use buildSrc to abstract imperative logic
Complex build logic is usually a good candidate for being encapsulated either as custom task or binary plugin. Custom task and plugin implementations should not live in the build script. It is very convenient to use buildSrc for that purpose as long as the code does not need to be shared among multiple, independent projects.
有興趣可參考:buildSrc介紹
總得來說:只對該項目中可見钥弯,適用于邏輯較為復(fù)雜,但又不需要外部可見的插件
獨立項目
先來一個整體代碼感受:
- 在Android Studio中新建 Java Library module uploader(moduleName 不重要督禽,根據(jù)實際情況定義)
- 修改項目文件夾
- [x] 移除java文件夾脆霎,因為在這個項目中用不到j(luò)ava代碼
- [x] 添加Groovy文件夾,主要的代碼文件放在這里
- [x] 添加resource文件夾赂蠢,存放用于標(biāo)識gradle插件的meta-data
- 修改build.gradle 文件
apply plugin: 'groovy'
dependencies{
//gradle sdk
compile gradleApi()
//groovy sdk
compile localGroovy()
}
//以上配置比較固定
//以下內(nèi)容主要用來上傳插件
apply plugin: 'maven'
repositories {
mavenCentral()
}
group = 'com.charles.plugin'
version = '1.0.0'
uploadArchives{
repositories {
mavenDeployer{
repository(url: uri('./../repo'))
}
}
}
- 創(chuàng)建Groovy腳本
接下來绪穆,在groovy目錄下,創(chuàng)建一個Groovy類(與Java類似,可以帶包名玖院,但Groovy類以.grovvy結(jié)尾菠红,所以groovy文件的創(chuàng)建是new->file->custom.groovy)。
//插件是一個類 繼承Plugin
class LintPlugin implements Plugin<Project> {
//重載 void apply(Project project)方法难菌,這個方法將會傳入使用這個插件的 project 的實例试溯,這是一個重要的 context。
@Override
void apply(Project project) {
/*project.task('lintOutputsTask') {
doLast {
println("lint outputs task start...")
}
}*/
project.afterEvaluate {
project.android.lintOptions.xmlOutput=new File(project.buildDir,"lintResult.xml")
}
project.tasks.create('cleanTest',CleanTestTask)
}
}
- 創(chuàng)建resources
resources目錄是標(biāo)識整個插件的目錄郊酒,也是后面apply plugin的內(nèi)容遇绞。
implementation-class=com.charles.plugin.LintPlugin
- 在主項目中使用插件
在主項目的build.gradle文件中,通過apply指令來加載自定義的插件燎窘,腳本如下所示:
apply plugin: 'com.charles.plugin'
buildscript {
ext.kotlin_version = '1.2.30'
repositories {
google()
jcenter()
maven {
url uri('./../repo')
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.charles.plugin:lintplugin:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
對比:
在buildSrc中創(chuàng)建自定義Gradle插件只能在當(dāng)前項目中使用摹闽,因此,對于具有普遍性的插件來說褐健,通常是建立一個獨立的Module來創(chuàng)建自定義Gradle插件付鹿。
區(qū)別在于:
- 不需要進(jìn)行model的名稱的寫死,也就是你可以隨意的命名
- buildSrc會自動的編譯和加入到classpath中蚜迅,這里我們需要手動依賴
- 需要上傳到maven倉庫中
參考:
- Android中Gradle深入理解
- 在AndroidStudio中自定義Gradle插件舵匾,
- Gradle自定義插件
- 一步步自定義Gradle插
- 自定義Gradle-Plugin 插件
- Gradle官方文檔
- Gradle自定義插件詳解
- 自己Demo代碼
聲明:此為原創(chuàng),轉(zhuǎn)載請聯(lián)系作者
作者:微信公眾號添加公眾號-遛狗的程序員 谁不,或者可以掃描以下二維碼關(guān)注相關(guān)技術(shù)文章坐梯。
當(dāng)然喜愛技術(shù),樂于分享的你也可以可以添加作者微信號: