英文:
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 "
, 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: <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.
# Create a select menu of all the available packages on the emulator and let the user choose one
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("Available packages:")
for i, package in enumerate(packages):
print(f"{i+1}: {package}")
package_index = int(input("Enter the number of the package to extract: ")) - 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(":")[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"Full path of {package_name}: {package_path}")
apk_type = input("Enter 1 to extract a single APK file, or 2 to extract a split APK: ")
if apk_type == "1":
print("Hhhhh")
#subprocess.run(["adb" ,"-s", device_id, "pull", package_path, f"{package_name}.apk"])
else:
print("Working in progress......................................")
答案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(["adb","-s", device_id, "shell", "pm grant", "path", package_name], capture_output=True).stdout.decode("utf-8")
to
new_output = subprocess.run(["adb","-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")
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="emulator-5554"
# Create a select menu of all the available packages on the emulator and let the user choose one
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("Available packages:")
for i, package in enumerate(packages):
print(f"{i+1}: {package}")
package_index = int(input("Enter the number of the package to extract: ")) - 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(":")[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"Full path of {package_name}: {package_path}")
apk_type = input("Enter 1 to extract a single APK file, or 2 to extract a split APK: ")
if apk_type == "1":
print("Hhhhh")
subprocess.run(["adb" ,"-s", device_id, "pull", package_path, f"{package_name}.apk"])
else:
print("Working in progress......................................")
答案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 "cp /data/app/xxx/base.apk /sdcard/"
adb pull /sdcard/base.apk ./package_name.apk
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论