我正在使用这个库来获取root访问权限https://github.com/topjohnwu/libsu
我可以使用root权限运行bash代码;
初始化:
class MainActivity : ComponentActivity() {
companion object {
init {
//here goes static initializer code
Shell.enableVerboseLogging = BuildConfig.DEBUG
Shell.setDefaultBuilder(
Shell.Builder.create()
.setFlags(FLAG_REDIRECT_STDERR)
.setFlags(FLAG_MOUNT_MASTER)
.setTimeout(10)
)
}
}
//onCreate
}
Bash用法:
val srcDir = "data/data"
val result = Shell.cmd(" ls '${srcDir}'").exec() //This will list the root protected directory
但是,我想用root权限运行kotlin代码(而不是bash(。
例如:
val dirChilds = File(srcDir).list() //It seems only that shell has root access not the whole app.
如果您想使用root权限运行Kotlin代码(而不是bash(,您可以使用以下泛型类将root访问封装在代码中:
http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html
我希望这能有所帮助!