gpt4 book ai didi

android - 如何使用 "libsu"库(或 adb)在 Android Q 上安装拆分 APK 文件?

转载 作者:行者123 更新时间:2023-12-02 11:00:42 27 4
gpt4 key购买 nike

背景

使用 root,我知道对于单个 APK 文件,我们可以使用“libsu”库( here )进行安装:

val installResult = Shell.su("pm install -t \"$filePath\"").exec()

如果失败(在新的 Android 版本上失败,不确定是哪个版本),如下(关于此 here ):

val installResult = Shell.su("cat \"$filePath\" | pm install -t -S ${apkSource.fileSize}").exec()

我还知道,在安装拆分 APK 文件时,事情会变得非常困惑(如 here 所示)。首先,您需要使用“pm install-create”命令创建一个 session :

var sessionId: Int? = null
run {
val sessionIdResult =
Shell.su("pm install-create -r -t").exec().out
val sessionIdPattern = Pattern.compile("(\\d+)")
val sessionIdMatcher = sessionIdPattern.matcher(sessionIdResult[0])
sessionIdMatcher.find()
sessionId = Integer.parseInt(sessionIdMatcher.group(1)!!)
Log.d("AppLog", "sessionId:$sessionId")
}

然后您必须“推送”每个 APK 文件,如下所示:

for (apkSource in fileInfoList) {
val filePath = File(apkSource.parentFilePath, apkSource.fileName).absolutePath
Log.d("AppLog", "installing APK : $filePath ${apkSource.fileSize} ")
val result = Shell.su("pm install-write -S ${apkSource.fileSize} $sessionId \"${apkSource.fileName}\" \"$filePath\"").exec()
Log.d("AppLog", "success pushing apk:${apkSource.fileName} ? ${result.isSuccess}")
}

然后使用 pm install-commit 提交更改:

val installResult = Shell.su("pm install-commit $sessionId").exec()

有关这一切的文档:

  install-create [-lrtsfdg] [-i PACKAGE] [--user USER_ID|all|current]
[-p INHERIT_PACKAGE] [--install-location 0/1/2]
[--install-reason 0/1/2/3/4] [--originating-uri URI]
[--referrer URI] [--abi ABI_NAME] [--force-sdk]
[--preload] [--instantapp] [--full] [--dont-kill]
[--force-uuid internal|UUID] [--pkg PACKAGE] [--apex] [-S BYTES]
[--multi-package] [--staged]
Like "install", but starts an install session. Use "install-write"
to push data into the session, and "install-commit" to finish.

install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH|-]
Write an apk into the given install session. If the path is '-', data
will be read from stdin. Options are:
-S: size in bytes of package, required for stdin

install-commit SESSION_ID
Commit the given active install session, installing the app.

问题

这一切在 Android P 之前都工作得很好,但由于某种原因,它在 Q beta 6 上失败了,向我显示了这个错误:

avc:  denied  { read } for  scontext=u:r:system_server:s0 tcontext=u:object_r:sdcardfs:s0 tclass=file permissive=0
System server has no access to read file context u:object_r:sdcardfs:s0 (from path /storage/emulated/0/Download/split/base.apk, context u:r:system_server:s0)
Error: Unable to open file: /storage/emulated/0/Download/split/base.apk
Consider using a file under /data/local/tmp/

我尝试过的

这与我发现的单个 APK here 的情况类似。 ,所以我想也许类似的解决方案也可以应用在这里:

val result = Shell.su("cat $filePath | pm install-write -S ${apkSource.fileSize} $sessionId \"${apkSource.fileName}\" \"$filePath\"").exec()

这仍然仅适用于 Android P 及更低版本。

因此,看到我查看的原始代码有效,它使用了 InputStream,正如文档所暗示的那样,这是可能的。这是他们拥有的:

while (apkSource.nextApk())
ensureCommandSucceeded(Root.exec(String.format("pm install-write -S %d %d \"%s\"", apkSource.getApkLength(), sessionId, apkSource.getApkName()), apkSource.openApkInputStream()));

所以我尝试的是这样的:

val result = Shell.su("pm install-write -S ${apkSource.fileSize} $sessionId \"${apkSource.fileName}\" -")
.add(SuFileInputStream(filePath)).exec()

遗憾的是这也不起作用。

问题

我知道我可以复制相同的代码,但是仍然有办法使用该库(因为它会更短、更优雅)?如果是这样,我该怎么做?

最佳答案

这很困惑,但是试试这个代码。它使用 SuFileInputStream 读取 apk 文件内容,然后将其传输到 install-write 命令中。理论上这应该可以解决问题。

                // getting session id
val createSessionResult = Shell.su("pm install-create -S $size").
val sessionIdRegex = "\\[([0-9]+)]".toRegex()
var sessionId: Int? = null
for (line in createSessionResult.out) {
val result = sessionIdRegex.find(line)?.groupValues?.get(1)?.toInt()
if (result != null) {
sessionId = result
break
}
}

// writing apks, you might want to extract this to another function
val writeShellInStream = PipedInputStream()
PipedOutputStream(writeShellInStream).use { writeShellInOutputStream ->
PrintWriter(writeShellInOutputStream).use { writeShellInWriter ->
writeShellInWriter.println("pm install-write -S $size $sessionId base") // eventually replace base with split apk name
writeShellInWriter.flush()

Shell.su(writeShellInStream).submit { writeResult ->
if (writeResult.isSuccess) {
Shell.su("pm install-commit $sessionId").submit { commitResult ->
// commitResult.isSuccess to check if worked
}
}
}
apkInputStream.copyTo(writeShellInOutputStream)
writeShellInWriter.println()
}
}

编辑:如果您不需要安装,您可能需要先尝试命令“cat [您的 apk 文件] | pm install-write -S [大小] [sessionId] [基本/拆分 apk 名称]”溪流。如果 cat 不起作用,请尝试“dd if=[apk file]”。

关于android - 如何使用 "libsu"库(或 adb)在 Android Q 上安装拆分 APK 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57468292/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com