自定义gradle插件&ASM字节码插入

自定义gradle插件
1.新建Android Studio Project,在项目下新建Module,选择Library
image.png
2.删除新建Module下除build.gradle外的所有文件
image.png
3.新建 src 目录, src/main/groovy, groovy目录下创建插件包名com.example.testplugin, main目录下新建 resource/META-INF/gradle-plugins目录
image.png
4.修改 build.gradle文件
apply plugin: 'maven'

dependencies {
    implementation gradleApi()
    implementation localGroovy()
    implementation 'com.android.tools.build:gradle:3.5.0'
}
repositories {
    mavenLocal()
}
uploadArchives {
    repositories.mavenDeployer {
        pom.groupId = 'com.example.testplugin'
        pom.artifactId = 'test'
        pom.version = '1.0.0'
        //发布到本地仓库
        repository(url: uri('../repo'))
    }
}
5.groovy目录下新建文件TestPluginImp.groovy,注意此处新建文件一定要带上 .groovy 文件后缀
image.png
6.TestPluginImp文件插入以下代码
package com.example.testplugin

import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPluginImp implements Plugin {

    void apply(Project project) {
        System.out.println '========================'
        System.out.println '我是测试gradle TestPlugin'
        System.out.println '========================'
    }
}
7.META-INF.gradle-plugins目录下新建com.example.testplugin.properties,并在文件中插入以下代码
implementation-class=com.example.testplugin.TestPluginImp
8. properties文件名称与文件中内容以及我们上面建立的gradle文件TestPluginImp相互对应
image.png
9.到此处自定义gradle插件就完成了,点击编译器右侧gradle->upload->uploadArchives将插件发布到本地仓库,会发现项目目录下会多出repro目录,里面是我们打包完的插件资源
image.png
10.话不多说,既然写完了,我们就验证下刚写完的gradle插件,在项目跟目录下增加maven本地仓目录,同时导入插件,在app目录gradle文件下apply插件
image.png

image.png
11. Rebuild Project,查看build output窗口输出,我们在TestPluginImp.groovy文件中的apply方法写入的内容被打印了,说明我们自定义的gradle插件生效了,好了自定义gradle插件到此结束
image.png
ASM字节码注入(在Activity生命周期中注入部分代码)
前言

安卓AOP三剑客:APT,AspectJ,Javassist(https://www.jianshu.com/p/dca3e2c8608a?from=timeline)
APT代表框架:DataBinding,Dagger2, ButterKnife, EventBus3 、DBFlow、AndroidAnnotation
AspectJ代表框架:Hugo(Jake Wharton)
Javassist代表框架:热修复框架HotFix 、Savior(InstantRun)

image.png

直接开始介绍ASM字节码注入
1.核心方法为transform,通过transformInvocation.inputstransformInvocation.outputProvider遍历和写入文件,通过ClassVisitor.visitMethod想方法中写入想要插入的字节码
2.建立java/com.example.testplugin目录,并在下面建立LifeTestClassVisitor}$、LifeTestOnCreateMethodVisitor} ,文件目录如下:
image.png
3.我们想要在Activity的onCreate以及onDestory里分别注入以下代码:
Log.e(this.getClass().getName(), this.getClass().getName()+"    onCreate");
Log.e(this.getClass().getName(), this.getClass().getName()+"    onDestory");
4.由于注入的是字节码所以我们要对需要进行注入的代码进行转换,这里我们借助一个插件 ASM Bytecode Outline,可以直接在Setting->Plugins中直接安装:
image.png
5.新建ByteCode.java文件,写入测试代码,通过ASM Bytecode插件查看字节码,如下:
image.png
6.继续写入我们需要插入的代码,并查看字节码,获取差异部分代码备用
image.png
7.遍历以及插入部分主要代码如下:
TestPluginImp.groovy
package com.example.testplugin

import com.android.build.api.transform.DirectoryInput
import com.android.build.api.transform.Format
import com.android.build.api.transform.JarInput
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.TransformInput
import com.android.build.api.transform.TransformInvocation
import com.android.build.api.transform.TransformOutputProvider
import com.android.build.gradle.AppExtension
import com.android.build.gradle.internal.pipeline.TransformManager
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter

import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry

import static org.objectweb.asm.ClassReader.EXPAND_FRAMES

