|
| 1 | +package cn.kaicity.common.repository |
| 2 | + |
| 3 | +import cn.kaicity.common.bean.InputBean |
| 4 | +import net.lingala.zip4j.ZipFile |
| 5 | +import java.io.File |
| 6 | + |
| 7 | +object ApkObf { |
| 8 | + |
| 9 | + suspend fun run(inputBean: InputBean, logCallback: (log: String) -> Unit) { |
| 10 | + val workDir = File(File(inputBean.output).parentFile, "temp") |
| 11 | + try { |
| 12 | + val originApk = File(inputBean.input) |
| 13 | + if (!originApk.exists()) { |
| 14 | + logCallback.invoke("Apk not Exists") |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + val targetApk = File(inputBean.output) |
| 19 | + if (targetApk.exists()) { |
| 20 | + targetApk.delete() |
| 21 | + } |
| 22 | + |
| 23 | + logCallback.invoke("Start Unzip Apk") |
| 24 | + |
| 25 | + unzip(originApk.absolutePath, workDir.absolutePath) |
| 26 | + |
| 27 | + val dexList = workDir.listFiles { name -> |
| 28 | + name.extension == "dex" |
| 29 | + } |
| 30 | + |
| 31 | + val dexOut = File(workDir,"dexDump") |
| 32 | + dexOut.mkdir() |
| 33 | + |
| 34 | + dexList?.forEach { |
| 35 | + logCallback.invoke("Start Obfuscator ${it.name}") |
| 36 | + val dexInputBean = |
| 37 | + InputBean(it.absolutePath, dexOut.absolutePath+"/" + it.name, inputBean.depth, inputBean.rule) |
| 38 | + DexObf.run(dexInputBean, logCallback) |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + originApk.copyTo(File(inputBean.output)) |
| 43 | + |
| 44 | + logCallback.invoke("Start Package Apk") |
| 45 | + |
| 46 | + zip(dexOut.absolutePath, inputBean.output) |
| 47 | + |
| 48 | + } catch (e: Exception) { |
| 49 | + logCallback.invoke("Error!!!") |
| 50 | + logCallback.invoke(e.message.toString()) |
| 51 | + } finally { |
| 52 | + removeFile(workDir) |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + private fun removeFile(file: File) { |
| 58 | + if (file.isFile) { |
| 59 | + file.delete() |
| 60 | + } else if (file.isDirectory) { |
| 61 | + file.listFiles()?.forEach(::removeFile) |
| 62 | + file.delete() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + //todo |
| 67 | + private fun zip(path: String, apk: String) { |
| 68 | + val dexList = File(path).listFiles { name -> |
| 69 | + name.extension == "dex" |
| 70 | + }?.toList() |
| 71 | + |
| 72 | + ZipFile(apk).use { |
| 73 | + it.addFiles(dexList) |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + //todo |
| 79 | + private fun unzip(apk: String, path: String) { |
| 80 | + ZipFile(apk).use { |
| 81 | + it.extractAll(path) |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +} |
0 commit comments