gradle 自定义插件之buildSrc

步骤

 1 创建buildSrc

在项目工程的根目录下创建buildSrc目录,这个名字不能错,必须是buildSrc。创建完之后,rebuild一下工程,会在buildSrc下生成一些目录。

编译之后 目录变色

2创建build.gradle

apply plugin: 'java'
sourceSets {
    main{
        java{
            srcDir 'src/main/java'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
}

 3 在buildSrc目录下创建src/main/java目录,之后再创建插件的包名:org.tyearlin,并编写插件代码:

CustomPlugin  

public class CustomPlugin  implements Plugin<Project> {
    @Override
    public void apply(Project project) {
     
      System.out.println("!!!!!!!!!!!!!!!!!!!!!!!mode name" + project.getName());;
        }
}

CustomPlugin.properties 

implementation-class=org.tyearlin.CustomPlugin

4 使用运行


在app模块下的build.gradle中引入插件:apply plugin: 'CustomPlugin',这里引入的插件明就是插件的包名。

编译 app 

看下 Hugo 插件的实现

Hugo是JakeWharton大神开发的一个通过注解触发的Debug日志库

package hugo.weaving.plugin

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile

class HugoPlugin implements Plugin<Project> {
  @Override void apply(Project project) {
//app lib判断 apply plugin: 'com.android.application'?apply plugin: 'com.android.library'

    def hasApp = project.plugins.withType(AppPlugin)
    def hasLib = project.plugins.withType(LibraryPlugin)
    if (!hasApp && !hasLib) {
      throw new IllegalStateException("'android' or 'android-library' plugin required.")
    }
 //控制台log
    final def log = project.logger
    final def variants
    if (hasApp) {
      variants = project.android.applicationVariants
    } else {
      variants = project.android.libraryVariants
    }

//依赖
    project.dependencies {
      debugCompile 'com.jakewharton.hugo:hugo-runtime:1.2.2-SNAPSHOT'
      // TODO this should come transitively
      debugCompile 'org.aspectj:aspectjrt:1.8.6'
      compile 'com.jakewharton.hugo:hugo-annotations:1.2.2-SNAPSHOT'
    }
//扩展属性 enable
    project.extensions.create('hugo', HugoExtension)
//所有渠道debug模式开启
    variants.all { variant ->
      if (!variant.buildType.isDebuggable()) {
        log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
        return;
      } else if (!project.hugo.enabled) {
//扩展属性 enable
        log.debug("Hugo is not disabled.")
        return;
      }

//,调用aspect反编译,args指定了aspect相关参数。

      JavaCompile javaCompile = variant.javaCompile
//java编译最后阶段 字节码织入
      javaCompile.doLast {
        String[] args = [
            "-showWeaveInfo",
            "-1.5",
            "-inpath", javaCompile.destinationDir.toString(),
            "-aspectpath", javaCompile.classpath.asPath,
            "-d", javaCompile.destinationDir.toString(),
            "-classpath", javaCompile.classpath.asPath,
            "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
        ]
        log.debug "ajc args: " + Arrays.toString(args)

        MessageHandler handler = new MessageHandler(true);
        new Main().run(args, handler);
        //输出日志
        for (IMessage message : handler.getMessages(null, true)) {
          switch (message.getKind()) {
            case IMessage.ABORT:
            case IMessage.ERROR:
            case IMessage.FAIL:
              log.error message.message, message.thrown
              break;
            case IMessage.WARNING:
              log.warn message.message, message.thrown
              break;
            case IMessage.INFO:
              log.info message.message, message.thrown
              break;
            case IMessage.DEBUG:
              log.debug message.message, message.thrown
              break;
          }
        }
      }
    }
  }
}

gradle官网 guid

https://docs.gradle.org/current/dsl/index.html

Guess you like

Origin blog.csdn.net/TyearLin/article/details/120185760