今天使用aapt.exe擷取apk包名的時候發現一個Bug,就是參數中含有中文就會爆類似以下錯誤
ziparchive W 06-11 09:15:36 16496 41932] Unable to open 'C:/Users/tim/Desktop/九州仙劍.apk': Illegal byte sequence
zipro W 06-11 09:15:36 16496 41932] Error opening archive C:/Users/tim/Desktop/九州仙劍.apk: I/O error
ERROR: dump failed because no AndroidManifest.xml found
Traceback (most recent call last):
File "D:\pythonLeanHis\king\StangTools\MainTool.py", line 553, in selectFile
packageName = self.getPackageName(fileName)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\pythonLeanHis\king\StangTools\MainTool.py", line 567, in getPackageName
output = subprocess.check_output([aaptPath, "dump", "badging", fileName])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\python\Lib\subprocess.py", line 465, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\python\Lib\subprocess.py", line 569, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['D:\\pythonLeanHis\\king\\StangTools\\base/aapt.exe', 'dump', 'badging', 'C:/Users/tim/Desktop/九州仙劍.apk']' returned non-zero exit status 1.
程序已結束,退出代碼1
returned non-zero exit status .這個錯誤是由于路徑中還有中文引起,把檔案名改為英文就沒這個錯誤,可以正常擷取到包名。
def getPackageName(self,fileName):
sdkPath = os.environ.get("ANDROID_SDK_ROOT")
if not sdkPath:
raise Exception("ANDROID_SDK_ROOT environment variable not set")
else:
aaptPath = os.path.join(sdkPath, "aapt.exe")
output = subprocess.check_output([aaptPath, "dump", "badging", fileName])
for line in output.splitlines():
line = line.decode("utf-8")
if line.startswith("package: name="):
return line.split("=")[1].split(" ")[0].replace("'", "")
raise Exception("Package name not found in APK file")
把fileName.encode('utf-8')也沒用,還是存在這個問題。沒辦法,隻能迂回處理,把檔案名改為英文