小试牛刀,写一个简单的plugin
class HenCoderPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
def extension = project.extensions.create('hencoder', HenCoderExtension)
project.afterEvaluate {
println "Hello ${extension.name}"
}
}
}
class HenCoderExtension {
def name = 'hello kitty'
}
//先应用 再配置 再打印
apply plugin: HenCoderPlugin
hencoder {
name 'pipixia'
}
plugin真正作用在于复用 重用
创建一个可复用的plugin
buildSrc 固定名称不能改,对于gradle来说这个名字是固定plugin插件
image-20210605213514597.png
报错:'buildSrc' cannot be used as a project name as it is a reserved name
删掉 setting.gradle 中得 buildSrc
buildSrc 是系统特殊的模块不需要添加 ,插件项目专属
在setting.gradle 下面 其他的*.gradle之前
image-20210605220658140.png如果没有Plugin 依赖
image-20210605220959809.png一一映射
创建文件夹 固定 resources>META-INF>gradle-plugins>*.properties
image-20210605223910665.png引用名字同 *.properties
image-20210605224115647.png重点buildSrc一点都不能错哦
总结,插件申请,拓展配置 系统固定模式
Transform 可以实现切片变成(注册后要全部负责)
HenCoderTransform.groovy
package com.example.buildsrc
import com.android.build.api.transform.Format
import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformException
import com.android.build.api.transform.TransformInvocation
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.utils.FileUtils
/**
* 来自于android的构建tool,直接运行会报错
*/
class HenCoderTransform extends Transform {
@Override
String getName() {
return 'hencoder'
}
@Override
Set<QualifiedContent.ContentType> getInputTypes() {
return TransformManager.CONTENT_CLASS
}
@Override
Set<? super QualifiedContent.Scope> getScopes() {
return TransformManager.SCOPE_FULL_PROJECT
}
@Override
boolean isIncremental() {
return false
}
/**
* 负责运输,没有它不行
* @param transformInvocation
* @throws TransformException* @throws InterruptedException* @throws IOException
*/
@Override
void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
super.transform(transformInvocation)
//整个过程就是输入输出,固定
def inputs = transformInvocation.inputs
def outputProvider = transformInvocation.outputProvider
inputs.each {
it.jarInputs.each {
File dest = outputProvider.getContentLocation(it.name,it.contentTypes,it.scopes, Format.JAR)
println "Jar:${it.file}"
FileUtils.copyFile(it.file,dest)
}
it.directoryInputs.each {
File dest = outputProvider.getContentLocation(it.name,it.contentTypes,it.scopes, Format.DIRECTORY)
println "Dir:${it.file}"
FileUtils.copyDirectory(it.file,dest)
}
}
//javassist
//ASM
}
}
插件 HenCoder.groovy
package com.example.buildsrc
import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
class HenCoder implements Plugin<Project> {
@Override
void apply(Project project) {
def extension = project.extensions.create('hencoder', HenCoderExtension)
project.afterEvaluate {
println "Hello ${extension.name}"
}
//创建Transform,并注册到打包过程
def transform = new HenCoderTransform()
def baseExtension=project.extensions.getByType(BaseExtension)
baseExtension.registerTransform(transform)
}
}
//Transform 使用 com.android.tools.build:gradle:... 达到切片编程
//BaseExtension 系统像android{} 的扩展
image-20210605213514597.png
更新 也可以用java或kotlin写plugin
buildSrc模块的build.gradle,由于gradle加载时间
apply plugin: 'java-gradle-plugin'
apply plugin: 'kotlin'
buildscript {
ext.kotlin_version = '1.4.32'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
dependencies {
compileOnly gradleApi()
compileOnly localGroovy()
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
HenCoder.kt
open class HenCoder:Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create("hencoder",HenCoderExtension::class.java)
project.afterEvaluate {
println("Hello ${extension.name}")
}
}
}
HenCoderExtension.kt
*/
open class HenCoderExtension {
var name = "00"
}
网友评论