From c855edefb7bfbfebbf824e3aa85fa4f89ec8dc32 Mon Sep 17 00:00:00 2001 From: "zhangbo.xx" Date: Thu, 23 Nov 2023 19:13:11 +0800 Subject: [PATCH] Using process builder directly --- .../security/app/android/AndroidUtils.kt | 68 ++++++++++++++----- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/net/bytedance/security/app/android/AndroidUtils.kt b/src/main/kotlin/net/bytedance/security/app/android/AndroidUtils.kt index 761bdd9..e320013 100644 --- a/src/main/kotlin/net/bytedance/security/app/android/AndroidUtils.kt +++ b/src/main/kotlin/net/bytedance/security/app/android/AndroidUtils.kt @@ -51,6 +51,7 @@ import java.io.File import java.io.IOException import java.nio.charset.StandardCharsets import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException import java.util.zip.ZipEntry import java.util.zip.ZipFile import kotlin.system.exitProcess @@ -187,26 +188,59 @@ object AndroidUtils { try { val start = System.currentTimeMillis() Log.logInfo("==========>Start dex to Java") - val wapperFile: String - val jadxFile: String - if (isWindows()) { - wapperFile = File(jadxPath, "wrapper.bat").path - jadxFile = File(jadxPath, "jadx.bat").path + + val doneFile = File(JavaSourceDir,".done") + + if (doneFile.exists()) { + Log.logInfo("Using jadx cache") + return + } + JavaSourceDir?.let { File(it).deleteRecursively() } + val jadx = if (isWindows()) { + File(jadxPath, "jadx.bat").path } else { - wapperFile = File(jadxPath, "wrapper.sh").path - jadxFile = File(jadxPath, "jadx").path + File(jadxPath, "jadx").path } - val processBuilder = ProcessBuilder( - wapperFile, - jadxFile, - apkPath, - JavaSourceDir, thread.toString() + + val command = listOf( + jadx, + "--quiet", + "--no-imports", + "--show-bad-code", + "--no-debug-info", + "--output-dir", JavaSourceDir, + "--threads-count", thread.toString(), + "--export-gradle", + apkPath ) - Log.logInfo(processBuilder.command().toString()) - dexToJavaProcess = processBuilder.start() - dexToJavaProcess?.waitFor(1800, TimeUnit.SECONDS) - dexToJavaProcess?.destroy() - dexToJavaProcess?.waitFor() + Log.logInfo("Executing command: ${command.joinToString(" ")}") + val processBuilder: Process = ProcessBuilder(command) + .redirectOutput(ProcessBuilder.Redirect.INHERIT) + .redirectError(ProcessBuilder.Redirect.INHERIT) + .start() + + val timeoutMillis = 1800000L // 1800 seconds + if (!processBuilder.waitFor(timeoutMillis, TimeUnit.MILLISECONDS)) { + processBuilder.destroyForcibly() + processBuilder.waitFor() + } + + val exitCode = try { + processBuilder.exitValue() + } catch (e: IllegalThreadStateException) { + // Process is still running + processBuilder.destroyForcibly() + processBuilder.waitFor() + -1 // Use a special code to indicate failure + } + + if (exitCode == 0) { + Log.logInfo("Command executed successfully.") + doneFile.createNewFile() + } else { + Log.logInfo("Command execution failed with exit code: $exitCode") + } + Log.logInfo("Dex to Java Done " + (System.currentTimeMillis() - start) + "ms<==========") } catch (e: Exception) { e.printStackTrace()