2012-10-11 110 views
1

我試圖自動更改我的VersionCode以匹配SVN的內部版本號。我正在使用python來解析Android Manifest並嘗試保存文件。在python中更改android清單中的versionCode並保存文件

我使用的元素樹查找特定節點這樣

def setVersionCode(fileName,versionCode): 
    ET.register_namespace("android", "http://schemas.android.com/apk/res/android") 
    tree = ET.ElementTree() 
    tree.parse(fileName) 
    root = tree.getroot() #This returns the root node 
    root.attrib["{http://schemas.android.com/apk/res/android}versionCode] " #This gives me the current Version code 

,然後調用它像這樣

setVersionCode(pathToTheManifest,"500") 

我想給的versionCode設置爲500(例如)並保存將文件恢復到相同的fileName路徑而不會丟失任何清單格式或XML數據。 我該怎麼做?

我知道這工作

root.attrib["{http://schemas.android.com/apk/res/android}versionCode] " = versionCode 

,因爲當我做了ET.dump它爲我改變的文件。但是,如何在不丟失數據或格式的情況下將其從相同的位置保存回來?

回答

0

嘗試使用write方法

def setVersionCode(fileName,versionCode): 
    ET.register_namespace('android', 'http://schemas.android.com/apk/res/android') 
    tree = ET.parse(fileName) 
    root = tree.getroot(); 
    root.attrib["{http://schemas.android.com/apk/res/android}versionCode"] = versionCode 
    tree.write(fileName) 
+0

謝謝,這是現在一個很好的3歲,我不工作,我有這個問題:)但也許是一個工作的答案,因此不適剛剛離開這裏 – Slartibartfast

相關問題