def mergeValuesColors(): if (isNeedMergeValues_Colors): print("start to merge values/colors.xml......") else: print("no need to merge values/colors.xml.") return gameColorXMLPath = gameApkPath + "/res/values/colors.xml" tempColorXMLPath = gameApkPath + "/res/values/temp_colors.xml" gameColorXMLBakPath = gameApkPath + "/res/values/colors.xml.bak" gameColorsList = [] gameColorsXML = open(gameColorXMLPath) for line in gameColorsXML: gameColorsList.append(line.replace(" ", "")) gameColorsXML.close() sdkColorList = [] sdkColorsXML = open(sdkApkPath + "/res/values/colors.xml") for line in sdkColorsXML: str = line.replace(" ", "") if (str.__contains__("<colorname=") and not gameColorsList.__contains__(str)): sdkColorList.append(line) sdkColorsXML.close() mergeFile = open(tempColorXMLPath, "w+") gameColorsXML = open(gameColorXMLPath) for line in gameColorsXML: if (line.__contains__("</resources>")): for color in sdkColorList: mergeFile.write(color) mergeFile.write(line) else: mergeFile.write(line) gameColorsXML.close() mergeFile.close() FileUtils.rename(gameColorXMLPath, gameColorXMLBakPath) FileUtils.rename(tempColorXMLPath, gameColorXMLPath) FileUtils.delFile(gameColorXMLBakPath) print("end merge values/colors.xml") return
def mergeValueIds(): global backupPath if (isNeedMergeValues_Ids): print("start to merge values/ids.xml......") else: print("no need to merge values/ids.xml.") return #backing up file ids.xml if (os.path.exists(backupPath + "/ids.xml") == 0): print("ids.xml is not exists.start backup...") FileUtils.doCopy(gameApkPath + "/res/values/ids.xml", backupPath) if (os.path.exists(backupPath + "/ids.xml") == 0): print("[error]backup ids.xml failed.") return #recover file from backup if (os.path.isfile(gameApkPath + "/res/values/ids.xml")): FileUtils.delFile(gameApkPath + "/res/values/ids.xml") FileUtils.doCopy(backupPath + "/ids.xml", gameApkPath + "/res/values/") gameIdsXMLPath = gameApkPath + "/res/values/ids.xml" tempIdsXMLPath = gameApkPath + "/res/values/temp_ids.xml" gameIdsXMLBakPath = gameApkPath + "/res/values/ids.xml.bak" gameIdsList = [] gameIdsXML = open(gameIdsXMLPath) for line in gameIdsXML: gameIdsList.append(line.replace(" ", "")) gameIdsXML.close() sdkIdsList = [] sdkIdsXML = open(sdkApkPath + "/res/values/ids.xml") for line in sdkIdsXML: str = line.replace(" ", "") if (str.__contains__("<itemtype=\"id\"") and not gameIdsList.__contains__(str)): sdkIdsList.append(line) sdkIdsXML.close() mergeFile = open(tempIdsXMLPath, "w+") gameColorsXML = open(gameIdsXMLPath) for line in gameColorsXML: if (line.__contains__("</resources>")): for color in sdkIdsList: mergeFile.write(color) mergeFile.write(line) else: mergeFile.write(line) mergeFile.flush() mergeFile.close() gameColorsXML.close() FileUtils.rename(gameIdsXMLPath, gameIdsXMLBakPath) FileUtils.rename(tempIdsXMLPath, gameIdsXMLPath) FileUtils.delFile(gameIdsXMLBakPath) print("end merge values/ids.xml") return
def mergeValueStyles(): global backupPath if (isNeedMergeValues_Styles): print("start to merge values/styles.xml......") else: print("no need to merge values/styles.xml.") return #backing up file styles.xml if (os.path.exists(backupPath + "/res/values/styles.xml") == 0): print("styles.xml is not exists.start backup...") FileUtils.doCopy(gameApkPath + "/res/values/styles.xml", backupPath) if (os.path.exists(backupPath + "/styles.xml") == 0): print("[error]backup styles.xml failed.") return #recover file from backup if (os.path.isfile(gameApkPath + "/res/values/styles.xml")): FileUtils.delFile(gameApkPath + "/res/values/styles.xml") FileUtils.doCopy(backupPath + "/styles.xml", gameApkPath + "/res/values/") gameStyleXMLPath = gameApkPath + "/res/values/styles.xml" tempStyleXMLPath = gameApkPath + "/res/values/temp_styles.xml" gameStyleXMLBakPath = gameApkPath + "/res/values/styles.xml.bak" sdkStyleXMLPath = sdkApkPath + "/res/values/styles.xml" gameStyleList = [] appendStyleList = [] gameStyleXML = open(gameStyleXMLPath) for line in gameStyleXML: line = line.replace(" ", "") if (line.__contains__("<stylename")): gameStyleList.append(line) gameStyleXML.close() sdkStyleXML = open(sdkStyleXMLPath) appendFlag = False for line in sdkStyleXML: str = line.replace(" ", "") if (str.__contains__("<stylename") and not gameStyleList.__contains__(str)): appendFlag = True appendStyleList.append(line) elif (gameStyleList.__contains__(str)): appendFlag = False elif (str.__contains__("</style>" or str.__contains__("/>"))): if (appendFlag): appendFlag = False appendStyleList.append(line) elif (appendFlag): appendStyleList.append(line) sdkStyleXML.close() mergeFile = open(tempStyleXMLPath, "w+") gameStyleXML = open(gameStyleXMLPath) for line in gameStyleXML: if (line.__contains__("</resources>")): for style in appendStyleList: mergeFile.write(style) mergeFile.write(line) else: mergeFile.write(line) mergeFile.flush() mergeFile.close() gameStyleXML.close() FileUtils.rename(gameStyleXMLPath, gameStyleXMLBakPath) FileUtils.rename(tempStyleXMLPath, gameStyleXMLPath) FileUtils.delFile(gameStyleXMLBakPath) print("end merge values/styles.xml") return
def mergeValuesStrings(): if (isNeedMergeValues_Strings): print("start to merge values/strings.xml......") else: print("no need to merge values/strings.xml.") return gameStringXMLPath = gameApkPath + "/res/values/strings.xml" tempStringXMLPath = gameApkPath + "/res/values/temp_strings.xml" gameStringXMLBakPath = gameApkPath + "/res/values/strings.xml.bak" gameStringsList = [] gameStringsXML = open(gameStringXMLPath) for line in gameStringsXML: distinctStr = line[0:line.find(">")].replace(" ", "") gameStringsList.append(distinctStr) gameStringsXML.close() sdkStringsList = [] sdkStringsXML = open(sdkApkPath + "/res/values/strings.xml") flagAppendStr = False for line in sdkStringsXML: str = line.replace(" ", "") if (flagAppendStr): sdkStringsList.append(line) if (str.__contains__("</string>")): flagAppendStr = False if (str.__contains__("<stringname=") and not gameStringsList.__contains__( str[0:str.find(">")].replace(" ", ""))): sdkStringsList.append(line) if (not str.__contains__("</string>")): flagAppendStr = True sdkStringsXML.close() mergeFile = open(tempStringXMLPath, "w+") gameColorsXML = open(gameStringXMLPath) for line in gameColorsXML: if (line.__contains__("</resources>")): for color in sdkStringsList: mergeFile.write(color) mergeFile.write(line) else: mergeFile.write(line) mergeFile.flush() mergeFile.close() gameColorsXML.close() FileUtils.rename(gameStringXMLPath, gameStringXMLBakPath) FileUtils.rename(tempStringXMLPath, gameStringXMLPath) FileUtils.delFile(gameStringXMLBakPath) print("end merge values/strings.xml") return
def mergeAndroidManifest(): global configMetaList, backupPath, gameApkPath, configSplashList, isNeedSplash, configProviderList, isNeedProvider if (isNeedCopyAndroidManifest): print("start merge androidManifest.xml......") else: print("no need to merge androidManifest.") return #check folder "bak" whether exists if (os.path.exists(backupPath) == 0): os.mkdir(backupPath) #backing up file backAndroidManifest.xml if (os.path.exists(backupPath + "/AndroidManifest.xml") == 0): print("AndroidManifest.xml is not exists.start backup...") FileUtils.doCopy(gameApkPath + "/AndroidManifest.xml", backupPath) if (os.path.exists(backupPath + "/AndroidManifest.xml") == 0): print("[error]backup AndroidManifest.xml failed.") return #recover file from backup if (os.path.isfile(gameApkPath + "/AndroidManifest.xml")): FileUtils.delFile(gameApkPath + "/AndroidManifest.xml") FileUtils.doCopy(backupPath + "/AndroidManifest.xml", gameApkPath) setPackageName() gameXmlList = [] sdkXmlPermissionList = [] sdkXmlContentList = [] gameXML = open(gameApkPath + "/AndroidManifest.xml") for line in gameXML: gameXmlList.append(line.replace(" ", "")) gameXML.close() print("len of gameXmlList is " + str(gameXmlList.__len__())) sdkXML = open(sdkApkPath + "/AndroidManifest.xml") flagStrPermission = False flagStrActivity = False flagStrService = False flagStrProvider = False flagStrReceiver = False for line in sdkXML: if (line.__contains__("android.intent.action.MAIN") and not isNeedSplash): continue if (flagStrPermission): sdkXmlPermissionList.append(line) if (line.__contains__("/>")): flagStrPermission = False if (line.__contains__("<uses-permission")): if (not gameXmlList.__contains__(line.replace(" ", ""))): sdkXmlPermissionList.append(line) if (not line.__contains__("/>")): flagStrPermission = True if (flagStrActivity): sdkXmlContentList.append(line) if (line.__contains__("</activity>")): flagStrActivity = False if (line.__contains__("<activity")): sdkXmlContentList.append(line) if (not line.__contains__("/>")): flagStrActivity = True if (flagStrService): sdkXmlContentList.append(line) if (line.__contains__("</service>")): flagStrService = False if (line.__contains__("<service")): sdkXmlContentList.append(line) if (not line.__contains__("/>")): flagStrService = True if (flagStrProvider): sdkXmlContentList.append(line) if (line.__contains__("</provider>")): flagStrProvider = False if (line.__contains__("<provider")): sdkXmlContentList.append(line) if (not line.__contains__("/>")): flagStrProvider = True if (flagStrReceiver): sdkXmlContentList.append(line) if (line.__contains__("</receiver>")): flagStrReceiver = False if (line.__contains__("<receiver")): sdkXmlContentList.append(line) if (not line.__contains__("/>")): flagStrReceiver = True elif (line.__contains__("<meta-data")): sdkXmlContentList.append(line) sdkXML.close() for m in configMetaList: sdkXmlContentList.append(m) # start to read gameXML = open(gameApkPath + "/AndroidManifest.xml") mergeFile = open(gameApkPath + "/temp_AndroidManifest.xml", "w+") tagProvider = False for line in gameXML: if (line.__contains__("<application")): for p in sdkXmlPermissionList: mergeFile.write(p) mergeFile.write(line) elif (line.__contains__("</application>")): for c in sdkXmlContentList: if (isNeedProvider): t = str(configProviderList[0]).replace("\n", "") if (c.__contains__("<provider") and c.__contains__(t)): c = c.replace(t, packageName) elif (c.__contains__("<provider")): tagProvider = True elif (tagProvider and c.__contains__(configProviderList[0])): c = c.replace(configProviderList[0], packageName) elif (c.__contains__("/>")): tagProvider = False mergeFile.write(c) mergeFile.write(line) else: if (line.__contains__("android.intent.action.MAIN")): if (isNeedSplash): line = line.replace("android.intent.action.MAIN", configSplashList[0]) if (line.__contains__("android.intent.category.LAUNCHER")): if (isNeedSplash): line = "<category android:name=\"android.intent.category.DEFAULT\" />" mergeFile.write(line) mergeFile.close() gameXML.close() # rename temp_AndroidManifest.xml FileUtils.delFile(gameApkPath + "/AndroidManifest.xml") FileUtils.rename(gameApkPath + "/temp_AndroidManifest.xml", gameApkPath + "/AndroidManifest.xml") print("end merge androidManifest.xml.") return
def mergeSdkPublic(self): print("start prcess mergeSdkPublic...") tempAttrValue = self.attrValue tempDrawableValue = self.drawableValue tempMipmapValue = self.mipmapValue tempLayoutValue = self.layoutValue tempStringValue = self.stringValue tempDimenValue = self.dimenValue tempStyleValue = self.styleValue tempBoolValue = self.boolValue tempColorValue = self.colorValue tempIdValue = self.idValue tempIntegerValue = self.integerValue tempAnimValue = self.animValue try: sdkPublicFile = open(self.sdkPath, "r") #读取各类型到各自的集合中 for str in sdkPublicFile: ostr = str str = str.replace(" ", "") distinctStr = str[0:str.find("id=\"")].replace(" ", "") if str.find("id=\"") and self.tempFinalList.__contains__( distinctStr): # print("===="+str[0:str.find("id=\"")].replace(" ","")) continue else: if (str.__contains__("type=\"attr\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempAttrValue = hex(long(eval(tempAttrValue) + 1)).replace("L", "") newStr = ostr.replace(originalValue, tempAttrValue) self.attrList.append(newStr) elif (str.__contains__("type=\"drawable\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempDrawableValue = hex( long(eval(tempDrawableValue)) + 1).replace( "L", "") newStr = ostr.replace(originalValue, tempDrawableValue) self.drawableList.append(newStr) elif (str.__contains__("type=\"mipmap\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempMipmapValue = hex(long(eval(tempMipmapValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempMipmapValue) self.mipmapList.append(newStr) elif (str.__contains__("type=\"layout\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempLayoutValue = hex(long(eval(tempLayoutValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempLayoutValue) self.layoutList.append(newStr) elif (str.__contains__("type=\"anim\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempAnimValue = hex(long(eval(tempAnimValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempAnimValue) self.animList.append(newStr) elif (str.__contains__("type=\"string\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempStringValue = hex(long(eval(tempStringValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempStringValue) self.stringList.append(newStr) elif (str.__contains__("type=\"dimen\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempDimenValue = hex(long(eval(tempDimenValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempDimenValue) self.dimenList.append(newStr) elif (str.__contains__("type=\"style\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempStyleValue = hex(long(eval(tempStyleValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempStyleValue) self.styleList.append(newStr) elif (str.__contains__("type=\"bool\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempBoolValue = hex(long(eval(tempBoolValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempBoolValue) self.boolList.append(newStr) elif (str.__contains__("type=\"color\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempColorValue = hex(long(eval(tempColorValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempColorValue) self.colorList.append(newStr) elif (str.__contains__("type=\"id\"")): originalValue = str[str.find("id=\"") + 4:str.find("\"/>")] tempIdValue = hex(long(eval(tempIdValue)) + 1).replace( "L", "") newStr = ostr.replace(originalValue, tempIdValue) self.idList.append(newStr) elif (str.__contains__("type=\"integer\"")): originalValue = str[str.find("integer=\"") + 4:str.find("\"/>")] tempIntegerValue = hex( long(eval(tempIntegerValue)) + 1).replace("L", "") newStr = ostr.replace(originalValue, tempIntegerValue) self.integerList.append(newStr) #将各数组内容追加到public.xml中去 if (os.path.exists("out") == 0): os.mkdir("out") mergeFile = open( self.gamePath.replace("public.xml", "temp_public.xml"), "w+") cpPublicFile = open(self.gamePath, "r") self.tempFinalList = [] for line in cpPublicFile: distinctStr = line[0:line.find("id=\"")].replace(" ", "") if line.find("id=\"") and self.tempFinalList.__contains__( distinctStr): print("exists distinctStr=" + distinctStr) continue else: mergeFile.write(line) self.tempFinalList.append(distinctStr) if (line.__contains__("type=\"attr\"") and line.__contains__(self.attrValue)): for s in self.attrList: mergeFile.write(s) elif (line.__contains__("type=\"drawable\"") and line.__contains__(self.drawableValue)): for s in self.drawableList: mergeFile.write(s) elif (line.__contains__("type=\"mipmap\"") and line.__contains__(self.mipmapValue)): for s in self.mipmapList: mergeFile.write(s) elif (line.__contains__("type=\"layout\"") and line.__contains__(self.layoutValue)): for s in self.layoutList: mergeFile.write(s) elif (line.__contains__("type=\"anim\"") and line.__contains__(self.animValue)): for s in self.animList: mergeFile.write(s) elif (line.__contains__("type=\"string\"") and line.__contains__(self.stringValue)): for s in self.stringList: mergeFile.write(s) elif (line.__contains__("type=\"dimen\"") and line.__contains__(self.dimenValue)): for s in self.dimenList: mergeFile.write(s) elif (line.__contains__("type=\"style\"") and line.__contains__(self.styleValue)): for s in self.styleList: mergeFile.write(s) elif (line.__contains__("type=\"bool\"") and line.__contains__(self.boolValue)): for s in self.boolList: mergeFile.write(s) elif (line.__contains__("type=\"color\"") and line.__contains__(self.colorValue)): for s in self.colorList: mergeFile.write(s) elif (line.__contains__("type=\"id\"") and line.__contains__(self.idValue)): for s in self.idList: mergeFile.write(s) elif (line.__contains__("type=\"integer\"") and line.__contains__(self.integerValue)): for s in self.integerList: mergeFile.write(s) mergeFile.flush() mergeFile.close() FileUtils.delFile(self.gamePath) FileUtils.rename( self.gamePath.replace("public.xml", "temp_public.xml"), self.gamePath) except: traceback.print_exc() self.log() return