weiguo 7 years ago
parent
commit
fdd18a410c

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

@@ -1,6 +1,9 @@
 package com.uddream.plugin;
 
+import android.content.DialogInterface;
+import android.support.design.widget.TabLayout;
 import android.view.View;
+import android.widget.AdapterView;
 import android.widget.CompoundButton;
 import android.widget.Toast;
 
@@ -20,6 +23,26 @@ public class Agent {
         Toast.makeText(view.getContext(), "agent click listener", Toast.LENGTH_LONG).show();
     }
 
+    public static void onItemClickPre(AdapterView<?> parent, View view, int position, long id) {
+        Toast.makeText(view.getContext(), "agent click listener", Toast.LENGTH_LONG).show();
+    }
+
+    public static void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+        Toast.makeText(view.getContext(), "agent click listener", Toast.LENGTH_LONG).show();
+    }
+
+    public static void onClickPre(DialogInterface dialog, int which) {
+    }
+
+    public static void onClick(DialogInterface dialog, int which) {
+    }
+
+    public static void onTabSelectedPre(TabLayout.Tab tab) {
+    }
+
+    public static void onTabSelected(TabLayout.Tab tab) {
+    }
+
     public static void onCheckedChangedPre(CompoundButton view, boolean isChecked) {
         Toast.makeText(view.getContext(), "agent check pre listener " + isChecked, Toast.LENGTH_LONG).show();
     }

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

@@ -1,13 +1,18 @@
 package com.uddream.plugin;
 
+import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
+import android.support.design.widget.TabLayout;
+import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.util.Log;
 import android.view.View;
+import android.widget.AdapterView;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
+import android.widget.ListView;
 
 import com.bumptech.glide.Glide;
 import com.uddream.library.Test;
@@ -35,6 +40,38 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
         new CheckBox(this).setOnCheckedChangeListener(this);
         Log.e("package", Agent.packageName + "_");
         Log.e("package", Agent.isPluginEnable + "_");
+
+        new ListView(this).setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                //nothing
+                Log.e("package", Agent.isPluginEnable + "_");
+            }
+        });
+
+        new AlertDialog.Builder(this).setNegativeButton("", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+
+            }
+        });
+
+        new TabLayout(this).setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
+            @Override
+            public void onTabSelected(TabLayout.Tab tab) {
+
+            }
+
+            @Override
+            public void onTabUnselected(TabLayout.Tab tab) {
+
+            }
+
+            @Override
+            public void onTabReselected(TabLayout.Tab tab) {
+
+            }
+        });
     }
 
     @Override

+ 30 - 0
buildSrc/build.gradle

@@ -10,4 +10,34 @@ dependencies {
     compile localGroovy()
     compile 'org.ow2.asm:asm:5.2'
     compile 'com.android.tools.build:gradle:2.1.0'
+}
+
+group = "com.android.ares"
+version = '2.3.0'
+
+uploadArchives {
+    /**
+     *  请在根目录中添加local.properties文件,如果存在该文件,在该文件最后追加如下内容(包括用户名、密码、仓库地址)
+     * userName=admin
+     * password=*****
+     * url=http://10.0.18.175/nexus/content/repositories/android/
+     */
+    def url = "https://nexus.uddream.cn/android"
+    def userName = "admin"
+    def password = "admin"
+    File file = project.rootProject.file("local.properties")
+    if (file.exists()) {
+        Properties properties = new Properties()
+        properties.load(file.newDataInputStream())
+        url = properties.getProperty("url")
+        userName = properties.getProperty("userName")
+        password = properties.getProperty("password")
+    }
+
+    repositories.mavenDeployer {
+        repository(url: url) {
+            authentication(userName: userName, password: password)
+            pom.artifactId = "gradle-agent"
+        }
+    }
 }

+ 10 - 3
buildSrc/src/main/groovy/com/gradle/ModifyClassTransform.groovy

