Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python更改AndroidManifest中的versionCode和versionName?_Android_Python_Python 2.7_Character Encoding_Android Manifest - Fatal编程技术网

如何使用python更改AndroidManifest中的versionCode和versionName?

如何使用python更改AndroidManifest中的versionCode和versionName?,android,python,python-2.7,character-encoding,android-manifest,Android,Python,Python 2.7,Character Encoding,Android Manifest,我试图在构建/签署apk之前设置versionCode和versionName。生成和签名操作将正常运行 问题是,当我尝试启动应用程序时,它崩溃了,我发现并分配了有关未知权限的警告 如果我在eclipse中打开项目而不更改AndroidManifest.xml中的任何内容,并运行“导出签名应用程序包”。我也犯了同样的错误 如果我添加一个空间,删除它,并保存AndroidManifest.xml,那么应用程序运行时不会出现问题 这至少会让我意识到这是一个编码问题。下面是我用来更改版本的代码 fh,

我试图在构建/签署apk之前设置versionCode和versionName。生成和签名操作将正常运行

问题是,当我尝试启动应用程序时,它崩溃了,我发现并分配了有关未知权限的警告

如果我在eclipse中打开项目而不更改AndroidManifest.xml中的任何内容,并运行“导出签名应用程序包”。我也犯了同样的错误

如果我添加一个空间,删除它,并保存AndroidManifest.xml,那么应用程序运行时不会出现问题

这至少会让我意识到这是一个编码问题。下面是我用来更改版本的代码

fh,abs_path = mkstemp()
file_path = 'AndroidManifest.xml'
old_file = open(file_path)
new_file = open(abs_path,'w')
for line in old_file:
    line = re.sub(r'android:versionCode=".*?"','android:versionCode="%s"' %    version_code,line)
    line = re.sub(r'android:versionName=".*?"','android:versionName="%s"' % version_name,line)
    new_file.write(line)
new_file.close()
close(fh)
old_file.close()
remove(file_path)
move(abs_path, file_path)
我也尝试过这样做来强制执行utf-8。我不确定清单应该有什么编码

line = re.sub(r'android:versionCode=".*?"',u'android:versionCode="%s"' %    version_code,line)
line = re.sub(r'android:versionName=".*?"',u'android:versionName="%s"' % version_name,line)
new_file.write(line.encode('utf-8'))
我试着像这样检查编码,但它得到了同样的错误

file -bi AndroidManifest.xml 
application/xml; charset=us-ascii

有人知道如何解决这个问题吗?

问题是Android manifest希望LF成为新的产品线,正如Michael Butscher所说的那样。为了实现这一点,我必须使用
io.open
并将新行设置为
'\n'
。我认为这是Android中的一个bug,Google应该修复它,以便AndroidManifest.xml支持所有常见类型的换行符

fh,abs_path = mkstemp()
file_path = 'AndroidManifest.xml'

old_file = open(file_path, 'r')
new_file = io.open(abs_path,mode='w', newline='\n')

for line in old_file:
        line = re.sub(r'android:versionCode=".*?"','android:versionCode="%s"' % version_code,line)
        line = re.sub(r'android:versionName=".*?"','android:versionName="%s"' % version_name,line)
        new_file.write(line.decode('utf-8'))

new_file.close()
close(fh)
old_file.close()
remove(file_path)
move(abs_path, file_path)

问题可能在于行尾,请尝试以“rb”模式读取文件并写入
new\u file=open(abs\u path,'wb')
It seams orignal是LF,我的python脚本写入CRLF。我将测试您的解决方案。顺便说一句:因为属性在文件中只出现一次,所以您也可以将文件作为一个整体加载到内存中,进行替换并立即写出