Cocos creator打包工程化(py)

背景:之前打包一直手动执行各种脚本,公司发布几个平台(不同服务器),然后上周测试服更新,连续几个版本出现更新问题,搞得人精疲力竭,所以还是交给脚本来执行比较可靠。

当前方式:编译之后将build/jsb-link目录下src,res,subpackages三个文件按照不同的脚本来编译生成版本号的配置文件(原生的是.manifest文件,但是我们支持大厅子游戏更新,所以自定义引擎使用的是.u3d文件作为配置文件),每次都要去改对应的版本号,生成好之后再合并上传。

优化点:1,直接读取项目内版本文件来生成版本号配置文件。

                2,不同平台读取配置文件来读取链接,而不用每个平台创建一个bat

                3,读取项目版本配置中在线服务器标致校验是否是对应版本(避免人为出错)

                4,不开编辑器指令打包

                5,合并压缩

环境:

        cocos creator 2.2,python2.7

配置文件config.ini

[test]
file=version_generator.js
subFile=version_generator_sub_new_test.js
url=https://xxx.ccc.net/client/update/

[game]
file=version_generator.js
subFile=version_generator_sub_new.js
url=https://yyy.dddd.net/client_vn/update/

[india]
file=version_generator.js
subFile=version_generator_sub_new.js
url=https://zzz.ddd.net/game_app/

file是用来生成版本配置文件的js文件:就是官网的js文件

subFile是用来处理subpackages的版本配置文件的js文件

url是将要上传的远程文件地址,

bat文件:

C:/CocosCreator_2.2.0/CocosCreator.exe --path %cd%/../ --build "platform=android;debug=false;md5Cache=false;template=link"
pause
python ./createWebZip.py native game
pause

第一行就是生成native的执行语句,不用打开编辑器。

第二行执行python文件生成版本配置和压缩相关。

python文件:

# -*- coding: UTF-8 -*-
import shutil
import os
import random
import string
import zipfile
import base64
import ConfigParser
import sys

import sys 
      
reload(sys)
sys.setdefaultencoding('utf-8')


#压缩文件
def zipDir(zipFilePath, dirName, curCwd):
    gameAPath = curCwd + os.sep + dirName
    print('Start zip A game:', gameAPath)
    
    with zipfile.ZipFile(zipFilePath, "w", compression=zipfile.ZIP_DEFLATED) as zf:
        for dirpath, dirnames, filenames in os.walk(gameAPath):
            fpath = dirpath.replace(gameAPath, '')
            fpath = fpath and fpath + os.sep or ''
            for filename in filenames:
                zf.write(os.path.join(dirpath, filename), fpath+filename)
        zf.close()

#运维配置的jinkens多了一层目录,这里处理的是将web-mobile先复制到tmp文件夹中,再压缩重命名成web-mobile.zip
def createZipFile(dirName):
    sPath = os.getcwd()
    zipFilePath = sPath + os.sep + dirName + ".zip"
    if os.path.isdir("tmp"):
        shutil.rmtree(sPath + os.sep + "tmp")
    os.mkdir("tmp")
    shutil.move(sPath + os.sep + dirName, sPath + os.sep + "tmp")
    if not os.path.exists(sPath):
        print "先编译生成web-mobile"
    else:
        zipDir(zipFilePath, "tmp", sPath)
    if os.path.isdir("tmp"):
        shutil.rmtree(sPath + os.sep + "tmp")

#获取出版本号和是否在线信息,每个项目不同,获取的方式也不同,
def getGameInfo(uuu3dPath):
    import re
    fileObj = open(uuu3dPath, "rb+")
    fileContent = fileObj.read()
    version = re.findall(r'\d+\.\d+\.\d+', fileContent)
    online = re.findall(r'online_switch[\s]*\:[\s]*([a-z]+)', fileContent)
    fileObj.close()
    if len(version) == 1 and len(online) == 1:
        if online[0] == 'true':
            print "线上发布,请谨慎操作……"
        print "发布版本号:", version[0]
        return version[0], online[0] == 'true'
    else:
        print "版本号获取失败,请检查uuu3d.js配置文件:" + uuu3dPath
        return None, False
    
#执行js文件
def createMainManifest(fileName, url, version):
    os.system("node " + fileName + " -v " + version + " -u " + url + version + "/ -s ./" + version + "/ -d " + version + "/")
    
def createSubManifest(fileName, url, version):
    os.system("node " + fileName + " -u " + url + version + "/game_res -s ./" + version + "/game_res/ -d " + version + "/game_res/")

