capacitor-dex-editor 0.0.19 → 0.0.20

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.
@@ -1736,25 +1736,71 @@ public class DexManager {
1736
1736
 
1737
1737
  Log.d(TAG, "Merged DEX written to: " + mergedDexFile.getAbsolutePath());
1738
1738
 
1739
- // 2. 直接替换 APK 中的 DEX(使用 RandomAccessFile 原地修改,避免复制整个 APK)
1740
- // 简化方案:只保存修改后的 DEX 到输出目录,让用户手动替换或使用其他工具
1741
- java.io.File outputDir = new java.io.File(new java.io.File(apkPath).getParent(), "dex_modified");
1742
- if (!outputDir.exists()) {
1743
- outputDir.mkdirs();
1739
+ zipFile.close();
1740
+
1741
+ // 2. 直接替换 APK 中的 DEX(MT 管理器的方式)
1742
+ // 使用 ZipOutputStream 重新写入 APK,只替换 DEX 部分
1743
+ java.io.File apkFile = new java.io.File(apkPath);
1744
+ java.io.File tempApkFile = new java.io.File(apkPath + ".tmp");
1745
+
1746
+ Log.d(TAG, "Replacing DEX in APK...");
1747
+
1748
+ java.util.zip.ZipFile originalZip = new java.util.zip.ZipFile(apkFile);
1749
+ java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(
1750
+ new java.io.BufferedOutputStream(new java.io.FileOutputStream(tempApkFile)));
1751
+
1752
+ // 设置不压缩,加快速度
1753
+ zos.setMethod(java.util.zip.ZipOutputStream.STORED);
1754
+
1755
+ java.util.Enumeration<? extends java.util.zip.ZipEntry> entries = originalZip.entries();
1756
+ while (entries.hasMoreElements()) {
1757
+ java.util.zip.ZipEntry entry = entries.nextElement();
1758
+
1759
+ if (entry.getName().equals(dexPath)) {
1760
+ // 替换 DEX 文件
1761
+ byte[] dexBytes = readFileBytes(mergedDexFile);
1762
+ java.util.zip.ZipEntry newEntry = new java.util.zip.ZipEntry(dexPath);
1763
+ newEntry.setMethod(java.util.zip.ZipEntry.STORED);
1764
+ newEntry.setSize(dexBytes.length);
1765
+ newEntry.setCompressedSize(dexBytes.length);
1766
+ newEntry.setCrc(calculateCrc32(dexBytes));
1767
+ zos.putNextEntry(newEntry);
1768
+ zos.write(dexBytes);
1769
+ zos.closeEntry();
1770
+ } else {
1771
+ // 复制原条目(保持原压缩方式)
1772
+ java.util.zip.ZipEntry newEntry = new java.util.zip.ZipEntry(entry);
1773
+ zos.putNextEntry(newEntry);
1774
+ if (!entry.isDirectory()) {
1775
+ java.io.InputStream is = originalZip.getInputStream(entry);
1776
+ byte[] buf = new byte[8192];
1777
+ int len;
1778
+ while ((len = is.read(buf)) != -1) {
1779
+ zos.write(buf, 0, len);
1780
+ }
1781
+ is.close();
1782
+ }
1783
+ zos.closeEntry();
1784
+ }
1744
1785
  }
1745
1786
 
1746
- // 复制修改后的 DEX 到输出目录
1747
- java.io.File outputDexFile = new java.io.File(outputDir, dexPath);
1748
- copyFile(mergedDexFile, outputDexFile);
1787
+ zos.close();
1788
+ originalZip.close();
1749
1789
 
1750
- Log.d(TAG, "Modified DEX saved to: " + outputDexFile.getAbsolutePath());
1790
+ // 用临时文件替换原文件
1791
+ if (!apkFile.delete()) {
1792
+ Log.e(TAG, "Failed to delete original APK");
1793
+ }
1794
+ if (!tempApkFile.renameTo(apkFile)) {
1795
+ // 如果 rename 失败,尝试复制
1796
+ copyFile(tempApkFile, apkFile);
1797
+ tempApkFile.delete();
1798
+ }
1751
1799
 
1752
- zipFile.close();
1800
+ Log.d(TAG, "APK updated successfully: " + apkPath);
1753
1801
 
1754
1802
  result.put("success", true);
1755
- result.put("message", "Smali 编译成功!修改后的 DEX 已保存");
1756
- result.put("modifiedDexPath", outputDexFile.getAbsolutePath());
1757
- result.put("note", "请使用 MT 管理器或其他工具将修改后的 DEX 替换到 APK 中");
1803
+ result.put("message", "Smali 保存成功!APK 已更新");
1758
1804
 
1759
1805
  // 清理临时文件
1760
1806
  cleanupTempDir(tempDir);
@@ -1768,6 +1814,30 @@ public class DexManager {
1768
1814
  return result;
1769
1815
  }
1770
1816
 
1817
+ /**
1818
+ * 读取文件为字节数组
1819
+ */
1820
+ private byte[] readFileBytes(java.io.File file) throws java.io.IOException {
1821
+ java.io.FileInputStream fis = new java.io.FileInputStream(file);
1822
+ java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
1823
+ byte[] buffer = new byte[8192];
1824
+ int len;
1825
+ while ((len = fis.read(buffer)) != -1) {
1826
+ baos.write(buffer, 0, len);
1827
+ }
1828
+ fis.close();
1829
+ return baos.toByteArray();
1830
+ }
1831
+
1832
+ /**
1833
+ * 计算 CRC32
1834
+ */
1835
+ private long calculateCrc32(byte[] data) {
1836
+ java.util.zip.CRC32 crc = new java.util.zip.CRC32();
1837
+ crc.update(data);
1838
+ return crc.getValue();
1839
+ }
1840
+
1771
1841
  /**
1772
1842
  * 复制文件
1773
1843
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-dex-editor",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
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",