Skip to content

Commit

Permalink
Using process builder directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbubble committed Nov 23, 2023
1 parent 853a8aa commit c855ede
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions src/main/kotlin/net/bytedance/security/app/android/AndroidUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c855ede

Please sign in to comment.