Cocos2d-x3.0 iOS 一键编译多个target并打包ipa

1.编写app打包为ipa的 shell脚本,将下面代码保存为app2ipa.sh。

[plain] view plain copy
  1. #!/bin/sh     
  2.   
  3. m_appPath=""  
  4. m_ipaPath=""  
  5. m_showMessage="NO"  
  6.   
  7. make_app_to_ipa()  
  8. {  
  9.     app_path=$1  
  10.     ipa_path=$2  
  11.     if [ "$m_showMessage" == "YES" ]  
  12.     then  
  13.         /usr/bin/xcrun -sdk iphoneos PackageApplication -v "$app_path" -o "$ipa_path"  
  14.     else  
  15.         /usr/bin/xcrun  > /dev/null 2>&1 -sdk iphoneos PackageApplication -v "$app_path" -o "$ipa_path"  
  16.     fi  
  17.     echo "  >>>> 打包ipa完成:$ipa_path"  
  18. }  
  19.   
  20. showHelp()  
  21. {  
  22. echo "Convert app to ipa"  
  23. echo "optional arguments:"  
  24. echo "  -h, help            show this help message and exit"  
  25. echo "  -a, app             app file path "  
  26. echo "  -i, ipa             ipa file path "  
  27. echo "  -m,msg              display build message, {NO,YES}"  
  28. exit  
  29. }  
  30.   
  31.   
  32. #// main--------------------------------  
  33. until [ $# -eq 0 ]  
  34. do  
  35.     case $1 in  
  36.     -a | app)  
  37.         m_appPath=$2  
  38.         shift  
  39.         ;;  
  40.     -i | ipa)  
  41.         m_ipaPath=$2  
  42.         shift  
  43.         ;;  
  44.     -m | msg)  
  45.         m_showMessage=$2  
  46.         shift  
  47.         ;;  
  48.     -h | help)  
  49.         showHelp  
  50.         ;;  
  51.     *)  
  52.             echo "error unknow args : $1"  
  53.             ;;  
  54.         esac  
  55.       
  56.     shift  
  57. done  
  58.   
  59. #开始构建  
  60. echo ">>>>>>>>>> Build Begin "  
  61. make_app_to_ipa $m_appPath $m_ipaPath  
  62. 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 关键字。Mac的target必须包含 Mac 关键字.

[python] view plain copy
  1. def build_ios(self):  
  2.     if not self._platforms.is_ios_active():  
  3.         return  
  4.   
  5.     if not cocos.os_is_mac():  
  6.         raise cocos.CCPluginError("Please build on MacOSX")  
  7.   
  8.     self.check_ios_mac_build_depends()  
  9.   
  10.     project_dir = self._project.get_project_dir()  
  11.     ios_project_dir = self._platforms.project_path()  
  12.     build_mode = self._mode  
  13.     if self._project._is_script_project():  
  14.         if build_mode == 'debug':  
  15.             output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_SCRIPT_DEBUG, 'ios')  
  16.         else:  
  17.             output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_SCRIPT_RELEASE, 'ios')  
  18.     else:  
  19.         output_dir = os.path.join(project_dir, CCPluginCompile.OUTPUT_DIR_NATIVE, build_mode, 'ios')  
  20.   
  21.     projectPath = os.path.join(ios_project_dir, self.xcodeproj_name)  
  22.     pbxprojectPath = os.path.join(projectPath, "project.pbxproj")  
  23.   
  24.     f = file(pbxprojectPath)  
  25.     contents = f.read()  
  26.   
  27.     section = re.search(r"Begin PBXProject section.*End PBXProject section", contents, re.S)  
  28.   
  29.     if section is None:  
  30.         message = "Can't find iOS target"  
  31.         raise cocos.CCPluginError(message)  
  32.   
  33.     targets = re.search(r"targets = (.*);", section.group(), re.S)  
  34.     if targets is None:  
  35.         message = "Can't find iOS target"  
  36.         raise cocos.CCPluginError(message)  
  37.   
  38.     targetName = None  
  39.     cfg_obj = self._platforms.get_current_config()  
  40.     if cfg_obj.target_name is not None:  
  41.         targetName = cfg_obj.target_name  
  42.     else:  
  43.         names = re.split("\*", targets.group())  
  44.         for name in names:  
  45.             if "iOS" in name:  
  46.                 targetName = str.strip(name)  
  47.                 cocos.Logging.info(" >>>>>>>> targetName = %s" % targetName)  
  48.                 if targetName is None:  
  49.                     message = "Can't find iOS target"  
  50.                     raise cocos.CCPluginError(message)  
  51.                   
  52.                 if os.path.isdir(output_dir):  
  53.                     target_app_dir = os.path.join(output_dir, "%s.app" % targetName[:targetName.find(' ')])  
  54.                     if os.path.isdir(target_app_dir):  
  55.                         shutil.rmtree(target_app_dir)  
  56.   
  57.                 cocos.Logging.info("building")  
  58.   
  59.                 command = ' '.join([  
  60.                     "xcodebuild",  
  61.                     "-project",  
  62.                     "\"%s\"" % projectPath,  
  63.                     "-configuration",  
  64.                     "%s" % 'Debug' if self._mode is 'debug' else 'Release',  
  65.                     "-target",  
  66.                     "\"%s\"" % targetName,  
  67.                     "-sdk",  
  68.                     "iphonesimulator",  
  69.                     "-arch i386",  
  70.                     "CONFIGURATION_BUILD_DIR=%s" % (output_dir)  
  71.                     ])  
  72.   
  73.                 self._run_cmd(command)  
  74.                 # app 转 ipa  
  75.                 app_path = os.path.join(output_dir, "%s.app" % targetName[:targetName.find(' ')])  
  76.                 ipa_path = os.path.join(output_dir, "%s.ipa" % targetName[:targetName.find(' ')])  
  77.                 command = ' '.join([  
  78.                      "app2ipa.sh",  
  79.                      "-a",  
  80.                      "\"%s\"" % app_path,  
  81.                      "-i",  
  82.                      "\"%s\"" % ipa_path,  
  83.                      "-m"  
  84.                      ])  
  85.                 cocos.Logging.info(" >>>>> run command %s" % command)  
  86.                 self._run_cmd(command)  
  87.   
  88.     filelist = os.listdir(output_dir)  
  89.   
  90.     for filename in filelist:  
  91.         name, extention = os.path.splitext(filename)  
  92.         if extention == '.a':  
  93.             filename = os.path.join(output_dir, filename)  
  94.             os.remove(filename)  
  95.         if extention == '.app' and name == targetName:  
  96.             filename = os.path.join(output_dir, filename)  
  97.             newname = os.path.join(output_dir, name[:name.find(' ')]+extention)  
  98.             os.rename(filename, newname)  
  99.             self._iosapp_path = newname  
  100.       
  101.     if self._no_res:  
  102.         self._remove_res(self._iosapp_path)  
  103.       
  104.     cocos.Logging.info("build succeeded.")  

好了,我们可以使用如下命令编译了

Python cocos.py compile -s /projects/MyGame/proj.ios_mac -m debug -p iOS

你可能感兴趣的:(Cocos2d-x3.x,iOS)