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

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

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.

  1. /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.

  1. # 创建一个包含所有模拟器上可用软件包的选择菜单,并允许用户选择一个
  2. output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "list", "packages", "-3"], capture_output=True).stdout.decode("utf-8")
  3. packages = output.strip().split("\n")
  4. print("可用软件包:")
  5. for i, package in enumerate(packages):
  6. print(f"{i+1}: {package}")
  7. package_index = int(input("输入要提取的软件包的编号:")) - 1
  8. print(package_index)
  9. # 打印所选软件包的路径并将其提取到本地文件系统
  10. package = packages[package_index]
  11. print(package)
  12. ##print(packages)
  13. package_name = package.split(":")[1]
  14. print(package_name)
  15. new_output = subprocess.run(["adb","-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")
  16. package_path = new_output.strip().split(":")[1]
  17. #print(output)
  18. #print(package_path)
  19. #print(f"{package_name}的完整路径:{package_path}")
  20. apk_type = input("输入1以提取单个APK文件,或输入2以提取拆分APK:")
  21. if apk_type == "1":
  22. print("Hhhhh")
  23. #subprocess.run(["adb" ,"-s", device_id, "pull", package_path, f"{package_name}.apk"])
  24. else:
  25. 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.

  1. /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.

  1. # Create a select menu of all the available packages on the emulator and let the user choose one
  2. 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;)
  3. packages = output.strip().split(&quot;\n&quot;)
  4. print(&quot;Available packages:&quot;)
  5. for i, package in enumerate(packages):
  6. print(f&quot;{i+1}: {package}&quot;)
  7. package_index = int(input(&quot;Enter the number of the package to extract: &quot;)) - 1
  8. print(package_index)
  9. # Print the path of the selected package and extract it to the local filesystem
  10. package = packages[package_index]
  11. print(package)
  12. ##print(packages)
  13. package_name = package.split(&quot;:&quot;)[1]
  14. print(package_name)
  15. 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;)
  16. package_path = new_output.strip().split(&quot;:&quot;)[1]
  17. #print(output)
  18. #print(package_path)
  19. #print(f&quot;Full path of {package_name}: {package_path}&quot;)
  20. apk_type = input(&quot;Enter 1 to extract a single APK file, or 2 to extract a split APK: &quot;)
  21. if apk_type == &quot;1&quot;:
  22. print(&quot;Hhhhh&quot;)
  23. #subprocess.run([&quot;adb&quot; ,&quot;-s&quot;, device_id, &quot;pull&quot;, package_path, f&quot;{package_name}.apk&quot;])
  24. else:
  25. print(&quot;Working in progress......................................&quot;)

答案1

得分: 1

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

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

改成

  1. 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上测试):

  1. import subprocess
  2. device_id = "emulator-5554"
  3. # 创建一个选择菜单,列出模拟器上所有可用的包,并让用户选择一个
  4. output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "list", "packages", "-3"], capture_output=True).stdout.decode("utf-8")
  5. packages = output.strip().split("\n")
  6. print("可用的包:")
  7. for i, package in enumerate(packages):
  8. print(f"{i+1}: {package}")
  9. package_index = int(input("输入要提取的包的编号: ")) - 1
  10. print(package_index)
  11. # 打印所选包的路径并将其提取到本地文件系统
  12. package = packages[package_index]
  13. print(package)
  14. package_name = package.split(":")[1]
  15. print(package_name)
  16. new_output = subprocess.run(["adb", "-s", device_id, "shell", "pm", "path", package_name], capture_output=True).stdout.decode("utf-8")
  17. package_path = new_output.strip().split(":")[1]
  18. apk_type = input("输入1以提取单个APK文件,或输入2以提取分割APK: ")
  19. if apk_type == "1":
  20. print("Hhhhh")
  21. subprocess.run(["adb", "-s", device_id, "pull", package_path, f"{package_name}.apk"])
  22. else:
  23. print("工作正在进行中......................................")

希望这对你有帮助。

英文:

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

  1. 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

  1. 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):

  1. import subprocess
  2. device_id=&quot;emulator-5554&quot;
  3. # Create a select menu of all the available packages on the emulator and let the user choose one
  4. 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;)
  5. packages = output.strip().split(&quot;\n&quot;)
  6. print(&quot;Available packages:&quot;)
  7. for i, package in enumerate(packages):
  8. print(f&quot;{i+1}: {package}&quot;)
  9. package_index = int(input(&quot;Enter the number of the package to extract: &quot;)) - 1
  10. print(package_index)
  11. # Print the path of the selected package and extract it to the local filesystem
  12. package = packages[package_index]
  13. print(package)
  14. ##print(packages)
  15. package_name = package.split(&quot;:&quot;)[1]
  16. print(package_name)
  17. 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;)
  18. package_path = new_output.strip().split(&quot;:&quot;)[1]
  19. #print(output)
  20. #print(package_path)
  21. #print(f&quot;Full path of {package_name}: {package_path}&quot;)
  22. apk_type = input(&quot;Enter 1 to extract a single APK file, or 2 to extract a split APK: &quot;)
  23. if apk_type == &quot;1&quot;:
  24. print(&quot;Hhhhh&quot;)
  25. subprocess.run([&quot;adb&quot; ,&quot;-s&quot;, device_id, &quot;pull&quot;, package_path, f&quot;{package_name}.apk&quot;])
  26. else:
  27. print(&quot;Working in progress......................................&quot;)

答案2

得分: 0

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

  1. adb shell "cp /data/app/xxx/base.apk /sdcard/"
  2. 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:

  1. adb shell &quot;cp /data/app/xxx/base.apk /sdcard/&quot;
  2. 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:

确定