一、通过adb发送指令,收集设备日志并保存
二、UI
三、代码
/**
* 获取设备列表
*/
fun getDevices(): List<String> {
val process = ProcessBuilder("adb", "devices")
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
process.waitFor()
return output.lines()
.drop(1)
.filter { it.isNotBlank() && it.contains("device") }
.map { it.split("\t").first() }
}
//抓取日志
fun startLogcat(device: String) {
//清除历史
ProcessBuilder("adb", "-s", device, "logcat", "-c")
.redirectErrorStream(true)
.start()
.waitFor()
val process = ProcessBuilder("adb", "-s", device, "logcat")
.redirectErrorStream(true)
.start()
logcatProcess = process
logs.clear()
scope.launch(Dispatchers.Default) {
BufferedReader(InputStreamReader(process.inputStream, Charsets.UTF_8)).useLines { lines ->
lines.forEach { line ->
if (filterKeyword.isBlank() || line.contains(filterKeyword, ignoreCase = true)) {
logs.add(line)
if (logs.size > MAX_LINES) logs.removeFirst()
}
}
}
}
}
四、下载
链接: https://pan.baidu.com/s/1DP7AmIUQgwJq54-0Ebblzg?pwd=4pvf