Browse Source

fix 增量更新 bug

weiguo 7 years ago
parent
commit
772d23e8a1

+ 1 - 0
app/src/main/java/com/uddream/plugin/MainActivity.java

@@ -53,6 +53,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
             @Override
             public void onClick(DialogInterface dialog, int which) {
 
+
             }
         });
 

+ 46 - 28
buildSrc/src/main/groovy/com/gradle/ModifyClassTransform.groovy

@@ -2,11 +2,10 @@ package com.gradle
 
 import com.android.build.api.transform.*
 import com.google.common.collect.ImmutableSet
+import com.google.common.io.Files
 import org.apache.commons.codec.digest.DigestUtils
 import org.apache.commons.io.FileUtils
 import org.gradle.api.Project
-import org.gradle.api.logging.Logger
-import org.gradle.api.logging.Logging
 
 /**
  * Created by Glen on 2017/2/7.
@@ -14,11 +13,10 @@ import org.gradle.api.logging.Logging
 
 public abstract class ModifyClassTransform extends Transform {
     protected Project project;
-    protected Logger logger;
 
     public ModifyClassTransform(Project project) {
         this.project = project
-        this.logger = Logging.getLogger(ModifyClassTransform.class)
+        MyLogger.init(project.name)
     }
 
     @Override
@@ -50,42 +48,70 @@ public abstract class ModifyClassTransform extends Transform {
     @Override
     void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
         long time = System.currentTimeMillis()
-        //delete all
-        transformInvocation.outputProvider.deleteAll()
 
         // Collecting inputs.
         transformInvocation.inputs.each { input ->
             input.directoryInputs.each { dirInput ->
                 if (transformInvocation.incremental) {
-//                    待修复bug
-//                    dirInput.changedFiles.each { changedFile ->
-//                        handleChangedFile(changedFile.key)
-//                    }
-                    dirInput.file.eachFileRecurse { File file ->
-                        handleChangedFile(file)
+                    File dirOutput = transformInvocation.outputProvider.getContentLocation(dirInput.file.name, dirInput.contentTypes, dirInput.scopes, Format.DIRECTORY)
+                    if (!dirOutput.exists()) {
+                        dirOutput.mkdirs()
+                    }
+                    dirInput.changedFiles.each { changedFile ->
+                        File fileInput = changedFile.getKey()
+                        File fileOutput = new File(fileInput.getAbsolutePath().replace(
+                                dirInput.file.getAbsolutePath(), dirOutput.getAbsolutePath()))
+                        if (!fileOutput.exists()) {
+                            fileOutput.getParentFile().mkdirs()
+                        }
+                        switch (changedFile.value) {
+                            case Status.ADDED:
+                            case Status.CHANGED:
+                                if (fileInput.isDirectory()) {
+                                    return // continue.
+                                }
+                                handleChangedFile(fileInput)
+                                if (fileInput.getName().endsWith('.class')) {
+                                    Files.copy(fileInput, fileOutput)
+                                }
+                                break
+                            case Status.REMOVED:
+                                if (fileOutput.exists()) {
+                                    if (fileOutput.isDirectory()) {
+                                        fileOutput.deleteDir()
+                                    } else {
+                                        fileOutput.delete()
+                                    }
+                                }
+                                break
+                        }
                     }
                 } else {
                     dirInput.file.eachFileRecurse { File file ->
                         handleChangedFile(file)
                     }
+                    //output modify class file
+                    File file = transformInvocation.outputProvider.getContentLocation(dirInput.file.name, dirInput.contentTypes, dirInput.scopes, Format.DIRECTORY)
+                    if (file.exists()) file.deleteDir()
+                    FileUtils.copyDirectory(dirInput.file, file)
                 }
-
-                //output modify class file
-                File file = transformInvocation.outputProvider.getContentLocation(dirInput.file.name, dirInput.contentTypes, dirInput.scopes, Format.DIRECTORY)
-                FileUtils.copyDirectory(dirInput.file, file)
             }
             input.jarInputs.each { jarInput ->
+                File file = transformInvocation.outputProvider.getContentLocation(getUniqueFileName(jarInput.file), jarInput.contentTypes, jarInput.scopes, Format.JAR)
                 switch (jarInput.status) {
                     case Status.NOTCHANGED:
                         if (transformInvocation.incremental) break
                     case Status.ADDED:
                     case Status.CHANGED:
+                        file.deleteOnExit()
                         handleChangedFile(jarInput.file)
+                        //output modify jar file
+                        FileUtils.copyFile(jarInput.file, file)
+                        break
+                    case Status.REMOVED:
+                        file.deleteOnExit()
+                        break
                 }
-
-                //output modify jar file
-                File file = transformInvocation.outputProvider.getContentLocation(getUniqueFileName(jarInput.file), jarInput.contentTypes, jarInput.scopes, Format.JAR)
-                FileUtils.copyFile(jarInput.file, file)
             }
         }
 
@@ -111,14 +137,6 @@ public abstract class ModifyClassTransform extends Transform {
     }
 
     /**
-     * logger
-     * @param msg
-     */
-    protected void debug(def msg) {
-        //this.logger.lifecycle(":" + this.project.name + ":" + msg)
-    }
-
-    /**
      * 增量文件
      * @param input
      */

+ 21 - 0
buildSrc/src/main/groovy/com/gradle/MyLogger.groovy

