前言
Android 开发常用的ADB命令,以及相关常见问题解决方案,本文会不断维护;
Android 检查权限
private fun checkRoot(): Boolean {
val binPath = "/system/bin/su"
val xBinPath = "/system/xbin/su"
if (File(binPath).exists() && isCanExecute(binPath)) {
return true
}
if (File(xBinPath).exists() && isCanExecute(xBinPath)) {
return true
}
return false
}
private fun isCanExecute(filePath: String): Boolean {
var process: Process? = null
try {
process = Runtime.getRuntime().exec("ls -l $filePath")
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream))
val readLine: String = bufferedReader.readLine()
if (readLine.length >= 4) {
val flag = readLine[3]
if (flag == 's' || flag == 'x')
return true
}
} catch (e: IOException) {
e.printStackTrace()
} finally {
process?.destroy()
}
return false
}
Android 代码执行ADB命令
private fun execute(command: String) {
var process: Process? = null
lateinit var bufferedReader: BufferedReader
lateinit var errorBufferedReader: BufferedReader
try {
process = Runtime.getRuntime().exec(command)
bufferedReader = BufferedReader(InputStreamReader(process.inputStream))
errorBufferedReader = BufferedReader(InputStreamReader(process.errorStream))
val result = StringBuffer()
val error = StringBuffer()
while (true) {
val readLine = bufferedReader.readLine()
if (readLine != null) {
result.append(readLine)
} else {
break
}
}
while (true) {
val readLine = errorBufferedReader.readLine()
if (readLine != null) {
error.append(readLine)
} else {
break
}
}
println("shuxin.wei exec: $command")
println("shuxin.wei result:${process.exitValue()} $result")
println("shuxin.wei error: $error")
process.waitFor()
} catch (e: IOException) {
e.printStackTrace()
} finally {
bufferedReader.close()
errorBufferedReader.close()
process?.destroy()
}
}
手机GPU过度绘制调试
开启:adb shell setprop debug.hwui.overdraw show
关闭:adb shell setprop debug.hwui.overdraw false
布局边界
开启:adb shell setprop debug.layout true
关闭:adb shell setprop debug.layout false