Skip to content

Instantly share code, notes, and snippets.

@diewland
Last active December 6, 2023 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diewland/d51a5ac476f3f63b481c4679763e034f to your computer and use it in GitHub Desktop.
Save diewland/d51a5ac476f3f63b481c4679763e034f to your computer and use it in GitHub Desktop.
Execute su commands for android ( root required )
package com.diewland.installmanager.util
import android.util.Log
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
object SuUtil {
private const val TAG = "SU_UTIL"
fun exec (cmd: String): ArrayList<String?> {
return exec(arrayListOf(cmd))
}
// https://stackoverflow.com/a/11311955/466693
fun exec (cmds: List<String>): ArrayList<String?> {
// do su process
val proc = Runtime.getRuntime().exec("su") ?: return arrayListOf(null, null)
val os = DataOutputStream(proc.outputStream)
for (cmd in cmds) {
os.writeBytes(cmd + "\n")
}
os.writeBytes("exit\n")
os.flush()
os.close()
proc.waitFor()
// return resp
return extractOutput(proc)
}
fun exec2 (cmd: String): ArrayList<String?> {
val proc = Runtime.getRuntime().exec(arrayOf("su", "-c", cmd))
proc.waitFor()
return extractOutput(proc)
}
private fun extractOutput (proc: Process): ArrayList<String?> {
// gather resp
val stdInput = BufferedReader(InputStreamReader(proc.inputStream))
val stdError = BufferedReader(InputStreamReader(proc.errorStream))
var s: String? // null included
var o: String? = ""
var e: String? = ""
while (stdInput.readLine().also { s = it } != null) { o += s }
while (stdError.readLine().also { s = it } != null) { e += s }
// if blank, cast to null
if (o.isNullOrBlank()) o = null
if (e.isNullOrBlank()) e = null
// debug
Log.d(TAG, "[success] $o")
Log.d(TAG, "[error ] $e")
// return output, error
return arrayListOf(o, e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment