天天看点

subprocess.check_output 报错returned non-zero exit status 1

作者:3tang

今天使用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 .这个错误是由于路径中还有中文引起,把文件名改为英文就没这个错误,可以正常获取到包名。

subprocess.check_output 报错returned non-zero exit status 1
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')也没用,还是存在这个问题。没办法,只能迂回处理,把文件名改为英文