class TestPluginImp extends Transform implements Plugin {

    void apply(Project project) {
        System.out.println '========================'
        System.out.println '我是测试gradle TestPlugin'
        System.out.println '========================'
        def android = project.extensions.getByType(AppExtension)
        android.registerTransform(this)
    }

    @Override
    String getName() {
        //该方法表示当前Transform在task列表中的名字,返回值最终经过一系列的拼接,具体拼接实现在TransformManager的getTaskNamePrefix()方法中,拼接格式:transform${InputType1}And${InputType2}And${InputTypeN}And${name}For${flavor}${BuildType}
        return "TestPluginImp"
    }

    @Override
    Set getInputTypes() {
        //该方法表示指定输入类型,这里我们指定CONTENT_CLASS类型
        return TransformManager.CONTENT_CLASS
    }

    @Override
    Set getScopes() {
        //该方法表示指定输入类型,这里我们指定CONTENT_CLASS类型
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        //该方法表示当前Transform是否支持增量编译
        return false
    }

    @Override
    void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
        //该方法是重点,它接收上一个Transform的输出,并把处理后的结果作为下一个Transform的输入
        System.out.println '--------------- LifeTestPlugin visit start --------------- '
        def startTime = System.currentTimeMillis()
        Collection inputs = transformInvocation.inputs
        TransformOutputProvider outputProvider = transformInvocation.outputProvider
        //删除之前的输出
        if (outputProvider != null)
            outputProvider.deleteAll()
        //遍历inputs
        inputs.each { TransformInput input ->
            //遍历directoryInputs
            input.directoryInputs.each { DirectoryInput directoryInput ->
                handleDirectoryInput(directoryInput, outputProvider)
            }

            //遍历jarInputs
            input.jarInputs.each { JarInput jarInput ->
                handleJarInputs(jarInput, outputProvider)
            }
        }
        def cost = (System.currentTimeMillis() - startTime) / 1000
        System.out.println '--------------- LifeTestPlugin visit end --------------- '
        System.out.println "LifeTestPlugin cost : $cost s"
    }
    /**
     * 处理文件目录下的class文件
     */
    static void handleDirectoryInput(DirectoryInput directoryInput, TransformOutputProvider outputProvider) {
        //是否是目录
        if (directoryInput.file.isDirectory()) {
            //列出目录所有文件(包含子文件夹,子文件夹内文件)
            directoryInput.file.eachFileRecurse { File file ->
                def name = file.name
                if (checkClassFile(name)) {
                    ClassReader classReader = new ClassReader(file.bytes)
                    ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
                    ClassVisitor cv = new LifeTestClassVisitor(classWriter)
                    classReader.accept(cv, EXPAND_FRAMES)
                    byte[] code = classWriter.toByteArray()
                    FileOutputStream fos = new FileOutputStream(
                            file.parentFile.absolutePath + File.separator + name)
                    fos.write(code)
                    fos.close()
                }
            }
        }
        //处理完输入文件之后,要把输出给下一个任务
        def dest = outputProvider.getContentLocation(directoryInput.name,
                directoryInput.contentTypes, directoryInput.scopes,
                Format.DIRECTORY)
        FileUtils.copyDirectory(directoryInput.file, dest)
    }

    /**
     * 处理Jar中的class文件
     */
    static void handleJarInputs(JarInput jarInput, TransformOutputProvider outputProvider) {
        if (jarInput.file.getAbsolutePath().endsWith(".jar")) {
            //重名名输出文件,因为可能同名,会覆盖
            def jarName = jarInput.name
            def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
            if (jarName.endsWith(".jar")) {
                jarName = jarName.substring(0, jarName.length() - 4)
            }
            JarFile jarFile = new JarFile(jarInput.file)
            Enumeration enumeration = jarFile.entries()
            File tmpFile = new File(jarInput.file.getParent() + File.separator + "classes_temp.jar")
            //避免上次的缓存被重复插入
            if (tmpFile.exists()) {
                tmpFile.delete()
            }
            JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tmpFile))
            //用于保存
            while (enumeration.hasMoreElements()) {
                JarEntry jarEntry = (JarEntry) enumeration.nextElement()
                String entryName = jarEntry.getName()
                ZipEntry zipEntry = new ZipEntry(entryName)
                InputStream inputStream = jarFile.getInputStream(jarEntry)
                //插桩class
                if (checkClassFile(entryName)) {
                    //class文件处理
                    jarOutputStream.putNextEntry(zipEntry)
                    ClassReader classReader = new ClassReader(IOUtils.toByteArray(inputStream))
                    ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
                    ClassVisitor cv = new LifeTestClassVisitor(classWriter)
                    classReader.accept(cv, EXPAND_FRAMES)
                    byte[] code = classWriter.toByteArray()
                    jarOutputStream.write(code)
                } else {
                    jarOutputStream.putNextEntry(zipEntry)
                    jarOutputStream.write(IOUtils.toByteArray(inputStream))
                }
                jarOutputStream.closeEntry()
            }
            //结束
            jarOutputStream.close()
            jarFile.close()
            def dest = outputProvider.getContentLocation(jarName + md5Name,
                    jarInput.contentTypes, jarInput.scopes, Format.JAR)
            FileUtils.copyFile(tmpFile, dest)
            tmpFile.delete()
        }
    }

    /**
     * 检查class文件是否需要处理
     * @param fileName
     * @return
     */
    static boolean checkClassFile(String name) {
        //只处理需要的class文件
        return (name.endsWith(".class") && !name.startsWith("R\$")
                && !"R.class".equals(name) && !"BuildConfig.class".equals(name)&&!name.startsWith("android"))
    }
}
LifeTestClassVisitor.java
package com.example.testplugin;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class LifeTestClassVisitor extends ClassVisitor implements Opcodes {

    private String mClassName;

    public LifeTestClassVisitor(ClassVisitor cv) {
        super(Opcodes.ASM5, cv);
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        System.out.println("LifeTestClassVisitor : visit -----> started:" + name);
        this.mClassName = name;
        super.visit(version, access, name, signature, superName, interfaces);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        System.out.println("LifeTestClassVisitor : visitMethod " + name);
        MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
        System.out.println("LifeTestClassVisitor : visit -----> started:" + this.mClassName);
        //匹配FragmentActivity
        if ("onCreate".equals(name)) {
            //处理onCreate
            System.out.println("LifeTestClassVisitor : change method ----> " + name);
            return new LifeTestOnCreateMethodVisitor(mv);
        } else if ("onDestroy".equals(name)) {
            //处理onDestroy
            System.out.println("LifeTestClassVisitor : change method ----> " + name);
            return new LifeTestOnDestroyMethodVisitor(mv);
        }
        return mv;
    }

    @Override
    public void visitEnd() {
        super.visitEnd();
    }
}

