weiguo 7 rokov pred
rodič
commit
ad709d362e

+ 2 - 0
app/src/main/java/com/uddream/plugin/Agent.java

@@ -9,6 +9,8 @@ import android.widget.Toast;
  */
 
 public class Agent {
+    public static boolean isPluginEnable;
+    public static String packageName;
 
     public static void onClickPre(View view) {
         Toast.makeText(view.getContext(), "agent click pre listener", Toast.LENGTH_LONG).show();

+ 3 - 5
app/src/main/java/com/uddream/plugin/MainActivity.java

@@ -4,6 +4,7 @@ import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
+import android.util.Log;
 import android.view.View;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
@@ -32,6 +33,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
         fab.setOnClickListener(new Test());
         fab.setOnClickListener(this);
         new CheckBox(this).setOnCheckedChangeListener(this);
+        Log.e("package", Agent.packageName + "_");
+        Log.e("package", Agent.isPluginEnable + "_");
     }
 
     @Override
@@ -40,11 +43,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
     }
 
     @Override
-    public void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-    }
-
-    @Override
     public void onClick(View v) {
         System.out.print("onClick listener");
     }

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

@@ -18,9 +18,9 @@ public class AresTransform extends ModifyClassTransform {
     @Override
     protected void handleChangedFile(File input) {
         String path = input.absolutePath.replace("/", "\\")
-        debug("changed:" + path)
 
         if (input.isFile()) {
+            debug("changed:" + path)
             if (path.endsWith(".class")) {
                 modifyClass(input)
             } else if (path.endsWith(".jar")) {
@@ -55,10 +55,6 @@ public class AresTransform extends ModifyClassTransform {
      * @return
      */
     private boolean modifyClass(File clazz) {
-        def str = clazz.absolutePath.replace("\\", "/")
-        if (str.endsWith(MethodAgentVisitor.AgentClass + ".class")) return
-        if (str.endsWith(MethodLifeCycleVisitor.LifeClass + ".class")) return
-
         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS)
         ClassReader cr = new ClassReader(clazz.bytes)
         ClassAdapter adapter = new ClassAdapter(cw, project)

+ 61 - 18
buildSrc/src/main/groovy/com/gradle/ares/ClassAdapter.groovy

@@ -21,7 +21,9 @@ public class ClassAdapter extends ClassVisitor {
     private String superName;
     private String[] interfaces;
 
-    private Set<MethodModel> addAgents;
+    private boolean isAdAgent;
+    private boolean isModifyField;
+    private Set<MethodModel> addLifecycleSet;
 
     public ClassAdapter(ClassVisitor cv, Project project) {
         super(Opcodes.ASM5, cv)
@@ -35,48 +37,89 @@ public class ClassAdapter extends ClassVisitor {
         this.className = name
         this.superName = superName
         this.interfaces = interfaces
-        debug("class:" + name + "_" + superName + "_" + interfaces)
+
+        //初始化
+        this.isAdAgent = !MethodAgentVisitor.AgentClass.equals(name)
+        this.isModifyField = MethodAgentVisitor.AgentClass.equals(name)
+        for (String su : MethodLifeCycleVisitor.SuperClass) {
+            if (su.equals(superName)) {
+                addLifecycleSet = new HashSet<>()
+            }
+        }
+        debug("class:" + this.className + "_" + this.superName + "_" + this.isAdAgent + "_" + this.isModifyField + "_" + (this.addLifecycleSet != null))
     }
 
     @Override
     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions)
-        def agent = MethodAgentVisitor.getAgent(this.className, this.superName, name, desc)
-        if (agent != null) {
-            modify = true
-            debug("method:" + access + "_" + name + "_" + desc)
-            mv = new MethodAgentVisitor(mv, agent)
-        }
 
-        agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc)
-        if (agent != null) {
-            debug("lifecycle:" + access + "_" + name + "_" + desc)
-            if (addAgents == null) addAgents = new HashSet<>();
-            addAgents.add(agent)
+        if (isAdAgent) {
+            def agent = MethodAgentVisitor.getAgent(this.className, this.superName, name, desc)
+            if (agent != null) {
+                modify = true
+                debug("method:" + access + "_" + name + "_" + desc)
+                mv = new MethodAgentVisitor(mv, agent)
+            }
+        }
 
-            modify = true
-            mv = new MethodLifeCycleVisitor(mv, agent)
+        if (addLifecycleSet != null) {
+            def agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc)
+            if (agent != null) {
+                modify = true
+                debug("lifecycle:" + access + "_" + name + "_" + desc)
+                mv = new MethodLifeCycleVisitor(mv, agent)
+                addLifecycleSet.add(agent)
+            }
         }
         return mv
     }
 
     @Override
     FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
+        if (isModifyField) {
+            if (MethodAgentVisitor.AgentPackageField.equals(name)) {
+                modify = true
+                debug("field:" + access + "_" + name + "_" + desc)
+                return super.visitField(access, name, desc, signature, getPackageName())
+            }
+            if (MethodAgentVisitor.AgentPluginFileld.equals(name)) {
+                modify = true
+                debug("field:" + access + "_" + name + "_" + desc)
+                return super.visitField(access, name, desc, signature, true)
+            }
+        }
         return super.visitField(access, name, desc, signature, value)
     }
 
     @Override
     void visitEnd() {
-        if (addAgents != null) {
-            MethodLifeCycleVisitor.addMethods(addAgents, this.className, this.superName, this.cv)
+        if (addLifecycleSet != null) {
+            MethodLifeCycleVisitor.addMethods(addLifecycleSet, this.className, this.superName, this.cv)
         }
         super.visitEnd()
     }
 
-    boolean getModify() {
+    public boolean getModify() {
         return modify
     }
 
+    private String getPackageName() {
+        String root = this.project.buildDir.getParentFile().absolutePath
+        String xml = root + "/src/main/" + "AndroidManifest.xml"
+        def file = new File(xml);
+        def lines = file.readLines()
+
+        for (line in lines) {
+            line = line.replace(" ", "")
+            if (line.contains("package")) {
+                line = line.replace("package=\"", "")
+                String packageName = line.substring(0, line.indexOf("\""))
+                return packageName
+            }
+        }
+        return null
+    }
+
     private void debug(def msg) {
         this.logger.lifecycle(":" + this.project.name + ":" + msg)
     }

+ 2 - 0
buildSrc/src/main/groovy/com/gradle/ares/MethodAgentVisitor.groovy

@@ -9,6 +9,8 @@ import org.objectweb.asm.Opcodes
 
 public class MethodAgentVisitor extends MethodVisitor {
     public final static String AgentClass = "com/uddream/plugin/Agent";
+    public final static String AgentPluginFileld = "isPluginEnable";
+    public final static String AgentPackageField = "packageName";
 
     public final static MethodModel[] Modify = [
             new MethodModel("onClick", "(Landroid/view/View;)V"),

+ 7 - 11
buildSrc/src/main/groovy/com/gradle/ares/MethodLifeCycleVisitor.groovy

@@ -38,22 +38,18 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
     ] as MethodModel[]
 
     public static MethodModel getAgent(String className, String superName, String name, String desc) {
-        for (String s : SuperClass) {
-            if (s.equals(superName)) {
-                for (MethodModel item : Modify) {
-                    if (item.name.equals(name) && item.desc.equals(desc)) {
-                        item.className = className
-                        item.superName = superName
-                        return item
-                    }
-                }
-                break
+        for (MethodModel item : Modify) {
+            if (item.name.equals(name) && item.desc.equals(desc)) {
+                item.className = className
+                item.superName = superName
+                return item
             }
         }
         return null
     }
 
-    public static void addMethods(Set<MethodModel> agentSet, String className, String superName, ClassVisitor cv) {
+    public
+    static void addMethods(Set<MethodModel> agentSet, String className, String superName, ClassVisitor cv) {
         addLifeCycleFieldMethod(className, cv)
         for (MethodModel agent : Modify) {
             if (!agentSet.contains(agent)) {