capacitor-dex-editor 0.0.73 → 0.0.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -641,28 +641,39 @@ public class DexManager {
641
641
  }
642
642
 
643
643
  /**
644
- * 设置方法的 Smali 代码
644
+ * 设置方法的 Smali 代码(优先使用 C++ 实现)
645
645
  */
646
646
  public void setMethodSmali(String sessionId, String className,
647
647
  String methodName, String methodSignature,
648
648
  String smaliCode) throws Exception {
649
649
  DexSession session = getSession(sessionId);
650
+
651
+ // 获取原类的 Smali 并替换方法
652
+ String classSmali = classToSmali(sessionId, className).getString("smali");
653
+ String modifiedSmali = replaceMethodInSmali(classSmali, methodName, methodSignature, smaliCode);
654
+
655
+ // 优先使用 C++ 实现
656
+ if (CppDex.isAvailable() && session.dexBytes != null) {
657
+ try {
658
+ byte[] newDexBytes = CppDex.modifyClass(session.dexBytes, className, modifiedSmali);
659
+ if (newDexBytes != null) {
660
+ session.dexBytes = newDexBytes;
661
+ session.modified = true;
662
+ Log.d(TAG, "Modified method via C++: " + className + "->" + methodName);
663
+ return;
664
+ }
665
+ } catch (Exception e) {
666
+ Log.w(TAG, "C++ modifyClass failed, fallback to Java", e);
667
+ }
668
+ }
669
+
670
+ // Java 回退实现
650
671
  ClassDef classDef = findClass(session, className);
651
-
652
672
  if (classDef == null) {
653
673
  throw new IllegalArgumentException("Class not found: " + className);
654
674
  }
655
-
656
- // 获取原类的 Smali
657
- String classSmali = classToSmali(sessionId, className).getString("smali");
658
-
659
- // 替换方法
660
- String modifiedSmali = replaceMethodInSmali(classSmali, methodName, methodSignature, smaliCode);
661
675
 
662
- // 重新编译
663
676
  ClassDef modifiedClass = compileSmaliToClass(modifiedSmali, session.originalDexFile.getOpcodes());
664
-
665
- // 更新会话
666
677
  session.removedClasses.add(className);
667
678
  session.modifiedClasses.add(modifiedClass);
668
679
  session.modified = true;
@@ -933,7 +944,7 @@ public class DexManager {
933
944
  }
934
945
 
935
946
  /**
936
- * 汇编 Smali 目录为 DEX
947
+ * 汇编 Smali 目录为 DEX(优先使用 C++ 实现)
937
948
  */
938
949
  public JSObject assemble(String smaliDir, String outputPath) throws Exception {
939
950
  File inputDir = new File(smaliDir);
@@ -944,7 +955,35 @@ public class DexManager {
944
955
  }
945
956
 
946
957
  outputFile.getParentFile().mkdirs();
958
+
959
+ // 优先使用 C++ 实现
960
+ if (CppDex.isAvailable()) {
961
+ try {
962
+ // 读取所有 smali 文件并合并
963
+ List<File> smaliFiles = collectSmaliFiles(inputDir);
964
+ StringBuilder allSmali = new StringBuilder();
965
+ for (File f : smaliFiles) {
966
+ allSmali.append(readFileContent(f));
967
+ allSmali.append("\n\n");
968
+ }
969
+
970
+ byte[] dexBytes = CppDex.smaliToDex(allSmali.toString());
971
+ if (dexBytes != null && dexBytes.length > 0) {
972
+ try (java.io.FileOutputStream fos = new java.io.FileOutputStream(outputFile)) {
973
+ fos.write(dexBytes);
974
+ }
975
+ JSObject result = new JSObject();
976
+ result.put("success", true);
977
+ result.put("outputPath", outputPath);
978
+ result.put("engine", "cpp");
979
+ return result;
980
+ }
981
+ } catch (Exception e) {
982
+ Log.w(TAG, "C++ smaliToDex failed, fallback to Java", e);
983
+ }
984
+ }
947
985
 
986
+ // Java 回退实现
948
987
  SmaliOptions options = new SmaliOptions();
949
988
  options.outputDexFile = outputPath;
950
989
 
@@ -959,6 +998,7 @@ public class DexManager {
959
998
  JSObject result = new JSObject();
960
999
  result.put("success", success);
961
1000
  result.put("outputPath", outputPath);
1001
+ result.put("engine", "java");
962
1002
  return result;
963
1003
  }
964
1004
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-dex-editor",
3
- "version": "0.0.73",
3
+ "version": "0.0.74",
4
4
  "description": "Capacitor-plugin-for-editing-DEX-files-in-APK",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",