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

前端之家收集整理的这篇文章主要介绍了Cocos2d-x3.0 iOS 一键编译多个target并打包ipa前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. #!/bin/sh
  2. m_appPath=""
  3. m_ipaPath=""
  4. m_showMessage="NO"
  5. make_app_to_ipa()
  6. {
  7. app_path=$1
  8. ipa_path=$2
  9. if["$m_showMessage"=="YES"]
  10. then
  11. /usr/bin/xcrun-sdkiphoneosPackageApplication-v"$app_path"-o"$ipa_path"
  12. else
  13. /usr/bin/xcrun>/dev/null2>&1-sdkiphoneosPackageApplication-v"$app_path"-o"$ipa_path"
  14. fi
  15. echo">>>>打包ipa完成:$ipa_path"
  16. }
  17. showHelp()
  18. {
  19. echo"Convertapptoipa"
  20. echo"optionalarguments:"
  21. echo"-h,helpshowthishelpmessageandexit"
  22. echo"-a,appappfilepath"
  23. echo"-i,ipaipafilepath"
  24. echo"-m,msgdisplaybuildmessage,{NO,YES}"
  25. exit
  26. }
  27. #//main--------------------------------
  28. until[$#-eq0]
  29. do
  30. case$1in
  31. -a|app)
  32. m_appPath=$2
  33. shift
  34. ;;
  35. -i|ipa)
  36. m_ipaPath=$2
  37. shift
  38. ;;
  39. -m|msg)
  40. m_showMessage=$2
  41. shift
  42. ;;
  43. -h|help)
  44. showHelp
  45. ;;
  46. *)
  47. echo"errorunknowargs:$1"
  48. ;;
  49. esac
  50. shift
  51. done
  52. #开始构建
  53. echo">>>>>>>>>>BuildBegin"
  54. make_app_to_ipa$m_appPath$m_ipaPath
  55. echo">>>>>>>>>>BuildFinished."
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 关键字.

  1. defbuild_ios(self):
  2. ifnotself._platforms.is_ios_active():
  3. return
  4. ifnotcocos.os_is_mac():
  5. raisecocos.CCPluginError("PleasebuildonMacOSX")
  6. self.check_ios_mac_build_depends()
  7. project_dir=self._project.get_project_dir()
  8. ios_project_dir=self._platforms.project_path()
  9. build_mode=self._mode
  10. ifself._project._is_script_project():
  11. ifbuild_mode=='debug':
  12. output_dir=os.path.join(project_dir,CCPluginCompile.OUTPUT_DIR_SCRIPT_DEBUG,'ios')
  13. else:
  14. output_dir=os.path.join(project_dir,CCPluginCompile.OUTPUT_DIR_SCRIPT_RELEASE,CCPluginCompile.OUTPUT_DIR_NATIVE,build_mode,'ios')
  15. projectPath=os.path.join(ios_project_dir,self.xcodeproj_name)
  16. pbxprojectPath=os.path.join(projectPath,"project.pbxproj")
  17. f=file(pbxprojectPath)
  18. contents=f.read()
  19. section=re.search(r"BeginPBXProjectsection.*EndPBXProjectsection",contents,re.S)
  20. ifsectionisNone:
  21. message="Can'tfindioStarget"
  22. raisecocos.CCPluginError(message)
  23. targets=re.search(r"targets=(.*);",section.group(),re.S)
  24. iftargetsisNone:
  25. message="Can'tfindioStarget"
  26. raisecocos.CCPluginError(message)
  27. targetName=None
  28. cfg_obj=self._platforms.get_current_config()
  29. ifcfg_obj.target_nameisnotNone:
  30. targetName=cfg_obj.target_name
  31. else:
  32. names=re.split("\*",targets.group())
  33. fornameinnames:
  34. if"iOS"inname:
  35. targetName=str.strip(name)
  36. cocos.Logging.info(">>>>>>>>targetName=%s"%targetName)
  37. iftargetNameisNone:
  38. message="Can'tfindioStarget"
  39. raisecocos.CCPluginError(message)
  40. ifos.path.isdir(output_dir):
  41. target_app_dir=os.path.join(output_dir,"%s.app"%targetName[:targetName.find('')])
  42. ifos.path.isdir(target_app_dir):
  43. shutil.rmtree(target_app_dir)
  44. cocos.Logging.info("building")
  45. command=''.join([
  46. "xcodebuild",
  47. "-project",
  48. "\"%s\""%projectPath,
  49. "-configuration",
  50. "%s"%'Debug'ifself._modeis'debug'else'Release',
  51. "-target",
  52. "\"%s\""%targetName,
  53. "-sdk",
  54. "iphonesimulator",
  55. "-archi386",
  56. "CONFIGURATION_BUILD_DIR=%s"%(output_dir)
  57. ])
  58. self._run_cmd(command)
  59. #app转ipa
  60. app_path=os.path.join(output_dir,"%s.app"%targetName[:targetName.find('')])
  61. ipa_path=os.path.join(output_dir,"%s.ipa"%targetName[:targetName.find('')])
  62. command=''.join([
  63. "app2ipa.sh",
  64. "-a",
  65. "\"%s\""%app_path,
  66. "-i",
  67. "\"%s\""%ipa_path,
  68. "-m"
  69. ])
  70. cocos.Logging.info(">>>>>runcommand%s"%command)
  71. self._run_cmd(command)
  72. filelist=os.listdir(output_dir)
  73. forfilenameinfilelist:
  74. name,extention=os.path.splitext(filename)
  75. ifextention=='.a':
  76. filename=os.path.join(output_dir,filename)
  77. os.remove(filename)
  78. ifextention=='.app'andname==targetName:
  79. filename=os.path.join(output_dir,filename)
  80. newname=os.path.join(output_dir,name[:name.find('')]+extention)
  81. os.rename(filename,newname)
  82. self._iosapp_path=newname
  83. ifself._no_res:
  84. self._remove_res(self._iosapp_path)
  85. cocos.Logging.info("buildsucceeded.")

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

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

原文链接:https://www.f2er.com/cocos2dx/339204.html

猜你在找的Cocos2d-x相关文章