执行exec命令后,Android应用程序崩溃

时间:2020-11-05 14:55:47

标签: android command exec root

我有以下代码,该代码用于自动更新应用程序,该应用程序已像在根设备上安装系统应用程序一样安装。它从copyToPrivateApps开始,并带有已下载的新apk文件。它从常规作用域附加到IO线程

fun copyToPrivateApps(context: Context, apkPath: String?) {
sudoForCommandAndResult("mount -o rw,remount /system")
sudoForCommandAndResult("mkdir $systemFolder")
sudoForCommandAndResult("cat $apkPath > $systemApkPath")
sudoForCommandAndResult("cp -R ${sudoForResult("find ./data/app -name ${context.packageName}*").drop(1) + "/lib"} $systemFolder")
sudoForCommandAndResult("chmod -R 755 $systemFolder")
sudoForCommandAndResult("chmod 644 $systemFngFolder")
sudoForCommandAndResult("chmod 644 $systemSilFolder")
sudoForCommandAndResult("chmod 644 $systemApkPath")
sudoForCommandAndResult("mount -o ro,remount /system")}

fun sudoForCommandAndResult(string: String): String {
var res = ""
var outputStream: DataOutputStream? = null
var response    : InputStream? = null
var error       : InputStream? = null
try {
    val su = Runtime.getRuntime().exec("su")
    outputStream = DataOutputStream(su.outputStream)
    response = su.inputStream
    error = su.errorStream

    outputStream.writeBytes(string + "\n")
    outputStream.flush()

    outputStream.writeBytes("exit\n")
    outputStream.flush()
    try {
        su.waitFor()
    } catch (e: Throwable) {
        e.printStackTrace()
    }

    val responseString = readFully(response!!).replace("su:main", "").trim()
    val errorString    = readFully(error!!).replace("su:main", "").trim()
    res =  logTimeFormat.format(Date()) + string + if(responseString.isNotBlank()) "\n" + logTimeFormat.format(Date()) + responseString else ""
    res += if(errorString.isNotBlank()) "\n" + logTimeFormat.format(Date()) + errorString else ""
} catch (e: Throwable) {
    res += logTimeFormat.format(Date()) + e.message?.trim()
    e.printStackTrace()
} finally {
    Closer.closeSilently(outputStream, response)
}
return res}

fun sudoForResult(string: String): String {
var res = ""
var outputStream: DataOutputStream? = null
var response    : InputStream? = null
var error       : InputStream? = null
try {
    val su = Runtime.getRuntime().exec("su")
    outputStream = DataOutputStream(su.outputStream)
    response = su.inputStream
    error = su.errorStream

    outputStream.writeBytes(string + "\n")
    outputStream.flush()

    outputStream.writeBytes("exit\n")
    outputStream.flush()
    try {
        su.waitFor()
    } catch (e: Throwable) {
        e.printStackTrace()
    }

    val responseString = readFully(response!!).replace("su:main", "").trim()
    val errorString    = readFully(error!!).replace("su:main", "").trim()
    res =  listOf(responseString, errorString).joinToString(separator = " ")
} catch (e: Throwable) {
    res += e.message?.trim()
    e.printStackTrace()
} finally {
    Closer.closeSilently(outputStream, response)
}
return res.trim()}

当尝试执行“ cp”命令时,它在val su = Runtime.getRuntime()。exec(“ su”)上的sudoForResult()内部失败 有以下错误: A / libc:致命信号4(SIGILL),代码1,tid 6974(DefaultDispatch)中的故障加法器0x7f6190fa24

和一些奇怪的警告

W / art:execv(/ system / bin / dex2oat --runtime-arg -classpath --runtime-arg --instruction-set = arm64 --instruction-set-features = smp,a53 --runtime- arg -Xrelocate --boot-image = / system / framework / boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m -j3 --instruction-set-variant =泛型--instruction-set-features =默认--dex-file = / system / priv-app / appName / appName.apk --oat-file = / data / dalvik-cache / arm64 / system @ priv-app @ appName @ appName.apk @ classes.dex)因为非零退出状态

有时它甚至在“ cat”命令上崩溃时也会出现类似错误

编辑:以前使用较小的更新apk文件工作,现在它几乎快了3倍。

1 个答案:

答案 0 :(得分:0)

尝试一下:

sudoForCommandAndResult("chmod +x $systemFolder")
sudoForCommandAndResult("chmod +x $systemApkPath")
//change cat command with dd if
sudoForCommandAndResult("dd if=$apkPath of=$systemApkPath")