Cocos2d-x3.0 iOS 一键编译多个target并打包ipa。
1.编写app打包为ipa的 shell脚本。将以下代码保存为app2ipa.sh。
#!/bin/sh m_appPath="" m_ipaPath="" m_showMessage="NO" make_app_to_ipa() { app_path=$1 ipa_path=$2 if [ "$m_showMessage" == "YES" ] then /usr/bin/xcrun -sdk iphoneos PackageApplication -v "$app_path" -o "$ipa_path" else /usr/bin/xcrun > /dev/null 2>&1 -sdk iphoneos PackageApplication -v "$app_path" -o "$ipa_path" fi echo " >>>> 打包ipa完毕:$ipa_path" } showHelp() { echo "Convert app to ipa" echo "optional arguments:" echo " -h, help show this help message and exit" echo " -a, app app file path " echo " -i, ipa ipa file path " echo " -m,msg display build message, {NO,YES}" exit } #// main-------------------------------- until [ $# -eq 0 ] do case $1 in -a | app) m_appPath=$2 shift ;; -i | ipa) m_ipaPath=$2 shift ;; -m | msg) m_showMessage=$2 shift ;; -h | help) showHelp ;; *) echo "error unknow args : $1" ;; esac shift done #開始构建 echo ">>>>>>>>>> Build Begin " make_app_to_ipa $m_appPath $m_ipaPath echo ">>>>>>>>>> Build Finished . "2.改动cocos2d-x-3.0\tools\cocos2d-console\plugins\project_compile 下的project_compile.py的build_ios方法。build_ios终于代码例如以下。实际上就是遍历targets循环编译。注意事项:iOS的target 中必须包括 iOS keyword。Mac的target必须包括 Mac keyword.
def build_ios(self): if not self._platforms.is_ios_active(): return if not cocos.os_is_mac(): raise cocos.CCPluginError("Please build on MacOSX") self.check_ios_mac_build_depends() project_dir = self._project.get_project_dir() ios_project_dir = self._platforms.project_path() build_mode = self._mode if self._project._is_script_project(): if build_mode == 'debug': output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_SCRIPT_DEBUG, 'ios') else: output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_SCRIPT_RELEASE, 'ios') else: output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_NATIVE, build_mode, 'ios') projectPath = os.path.join(ios_project_dir, self.xcodeproj_name) pbxprojectPath = os.path.join(projectPath, "project.pbxproj") f = file(pbxprojectPath) contents = f.read() section = re.search(r"Begin PBXProject section.*End PBXProject section", contents, re.S) if section is None: message = "Can't find iOS target" raise cocos.CCPluginError(message) targets = re.search(r"targets = (.*);", section.group(), re.S) if targets is None: message = "Can't find iOS target" raise cocos.CCPluginError(message) targetName = None cfg_obj = self._platforms.get_current_config() if cfg_obj.target_name is not None: targetName = cfg_obj.target_name else: names = re.split("\*", targets.group()) for name in names: if "iOS" in name: targetName = str.strip(name) cocos.Logging.info(" >>>>>>>> targetName = %s" % targetName) if targetName is None: message = "Can't find iOS target" raise cocos.CCPluginError(message) if os.path.isdir(output_dir): target_app_dir = os.path.join(output_dir, "%s.app" % targetName[:targetName.find(' ')]) if os.path.isdir(target_app_dir): shutil.rmtree(target_app_dir) cocos.Logging.info("building") command = ' '.join([ "xcodebuild", "-project", "\"%s\"" % projectPath, "-configuration", "%s" % 'Debug' if self._mode is 'debug' else 'Release', "-target", "\"%s\"" % targetName, "-sdk", "iphonesimulator", "-arch i386", "CONFIGURATION_BUILD_DIR=%s" % (output_dir) ]) self._run_cmd(command) # app 转 ipa app_path = os.path.join(output_dir, "%s.app" % targetName[:targetName.find(' ')]) ipa_path = os.path.join(output_dir, "%s.ipa" % targetName[:targetName.find(' ')]) command = ' '.join([ "app2ipa.sh", "-a", "\"%s\"" % app_path, "-i", "\"%s\"" % ipa_path, "-m" ]) cocos.Logging.info(" >>>>> run command %s" % command) self._run_cmd(command) filelist = os.listdir(output_dir) for filename in filelist: name, extention = os.path.splitext(filename) if extention == '.a': filename = os.path.join(output_dir, filename) os.remove(filename) if extention == '.app' and name == targetName: filename = os.path.join(output_dir, filename) newname = os.path.join(output_dir, name[:name.find(' ')]+extention) os.rename(filename, newname) self._iosapp_path = newname if self._no_res: self._remove_res(self._iosapp_path) cocos.Logging.info("build succeeded.")
好了,我们能够使用例如以下命令编译了
python cocos.py compile -s /projects/MyGame/proj.ios_mac -m debug -p ios