#将src和res先移出来生成版本文件
def dealUpdateFile(sPath, srcName, version):
    versionDir = sPath + os.sep + version
    originSrc = sPath + os.sep + srcName + os.sep + "src"
    originRes = sPath + os.sep + srcName + os.sep + "res"
    shutil.move(originSrc, versionDir)
    shutil.move(originRes, versionDir)

#将subpackages移动到对应目录然后执行js文件生成版本文件
def dealSubUpdateFile(sPath, srcName, version):
    versionDir = sPath + os.sep + version
    originSub = sPath + os.sep + srcName + os.sep + "subpackages"
    shutil.move(originSub, versionDir)
    os.rename(versionDir + os.sep + "subpackages", versionDir + os.sep + "game_res")


def createWebZipFile(dirName):
    createZipFile(dirName)

#生成native更新文件{game_res:子包,src:代码,res:资源,config.u3d, version.u3d}
def createNativeZipFile(dirName, platform):
    import time
    sPath = os.getcwd()
    #读取项目内配置文件地址,来获取版本号version和是否在线的信息online
    uuu3dPath = sPath + "/../assets/Framework/Unity3d/uuu3d.js"
    version, online = getGameInfo(uuu3dPath)
    zipFilePath = sPath + os.sep + platform + "_" + version + ".zip"
    if(not online and platform != 'test' or online and platform == 'test'):
        print "当前平台和uuu3d配置文件不匹配,请检查校正后再打包……", platform
        return
    if os.path.isfile(zipFilePath):
        #TODO:这里有问题,不能直接相加,文件命名冲突想做成随机名字的
        zipFilePath = zipFilePath.replace('.zip', "_" + time.time() + ".zip")
        print "当前版本号已经存在压缩包:", zipFilePath,"  重命名为:" + zipFilePath
    fileName, subFile, url = getConfig(platform)
    if version:
        versionDir = sPath + os.sep + version
        if os.path.exists(versionDir):
            shutil.rmtree(versionDir)
        os.mkdir(versionDir)
        dealUpdateFile(sPath, dirName, version)
        createMainManifest(fileName, url, version)
        dealSubUpdateFile(sPath, dirName, version)
        createSubManifest(subFile, url, version)
        
        zipDir(zipFilePath, version, sPath)
        if os.path.isdir(version):
            shutil.rmtree(sPath + os.sep + version)

#读取ini配置文件
def getConfig(platform):
    cf=ConfigParser.ConfigParser()
    cf.read("config.ini")
    fileName = cf.get(platform, "file")
    subFile = cf.get(platform, "subFile")
    url = cf.get(platform, "url")
    return fileName, subFile, url
    
if __name__ == '__main__':
    #web平台发布:用编辑器发布之后,再执行指令,因为cocos creator 2.2执行web发布md5Cache无效
    if len(sys.argv) > 0 and sys.argv[1] == 'web-mobile':
        createWebZipFile(sys.argv[1])
    elif len(sys.argv) > 0 and sys.argv[1] == 'native':
        platform = sys.argv[2]
        print "发布平台:", platform
        createNativeZipFile("jsb-link", platform)
        
        

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cocos creator打包APK的过程可以分为以下几个步骤: 1. 配置环境:首先需要安装Android Studio,并按照cocos官方文档的指引进行配置。具体的配置步骤可以参考官方文档(\[2\])。 2. cocos构建发布:在cocos creator中,选择构建发布选项,然后选择Android平台。在构建设置中,可以设置一些参数,如包名、签名等。根据项目的需求进行相应的设置。 3. 打包APK:根据官方文档的提示,使用Android Studio打开构建生成的工程文件(一般位于项目目录下的`build/jsb-default/frameworks/runtime-src/proj.android-studio`)。在Android Studio中,选择Build菜单下的Build Bundle(s) / APK(s)选项,即可开始打包APK的过程。 在打包过程中,可能会遇到一些SDK版本的问题,特别是对于对原生Android开发不熟悉的人来说。如果遇到问题,可以参考官方文档或者在评论区提问,寻求帮助(\[1\])。 #### 引用[.reference_title] - *1* *2* [cocos creator 3.x打包构建原生安卓APK流程(打包release版本,修改APP图标)](https://blog.csdn.net/hangsky1990/article/details/131740544)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [cocos creator 打包原生安卓apk 构建与编译](https://blog.csdn.net/qq_41506812/article/details/118069337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值