浏览代码

update with new library

yayan 9 月之前
父节点
当前提交
df04267ef8

+ 1 - 1
build.gradle

@@ -6,7 +6,7 @@ plugins {
 }
 
 group = 'io.nexilis'
-version = '3.3.7'
+version = '3.3.7-k'
 
 compileJava {
     sourceCompatibility = '1.8'

+ 8 - 0
src/main/java/io/nexilis/debug/Activity.java

@@ -0,0 +1,8 @@
+package io.nexilis.debug;
+
+public abstract class Activity {
+
+    public void onCreate() {
+
+    }
+}

+ 26 - 0
src/main/java/io/nexilis/debug/Debug.java

@@ -0,0 +1,26 @@
+package io.nexilis.debug;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
+
+public class Debug {
+
+    public static void main(String[] args) {
+        String build = "C:\\Users\\yayan\\Documents\\11122024\\nxsample\\android\\app\\build";
+        String fileName = "MainActivity";
+        try (Stream<Path> stream = Files.walk(Paths.get(build))) {
+//            stream.forEach(path -> System.out.println(">>> " + path.toString()));
+            stream.filter(path -> path.toString().endsWith(fileName + ".class"))
+                    .forEach(classFile -> {
+                        System.out.println("> Task :" + ":filter:" + classFile);
+                    });
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}

+ 2 - 2
src/main/java/io/nexilis/debug/LibraryInjector.java

@@ -13,7 +13,7 @@ import java.nio.file.Paths;
 public class LibraryInjector {
 
     public static void main(String[] args) {
-        inject("C:\\Users\\yayan\\IdeaProjects\\streamshield\\build\\classes\\java\\main\\io\\nexilis\\debug\\MainActivity.class");
+        inject(".\\build\\classes\\java\\main\\io\\nexilis\\debug\\MainActivity.class");
     }
 
     public static void inject(String input) {
@@ -24,7 +24,7 @@ public class LibraryInjector {
             classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
 
             byte[] modifiedClass = classWriter.toByteArray();
-            try (FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\yayan\\IdeaProjects\\streamshield\\build\\classes\\java\\main\\io\\nexilis\\debug\\MainActivity-mod.class")) {
+            try (FileOutputStream fileOutputStream = new FileOutputStream(".\\build\\classes\\java\\main\\io\\nexilis\\debug\\MainActivityMod.class")) {
                 fileOutputStream.write(modifiedClass);
             }
         } catch (IOException e) {

+ 1 - 4
src/main/java/io/nexilis/debug/MainActivity.java

@@ -1,8 +1,5 @@
 package io.nexilis.debug;
 
-public class MainActivity {
+public class MainActivity extends Activity {
 
-    protected void onCreate() {
-        System.out.println();
-    }
 }

+ 3 - 2
src/main/java/io/nexilis/transformer/InjectProtectionTask.java

@@ -34,11 +34,12 @@ public class InjectProtectionTask extends DefaultTask {
     @TaskAction
     public void injectProtection() throws IOException {
         Path inputPath = inputDir.getAsFile().get().toPath();
-        String fileName = inputPath.getFileName().toString();
+        String fileName = inputPath.getFileName().toString().split("\\.")[0];
         try (Stream<Path> stream = Files.walk(getProject().getLayout().getBuildDirectory().get().getAsFile().toPath())) {
-            stream.filter(path -> path.toString().endsWith(fileName.replace(fileName.split("\\.")[1],".class")))
+            stream.filter(path -> path.toString().endsWith(fileName + ".class"))
                     .forEach(classFile -> {
                         try {
+                            System.out.println("> Task :" + getName() + ":filter:" + classFile);
                             injectProtectionIntoClass(classFile);
                         } catch (IOException e) {
                             throw new UncheckedIOException(e);

+ 32 - 6
src/main/java/io/nexilis/transformer/ProtectionInjector.java

@@ -8,8 +8,12 @@ import org.objectweb.asm.commons.AdviceAdapter;
 
 public class ProtectionInjector extends ClassVisitor {
 
+    private String superName;
+
     private final Project project;
 
+    private boolean isOnCreateExist = false;
+
     public ProtectionInjector(ClassVisitor cv, Project project) {
         super(Opcodes.ASM9, cv);
         this.project = project;
@@ -18,6 +22,7 @@ public class ProtectionInjector extends ClassVisitor {
     @Override
     public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
         String[] newInterfaces = append(interfaces, "io/nexilis/ui/Callback");
+        this.superName = superName;
         super.visit(version, access, name, signature, superName, newInterfaces);
     }
 
@@ -32,6 +37,7 @@ public class ProtectionInjector extends ClassVisitor {
     public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
         MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
         if ("onCreate".equals(name) && "(Landroid/os/Bundle;)V".equals(descriptor)) {
+            isOnCreateExist = true;
             return new InjectDetectionLibraryMethodVisitor(api, mv, access, name, descriptor, project);
         }
         return mv;
@@ -54,17 +60,37 @@ public class ProtectionInjector extends ClassVisitor {
         @Override
         protected void onMethodExit(int opcode) {
             super.onMethodExit(opcode);
-            mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.appId") == null ? "" : project.getProperties().get("nexilis.appId")) : "");
-            mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.account") == null ? "" : project.getProperties().get("nexilis.account")) : "");
-            mv.visitVarInsn(ALOAD, 0);
-            mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.floatingMode") == null ? 0 : project.getProperties().get("nexilis.floatingMode")) : 0);
-            mv.visitVarInsn(ALOAD, 0);
-            mv.visitMethodInsn(INVOKESTATIC, "io/nexilis/ui/API", "connect", "(Ljava/lang/String;Ljava/lang/String;Landroid/app/Activity;ILio/nexilis/ui/Callback;)V", false);
+            injectConnect(mv, project);
         }
     }
 
+    private static void injectConnect(MethodVisitor mv, Project project) {
+        mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.appId") == null ? "" : project.getProperties().get("nexilis.appId")) : "");
+        mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.account") == null ? "" : project.getProperties().get("nexilis.account")) : "");
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitLdcInsn(project != null ? (project.getProperties().get("nexilis.floatingMode") == null ? 0 : Integer.parseInt(String.valueOf(project.getProperties().get("nexilis.floatingMode")))) : 0);
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "io/nexilis/ui/API", "connect", "(Ljava/lang/String;Ljava/lang/String;Landroid/app/Activity;ILio/nexilis/ui/Callback;)V", false);
+    }
+
     @Override
     public void visitEnd() {
+        if (!isOnCreateExist) {
+            MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "onCreate", "(Landroid/os/Bundle;)V", null, null);
+            if (mv != null) {
+                mv.visitCode();
+
+                mv.visitVarInsn(Opcodes.ALOAD, 0);
+                mv.visitVarInsn(Opcodes.ALOAD, 1);
+                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "onCreate", "(Landroid/os/Bundle;)V", false);
+
+                injectConnect(mv, project);
+
+                mv.visitInsn(Opcodes.RETURN);
+                mv.visitMaxs(1, 1);
+                mv.visitEnd();
+            }
+        }
         MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "onSuccess", "(Ljava/lang/String;)V", null, null);
         if (mv != null) {
             mv.visitCode();