@@ -49,6 +49,7 @@ public abstract class ModifyClassTransform extends Transform {
 
     @Override
     void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
+        long time = System.currentTimeMillis()
         //delete all
         transformInvocation.outputProvider.deleteAll()
 
@@ -56,8 +57,12 @@ public abstract class ModifyClassTransform extends Transform {
         transformInvocation.inputs.each { input ->
             input.directoryInputs.each { dirInput ->
                 if (transformInvocation.incremental) {
-                    dirInput.changedFiles.each { changedFile ->
-                        handleChangedFile(changedFile.key)
+//                    待修复bug
+//                    dirInput.changedFiles.each { changedFile ->
+//                        handleChangedFile(changedFile.key)
+//                    }
+                    dirInput.file.eachFileRecurse { File file ->
+                        handleChangedFile(file)
                     }
                 } else {
                     dirInput.file.eachFileRecurse { File file ->
@@ -83,6 +88,8 @@ public abstract class ModifyClassTransform extends Transform {
                 FileUtils.copyFile(jarInput.file, file)
             }
         }
+
+        this.project.logger.error(":" + this.project.name + ":" + getName() + " consume time " + (System.currentTimeMillis() - time) + "ms")
     }
 
     /**
@@ -108,7 +115,7 @@ public abstract class ModifyClassTransform extends Transform {
      * @param msg
      */
     protected void debug(def msg) {
-        this.logger.lifecycle(":" + this.project.name + ":" + msg)
+        //this.logger.lifecycle(":" + this.project.name + ":" + msg)
     }
 
     /**

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

@@ -16,6 +16,11 @@ public class AresTransform extends ModifyClassTransform {
     }
 
     @Override
+    String getName() {
+        return "AresPlugin"
+    }
+
+    @Override
     protected void handleChangedFile(File input) {
         String path = input.absolutePath.replace("/", "\\")
 

+ 22 - 0
buildSrc/src/main/groovy/com/gradle/ares/AresValue.groovy

@@ -0,0 +1,22 @@
+package com.gradle.ares;
+
+/**
+ * Created by Glen on 2017/2/8.
+ */
+
+public class AresValue {
+    /**
+     * define agent
+     */
+    public final static String AgentClass = "com/uddream/plugin/Agent";
+//    public final static String AgentClass = "com/peony/framework/ares/core/AresAgent";
+    public final static String AgentPluginField = "isPluginEnable";
+    public final static String AgentPackageField = "packageName";
+
+    /**
+     * define lifecycle
+     */
+    public final static String LifeClass = "com/uddream/plugin/LifeCycleDelegate";
+//    public final static String LifeClass = "com/peony/framework/ares/core/LifeCycleDelegate";
+    public final static String GetLifeMethod = "\$getLifeCycleAresDelegate";
+}

+ 14 - 7
buildSrc/src/main/groovy/com/gradle/ares/ClassAdapter.groovy

@@ -17,6 +17,8 @@ public class ClassAdapter extends ClassVisitor {
     private Logger logger;
 
     private boolean modify;
+
+    private int access;
     private String className;
     private String superName;
     private String[] interfaces;
@@ -34,13 +36,14 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
         super.visit(version, access, name, signature, superName, interfaces)
+        this.access = access
         this.className = name
         this.superName = superName
         this.interfaces = interfaces
 
         //初始化
-        this.isAdAgent = !MethodAgentVisitor.AgentClass.equals(name)
-        this.isModifyField = MethodAgentVisitor.AgentClass.equals(name)
+        this.isAdAgent = !AresValue.AgentClass.equals(name)
+        this.isModifyField = AresValue.AgentClass.equals(name)
         for (String su : MethodLifeCycleVisitor.SuperClass) {
             if (su.equals(superName)) {
                 addLifecycleSet = new HashSet<>()
@@ -53,17 +56,17 @@ public class ClassAdapter extends ClassVisitor {
     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.className, this.superName, name, desc)
+            def agent = MethodAgentVisitor.getAgent(this.access, this.className, this.superName, name, desc)
             if (agent != null) {
                 modify = true
-                debug("method:" + access + "_" + name + "_" + desc)
                 mv = new MethodAgentVisitor(mv, agent)
             }
         }
 
         if (addLifecycleSet != null) {
-            def agent = MethodLifeCycleVisitor.getAgent(this.className, this.superName, name, desc)
+            def agent = MethodLifeCycleVisitor.getAgent(this.access, this.className, this.superName, name, desc)
             if (agent != null) {
                 modify = true
                 debug("lifecycle:" + access + "_" + name + "_" + desc)
@@ -77,12 +80,12 @@ public class ClassAdapter extends ClassVisitor {
     @Override
     FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
         if (isModifyField) {
-            if (MethodAgentVisitor.AgentPackageField.equals(name)) {
+            if (AresValue.AgentPackageField.equals(name)) {
                 modify = true
                 debug("field:" + access + "_" + name + "_" + desc)
                 return super.visitField(access, name, desc, signature, getPackageName())
             }
-            if (MethodAgentVisitor.AgentPluginFileld.equals(name)) {
+            if (AresValue.AgentPluginField.equals(name)) {
                 modify = true
                 debug("field:" + access + "_" + name + "_" + desc)
                 return super.visitField(access, name, desc, signature, true)
@@ -103,6 +106,10 @@ public class ClassAdapter extends ClassVisitor {
         return modify
     }
 
+    /**
+     * 获取xml定义包名
+     * @return
+     */
     private String getPackageName() {
         String root = this.project.buildDir.getParentFile().absolutePath
         String xml = root + "/src/main/" + "AndroidManifest.xml"

+ 14 - 19
buildSrc/src/main/groovy/com/gradle/ares/MethodAgentVisitor.groovy

@@ -7,19 +7,21 @@ import org.objectweb.asm.Opcodes
  * Created by Glen on 2017/2/8.
  */
 
-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 class MethodAgentVisitor extends MethodBaseVisitor {
 
     public final static MethodModel[] Modify = [
-            new MethodModel("onClick", "(Landroid/view/View;)V"),
-            new MethodModel("onCheckedChanged", "(Landroid/widget/CompoundButton;Z)V")
+            new MethodModel("onClick", "(Landroid/view/View;)V", 1),//view
+            new MethodModel("onClick", "(Landroid/content/DialogInterface;I)V", 2),//dialog
+            new MethodModel("onItemClick", "(Landroid/widget/AdapterView;Landroid/view/View;IJ)V", 4),//list view
+            new MethodModel("onTabSelected", "(Landroid/support/design/widget/TabLayout\$Tab;)V", 1),//design lib tab layout
+            new MethodModel("onCheckedChanged", "(Landroid/widget/CompoundButton;Z)V", 2)
     ] as MethodModel[]
 
-    public static MethodModel getAgent(String className, String superName, String name, String desc) {
+    public
+    static MethodModel getAgent(int access, String className, String superName, String name, String desc) {
         for (MethodModel item : Modify) {
             if (item.name.equals(name) && item.desc.equals(desc)) {
+                item.access = access
                 item.className = className
                 item.superName = superName
                 return item
@@ -28,29 +30,22 @@ public class MethodAgentVisitor extends MethodVisitor {
         return null
     }
 
-    private int pCount;
-    private MethodModel agent;
-
     MethodAgentVisitor(MethodVisitor mv, MethodModel agent) {
-        super(Opcodes.ASM5, mv)
-        this.agent = agent
-        for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
-            if (!item.equals("")) this.pCount++
-        }
+        super(mv, agent)
     }
 
     @Override
     void visitCode() {
         super.visitCode()
-        for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
-        visitMethodInsn(Opcodes.INVOKESTATIC, AgentClass, agent.name + "Pre", agent.desc, false)
+        for (int i = 0; i < this.agent.count; i++) loadArg(i)
+        visitMethodInsn(Opcodes.INVOKESTATIC, AresValue.AgentClass, this.agent.name + "Pre", this.agent.desc, false)
     }
 
     @Override
     public void visitInsn(int opcode) {
         if (opcode == Opcodes.RETURN) {
-            for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
-            visitMethodInsn(Opcodes.INVOKESTATIC, AgentClass, agent.name, agent.desc, false)
+            for (int i = 0; i < this.agent.count; i++) loadArg(i)
+            visitMethodInsn(Opcodes.INVOKESTATIC, AresValue.AgentClass, this.agent.name, this.agent.desc, false)
         }
         super.visitInsn(opcode);
     }

+ 18 - 0
buildSrc/src/main/groovy/com/gradle/ares/MethodBaseVisitor.groovy

@@ -0,0 +1,18 @@
+package com.gradle.ares
+
+import org.objectweb.asm.MethodVisitor
+import org.objectweb.asm.Opcodes
+import org.objectweb.asm.commons.GeneratorAdapter;
+
+/**
+ * Created by Glen on 2017/2/8.
+ */
+
+public class MethodBaseVisitor extends GeneratorAdapter {
+    protected MethodModel agent;
+
+    MethodBaseVisitor(MethodVisitor mv, MethodModel agent) {
+        super(Opcodes.ASM5, mv, agent.access, agent.name, agent.desc)
+        this.agent = agent
+    }
+}

+ 32 - 43
buildSrc/src/main/groovy/com/gradle/ares/MethodLifeCycleVisitor.groovy

@@ -5,14 +5,13 @@ import org.objectweb.asm.FieldVisitor
 import org.objectweb.asm.Label
 import org.objectweb.asm.MethodVisitor
 import org.objectweb.asm.Opcodes
+import org.objectweb.asm.commons.GeneratorAdapter
 
 /**
  * Created by Glen on 2017/2/8.
  */
 
-public class MethodLifeCycleVisitor extends MethodVisitor {
-    public final static String LifeClass = "com/uddream/plugin/LifeCycleDelegate";
-    public final static String GetLifeMethod = "\$getLifeCycleAresDelegate";
+public class MethodLifeCycleVisitor extends MethodBaseVisitor {
 
     public final static String[] SuperClass = [
             "android/app/Activity",
@@ -27,19 +26,21 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
     ] as String[]
 
     public final static MethodModel[] Modify = [
-            new MethodModel("onCreate", "(Landroid/os/Bundle;)V"),
-            new MethodModel("onResume", "()V"),
-            new MethodModel("onPause", "()V"),
-            new MethodModel("onDestroy", "()V"),
-            new MethodModel("onSaveInstanceState", "(Landroid/os/Bundle;)V"),
-            new MethodModel("onRestoreInstanceState", "(Landroid/os/Bundle;)V"),
-            new MethodModel("setUserVisibleHint", "(Z)V"),
-            new MethodModel("onHiddenChanged", "(Z)V")
+            new MethodModel("onCreate", "(Landroid/os/Bundle;)V", 1),
+            new MethodModel("onResume", "()V", 0),
+            new MethodModel("onPause", "()V", 0),
+            new MethodModel("onDestroy", "()V", 0),
+            new MethodModel("onSaveInstanceState", "(Landroid/os/Bundle;)V", 1),
+            new MethodModel("onRestoreInstanceState", "(Landroid/os/Bundle;)V", 1),
+            new MethodModel("setUserVisibleHint", "(Z)V", 1),
+            new MethodModel("onHiddenChanged", "(Z)V", 1)
     ] as MethodModel[]
 
-    public static MethodModel getAgent(String className, String superName, String name, String desc) {
+    public
+    static MethodModel getAgent(int access, String className, String superName, String name, String desc) {
         for (MethodModel item : Modify) {
             if (item.name.equals(name) && item.desc.equals(desc)) {
+                item.access = access
                 item.className = className
                 item.superName = superName
                 return item
@@ -54,24 +55,20 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
         for (MethodModel agent : Modify) {
             if (!agentSet.contains(agent)) {
                 //method
-                int count = 0;
-                for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
-                    if (!item.equals("")) count++
-                }
-
                 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, agent.name, agent.desc, null, null)
-                mv.visitCode()
-                mv.visitVarInsn(Opcodes.ALOAD, 0)
-                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, GetLifeMethod, "()L" + LifeClass + ";", false)
-                for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
-                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
+                GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, mv, Opcodes.ACC_PUBLIC, agent.name, agent.desc)
+                adapter.visitCode()
+                adapter.loadThis()
+                adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, AresValue.GetLifeMethod, "()L" + AresValue.LifeClass + ";", false)
+                for (int i = 0; i < agent.count; i++) adapter.loadArg(i)
+                adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, AresValue.LifeClass, agent.name, agent.desc, false)
 
-                mv.visitVarInsn(Opcodes.ALOAD, 0)
-                for (int i = 1; i <= count; i++) mv.visitVarInsn(Opcodes.ALOAD, i)
-                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, agent.name, agent.desc, false)
-                mv.visitInsn(Opcodes.RETURN)
-                mv.visitMaxs(0, 0)
-                mv.visitEnd()
+                adapter.loadThis()
+                for (int i = 0; i < agent.count; i++) adapter.loadArg(i)
+                adapter.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, agent.name, agent.desc, false)
+                adapter.visitInsn(Opcodes.RETURN)
+                adapter.visitMaxs(0, 0)
+                adapter.visitEnd()
             }
         }
     }
@@ -83,12 +80,12 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
     private static void addLifeCycleFieldMethod(String className, ClassVisitor cv) {
         //desc
         String fieldName = "\$lifeCycleAresDelegate"
-        String fieldDesc = "L" + LifeClass + ";"
+        String fieldDesc = "L" + AresValue.LifeClass + ";"
         //field
         FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC, fieldName, fieldDesc, null, null)
         fv.visitEnd()
         //method
-        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, GetLifeMethod, "()" + fieldDesc, null, null)
+        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, AresValue.GetLifeMethod, "()" + fieldDesc, null, null)
         mv.visitCode()
         mv.visitVarInsn(Opcodes.ALOAD, 0)
         mv.visitFieldInsn(Opcodes.GETFIELD, className, fieldName, fieldDesc)
@@ -96,7 +93,7 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
         mv.visitJumpInsn(Opcodes.IFNONNULL, label)
         mv.visitVarInsn(Opcodes.ALOAD, 0)
         mv.visitVarInsn(Opcodes.ALOAD, 0)
-        mv.visitMethodInsn(Opcodes.INVOKESTATIC, LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
+        mv.visitMethodInsn(Opcodes.INVOKESTATIC, AresValue.LifeClass, "create", "(Ljava/lang/Object;)" + fieldDesc, false)
         mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldName, fieldDesc)
         mv.visitLabel(label)
         mv.visitVarInsn(Opcodes.ALOAD, 0)
@@ -106,25 +103,17 @@ public class MethodLifeCycleVisitor extends MethodVisitor {
         mv.visitEnd()
     }
 
-    private int pCount;
-    private String className;
-    private MethodModel agent;
-
     MethodLifeCycleVisitor(MethodVisitor mv, MethodModel agent) {
-        super(Opcodes.ASM5, mv)
+        super(mv, agent)
         this.agent = agent
-        this.className = agent.className
-        for (def item : agent.desc.substring(1, agent.desc.indexOf(")")).split(";")) {
-            if (!item.equals("")) this.pCount++
-        }
     }
 
     @Override
     void visitCode() {
         super.visitCode()
         visitVarInsn(Opcodes.ALOAD, 0)
-        visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.className, GetLifeMethod, "()L" + LifeClass + ";", false)
-        for (int i = 1; i <= pCount; i++) visitVarInsn(Opcodes.ALOAD, i)
-        visitMethodInsn(Opcodes.INVOKEVIRTUAL, LifeClass, agent.name, agent.desc, false)
+        visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.agent.className, AresValue.GetLifeMethod, "()L" + AresValue.LifeClass + ";", false)
+        for (int i = 0; i < this.agent.count; i++) loadArg(i)
+        visitMethodInsn(Opcodes.INVOKEVIRTUAL, AresValue.LifeClass, this.agent.name, this.agent.desc, false)
     }
 }

+ 4 - 1
buildSrc/src/main/groovy/com/gradle/ares/MethodModel.groovy

@@ -5,14 +5,17 @@ package com.gradle.ares;
  */
 
 public class MethodModel {
+    int access;
     String className;
     String superName;
     String name;
     String desc;
+    int count;
 
-    MethodModel(String name, String desc) {
+    MethodModel(String name, String desc, int count) {
         this.name = name
         this.desc = desc
+        this.count = count
     }
 
     public String getName() {