如何在Python中正确使用adb和子进程提取apk文件。

huangapple go评论92阅读模式
英文:

How to properly pull an apk using adb and subprocess in python

问题

I'm trying to get apks based on the number it falls on on the select menu but I can't seem to get apk path when I enter it's number on the selection menu using adb and subprocess I aim to use apktool to decompile it when it's done but this is what I get when I enter the number on the select menu.

/system/bin/sh: <stdin>[1]: 15: inaccessible or not found

I read the docs and followed it closely below here is my code what am I doing wrong?
I would love to know.

# 创建一个包含所有模拟器上可用软件包的选择菜单,并允许用户选择一个
output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "list", "packages", "-3"], capture_output=True).stdout.decode("utf-8")
packages = output.strip().split("\n")
print("可用软件包:")
for i, package in enumerate(packages):
    print(f"{i+1}: {package}")
package_index = int(input("输入要提取的软件包的编号:")) - 1
print(package_index)

# 打印所选软件包的路径并将其提取到本地文件系统
package = packages[package_index]
print(package)
##print(packages)
package_name = package.split(":")[1]
print(package_name)
new_output = subprocess.run(["adb","-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")
package_path = new_output.strip().split(":")[1]
#print(output)
#print(package_path)

#print(f"{package_name}的完整路径:{package_path}")
apk_type = input("输入1以提取单个APK文件,或输入2以提取拆分APK:")
if apk_type == "1":
    print("Hhhhh")
    #subprocess.run(["adb" ,"-s", device_id, "pull", package_path, f"{package_name}.apk"])
else:
    print("工作正在进行中......................................")

(Note: The code you provided contains some HTML-encoded characters such as &quot;, which I've replaced with their corresponding characters in the translation.)

英文:

I'm trying to get apks based on the number it falls on on the select menu but I can't seem to get apk path when I enter it's number on the selection menu using adb and subprocess I aim to use apktool to decompile it when it's done but this is what I get when I enter the number on the select menu.

/system/bin/sh: &lt;stdin&gt;[1]: 15: inaccessible or not found

I read the docs and followed it closely below here is my code what am I doing wrong?
I would love to know.

#  Create a select menu of all the available packages on the emulator and let the user choose one
output = subprocess.run([&quot;adb&quot;, &quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm&quot;, &quot;list&quot;, &quot;packages&quot;, &quot;-3&quot;], capture_output=True).stdout.decode(&quot;utf-8&quot;)
packages = output.strip().split(&quot;\n&quot;)
print(&quot;Available packages:&quot;)
for i, package in enumerate(packages):
    print(f&quot;{i+1}: {package}&quot;)
package_index = int(input(&quot;Enter the number of the package to extract: &quot;)) - 1
print(package_index)

# Print the path of the selected package and extract it to the local filesystem
package = packages[package_index]
print(package)
##print(packages)
package_name = package.split(&quot;:&quot;)[1]
print(package_name)
new_output = subprocess.run([&quot;adb&quot;,&quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm&quot;, &quot;path&quot;, package_name], capture_output=True).stdout.decode(&quot;utf-8&quot;)
package_path = new_output.strip().split(&quot;:&quot;)[1]
#print(output)
#print(package_path)

#print(f&quot;Full path of {package_name}: {package_path}&quot;)
apk_type = input(&quot;Enter 1 to extract a single APK file, or 2 to extract a split APK: &quot;)
if apk_type == &quot;1&quot;:
    print(&quot;Hhhhh&quot;)
    #subprocess.run([&quot;adb&quot; ,&quot;-s&quot;, device_id, &quot;pull&quot;, package_path, f&quot;{package_name}.apk&quot;])
else:
    print(&quot;Working in progress......................................&quot;)

答案1

得分: 1

你应该使用 adb shell pm path packageId 代替。所以从

new_output = subprocess.run(["adb","-s", device_id, "shell", "pm grant", "path", package_name], capture_output=True).stdout.decode("utf-8")

改成

new_output = subprocess.run(["adb","-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")

在进行这个更改后,你的Python脚本将正常工作。

以下是更新后的脚本,运行在模拟器上(已在Python3.8.12、MacOS上测试):

import subprocess
device_id = "emulator-5554"

# 创建一个选择菜单,列出模拟器上所有可用的包,并让用户选择一个
output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "list", "packages", "-3"], capture_output=True).stdout.decode("utf-8")
packages = output.strip().split("\n")
print("可用的包:")
for i, package in enumerate(packages):
    print(f"{i+1}: {package}")
package_index = int(input("输入要提取的包的编号: ")) - 1
print(package_index)

# 打印所选包的路径并将其提取到本地文件系统
package = packages[package_index]
print(package)
package_name = package.split(":")[1]
print(package_name)
new_output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")
package_path = new_output.strip().split(":")[1]

apk_type = input("输入1以提取单个APK文件,或输入2以提取分割APK: ")
if apk_type == "1":
    print("Hhhhh")
    subprocess.run(["adb", "-s", device_id, "pull", package_path, f"{package_name}.apk"])
else:
    print("工作正在进行中......................................")

希望这对你有帮助。

英文:

You should use adb shell pm path packageId instead. So from

new_output = subprocess.run([&quot;adb&quot;,&quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm grant&quot;, &quot;path&quot;, package_name], capture_output=True).stdout.decode(&quot;utf-8&quot;)

to

new_output = subprocess.run([&quot;adb&quot;,&quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm&quot;, &quot;path&quot;, package_name], capture_output=True).stdout.decode(&quot;utf-8&quot;)

after this change your python script will work.

Below the updated script, running against an emulator (tested with Python3.8.12, MacOS):

import subprocess
device_id=&quot;emulator-5554&quot;

#  Create a select menu of all the available packages on the emulator and let the user choose one
output = subprocess.run([&quot;adb&quot;, &quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm&quot;, &quot;list&quot;, &quot;packages&quot;, &quot;-3&quot;], capture_output=True).stdout.decode(&quot;utf-8&quot;)
packages = output.strip().split(&quot;\n&quot;)
print(&quot;Available packages:&quot;)
for i, package in enumerate(packages):
    print(f&quot;{i+1}: {package}&quot;)
package_index = int(input(&quot;Enter the number of the package to extract: &quot;)) - 1
print(package_index)

# Print the path of the selected package and extract it to the local filesystem
package = packages[package_index]
print(package)
##print(packages)
package_name = package.split(&quot;:&quot;)[1]
print(package_name)
new_output = subprocess.run([&quot;adb&quot;,&quot;-s&quot;, device_id, &quot;shell&quot;, &quot;pm&quot;, &quot;path&quot;, package_name], capture_output=True).stdout.decode(&quot;utf-8&quot;)
package_path = new_output.strip().split(&quot;:&quot;)[1]
#print(output)
#print(package_path)

#print(f&quot;Full path of {package_name}: {package_path}&quot;)
apk_type = input(&quot;Enter 1 to extract a single APK file, or 2 to extract a split APK: &quot;)
if apk_type == &quot;1&quot;:
    print(&quot;Hhhhh&quot;)
    subprocess.run([&quot;adb&quot; ,&quot;-s&quot;, device_id, &quot;pull&quot;, package_path, f&quot;{package_name}.apk&quot;])
else:
    print(&quot;Working in progress......................................&quot;)

答案2

得分: 0

你无法直接提取APK文件 /data/app/xxx/xx.apk,尝试将其复制到某个 '正常' 路径:

adb shell "cp /data/app/xxx/base.apk /sdcard/"
adb pull /sdcard/base.apk ./package_name.apk
英文:

You can not pull apk file /data/app/xxx/xx.apk directly, try copy it to some 'normal' path:

adb shell &quot;cp /data/app/xxx/base.apk /sdcard/&quot;
adb pull /sdcard/base.apk ./package_name.apk

huangapple
  • 本文由 发表于 2023年2月19日 10:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定