@@ -0,0 +1,21 @@
+package com.gradle
+
+import org.gradle.api.logging.Logger;
+import org.gradle.api.logging.Logging;
+
+/**
+ * Created by Glen on 2017/2/9.
+ */
+
+public class MyLogger {
+    public static String tag = "app";
+    private final static Logger logger = Logging.getLogger(MyLogger.class);
+
+    public static void init(String tag) {
+        MyLogger.tag = tag
+    }
+
+    public static void lifecycle(String msg) {
+        logger.lifecycle(":" + tag + ":" + msg);
+    }
+}

+ 2 - 1
buildSrc/src/main/groovy/com/gradle/ares/AresTransform.groovy

@@ -2,6 +2,7 @@ package com.gradle.ares
 
 import com.gradle.JarZipUtil
 import com.gradle.ModifyClassTransform
+import com.gradle.MyLogger
 import org.gradle.api.Project
 import org.objectweb.asm.ClassReader
 import org.objectweb.asm.ClassWriter
@@ -25,7 +26,7 @@ public class AresTransform extends ModifyClassTransform {
         String path = input.absolutePath.replace("/", "\\")
 
         if (input.isFile()) {
-            debug("changed:" + path)
+            MyLogger.lifecycle("changed:" + path)
             if (path.endsWith(".class")) {
                 modifyClass(input)
             } else if (path.endsWith(".jar")) {

+ 8 - 15
buildSrc/src/main/groovy/com/gradle/ares/ClassAdapter.groovy

@@ -1,8 +1,7 @@
 package com.gradle.ares
 
+import com.gradle.MyLogger
 import org.gradle.api.Project
-import org.gradle.api.logging.Logger
-import org.gradle.api.logging.Logging
 import org.objectweb.asm.ClassVisitor
 import org.objectweb.asm.FieldVisitor
 import org.objectweb.asm.MethodVisitor
@@ -14,9 +13,6 @@ import org.objectweb.asm.Opcodes
 
 public class ClassAdapter extends ClassVisitor {
     private Project project;
-    private Logger logger;
-
-    private boolean modify;
 
     private int access;
     private String className;
@@ -27,10 +23,11 @@ public class ClassAdapter extends ClassVisitor {
     private boolean isModifyField;
     private Set<MethodModel> addLifecycleSet;
 
+    private boolean modify;
+
     public ClassAdapter(ClassVisitor cv, Project project) {
         super(Opcodes.ASM5, cv)
         this.project = project
-        this.logger = Logging.getLogger(ClassAdapter.class)
     }
 
     @Override
@@ -49,18 +46,18 @@ public class ClassAdapter extends ClassVisitor {
                 addLifecycleSet = new HashSet<>()
             }
         }
-        debug("class:" + this.className + "_" + this.superName + "_" + this.isAdAgent + "_" + this.isModifyField + "_" + (this.addLifecycleSet != null))
+        MyLogger.lifecycle("class:" + this.className + "_" + this.superName + "_" + this.interfaces)
     }
 
     @Override
     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions)
 
-        debug("method:" + access + "_" + name + "_" + desc)
         if (isAdAgent) {
             def agent = MethodAgentVisitor.getAgent(this.access, this.className, this.superName, name, desc)
             if (agent != null) {
                 modify = true
+                MyLogger.lifecycle("agent:" + name + "_" + desc)
                 mv = new MethodAgentVisitor(mv, agent)
             }
         }
@@ -69,7 +66,7 @@ public class ClassAdapter extends ClassVisitor {
             def agent = MethodLifeCycleVisitor.getAgent(this.access, this.className, this.superName, name, desc)
             if (agent != null) {
                 modify = true
-                debug("lifecycle:" + access + "_" + name + "_" + desc)
+                MyLogger.lifecycle("lifecycle:modify:" + name + "_" + desc)
                 mv = new MethodLifeCycleVisitor(mv, agent)
                 addLifecycleSet.add(agent)
             }
@@ -82,12 +79,12 @@ public class ClassAdapter extends ClassVisitor {
         if (isModifyField) {
             if (AresValue.AgentPackageField.equals(name)) {
                 modify = true
-                debug("field:" + access + "_" + name + "_" + desc)
+                MyLogger.lifecycle("field:" + name + "_" + desc)
                 return super.visitField(access, name, desc, signature, getPackageName())
             }
             if (AresValue.AgentPluginField.equals(name)) {
                 modify = true
-                debug("field:" + access + "_" + name + "_" + desc)
+                MyLogger.lifecycle("field:" + name + "_" + desc)
                 return super.visitField(access, name, desc, signature, true)
             }
         }
@@ -126,8 +123,4 @@ public class ClassAdapter extends ClassVisitor {
         }
         return null
     }
-
-    private void debug(def msg) {
-        this.logger.lifecycle(":" + this.project.name + ":" + msg)
-    }
 }

+ 3 - 0
buildSrc/src/main/groovy/com/gradle/ares/MethodLifeCycleVisitor.groovy

@@ -1,5 +1,6 @@
 package com.gradle.ares
 
+import com.gradle.MyLogger
 import org.objectweb.asm.ClassVisitor
 import org.objectweb.asm.FieldVisitor
 import org.objectweb.asm.Label
@@ -55,6 +56,8 @@ public class MethodLifeCycleVisitor extends MethodBaseVisitor {
         for (MethodModel agent : Modify) {
             if (!agentSet.contains(agent)) {
                 //method
+                MyLogger.lifecycle("lifecycle:add:" + agent.name + "_" + agent.desc)
+
                 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, agent.name, agent.desc, null, null)
                 GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, mv, Opcodes.ACC_PUBLIC, agent.name, agent.desc)
                 adapter.visitCode()