LifeTestOnCreateMethodVisitor.java
package com.example.testplugin;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.POP;

public class LifeTestOnCreateMethodVisitor extends MethodVisitor {

    public LifeTestOnCreateMethodVisitor(MethodVisitor mv) {
        super(Opcodes.ASM4, mv);
    }

    @Override
    public void visitCode() {
        super.visitCode();
        //方法执行前插入,此处为ASM Bytecode比较出的差异代码
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
        mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "", "()V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn("    onCreate");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false);
        mv.visitInsn(POP);
    }

    @Override
    public void visitInsn(int opcode) {
        super.visitInsn(opcode);
    }
}

LifeTestOnDestroyMethodVisitor.java
package com.example.testplugin;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.POP;

public class LifeTestOnDestroyMethodVisitor extends MethodVisitor {

    public LifeTestOnDestroyMethodVisitor(MethodVisitor mv) {
        super(Opcodes.ASM4, mv);
    }

    @Override
    public void visitCode() {
        super.visitCode();
        //方法执行前插入,此处为ASM Bytecode比较出的差异代码
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
        mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "", "()V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn("    onDestory");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false);
        mv.visitInsn(POP);
    }



    @Override
    public void visitInsn(int opcode) {
        super.visitInsn(opcode);
    }

}

8.重新 uploadArchives插件,看log输出
image.png
9.运行demo,并退出demo查看log输出
image.png
10.到这里说明我们的代码生效了,下面我们通过dex2jar以及jd-gui反编译我们的dex文件看下具体插入的代码
image.png

最后附上:Demo传送门

你可能感兴趣的:(自定义gradle插件&ASM字节码插入)