首页 > 其他分享 >cocos creator 快速生成apk

cocos creator 快速生成apk

时间:2023-05-06 21:23:12浏览次数:38  
标签:cocos creator format gameName apk new path os

#!/usr/bin/python
#coding:utf8

import os
import sys
import re
import execjs, time

curFilepath = os.path.realpath(__file__)
storeFile = os.path.join(os.path.split(curFilepath)[0], 'xxx.keystore')
samsung = 'samsung'
MTPProjectPath = os.path.abspath(os.path.join(curFilepath, "../../.."))
MTPProjectBuild = os.path.join(MTPProjectPath, 'build/jsb-default')
MTPProjectBuild2 = os.path.join(MTPProjectPath, 'build/apkCache')
SRC_APKTEMP = os.path.join(os.path.split(curFilepath)[0], samsung)
DST_APKTEMP = os.path.join(MTPProjectBuild2, samsung)
apktool = os.path.join(os.path.split(curFilepath)[0], 'apktool_2.7.0.jar')

bundleNames = {
}

SUB_GAMES = {
}

BUNDLE_IDS = {
}

def readConfig(gameName):
    packageName = BUNDLE_IDS[gameName].replace("com", "co")
    cfgPath = 'assets/resources/xxx.ts'
    filename = os.path.join(MTPProjectPath, cfgPath)

    with open(filename, 'r', encoding='UTF-8') as file:
        result = file.read()
        result = result.replace("export class xxx","class xxxxx")

    context = execjs.compile(result)

    return context.call('xxxxxxx.readConfig', packageName)

CONFIG = None


# 替换成对应的游戏
def replaceJSGame(gameName):
    new_s = ''
    jsPath = os.path.join(DST_APKTEMP, "assets/assets/resources/index.js")
    with open(jsPath, 'r') as f:
        con = f.read()
        new_s = re.sub(r"(exports.xxxxxxxxxxx\.)\w+;", r"\1{};".format(gameName), con)
        f.close()

    with open(jsPath, 'w') as f:
        f.write(new_s)

# 修改安装名
def replaceAppName(gameName):
    new_s = ''
    jsPath = os.path.join(DST_APKTEMP, "res/values/strings.xml")
    with open(jsPath, 'r') as f:
        con = f.read()
        new_s = con.replace('GameName', 'Fast-{}'.format(gameName))
        new_s = new_s.replace("172594591747244", CONFIG['facebook_app_id'])
        f.close()

    with open(jsPath, 'w') as f:
        f.write(new_s)

#  修改androidmanifest.xml
def replaceAndroidManifest(gameName):
    new_s = ''
    jsPath = os.path.join(DST_APKTEMP, "AndroidManifest.xml")
    with open(jsPath, 'r') as f:
        con = f.read()
        new_s = con.replace("xxxxxxx.samsung", BUNDLE_IDS[gameName])
        new_s = new_s.replace("172594591747244", CONFIG['facebook_app_id'])
        f.close()

    with open(jsPath, 'w') as f:
        f.write(new_s)

def replaceVersion(gameName):
    new_s = ''
    jsPath = os.path.join(DST_APKTEMP, "assets/manifest/project.manifest")
    pattern = r'"version":".*?"'
    with open(jsPath, 'r') as f:
        con = f.read()
        new_s = re.sub(pattern, r'"version":"3.1.1.1"', con)
        f.close()

    with open(jsPath, 'w') as f:
        f.write(new_s)

def replaceBuildConfig(gameName):
    new_s = ''
    jsPath = os.path.join(DST_APKTEMP, "smali_classes3/co/xxxx/xxx/BuildConfig.smali")
    with open(jsPath, 'r') as f:
        con = f.read()
        new_s = con.replace("bundle.samsung", BUNDLE_IDS[gameName])
        f.close()

    with open(jsPath, 'w') as f:
        f.write(new_s)

# 签名apk
def sign(unSignApkFile, signApkFile):
    password='avia123'
    keyAlias='avia'
    fmtCmd = 'jarsigner -keystore {storeFile} -signedjar  {signApkFile}  {unSignApkFile} {keyAlias} -keypass {password} -storepass {password}'
    cmd = fmtCmd.format(storeFile=storeFile, password=password, signApkFile=signApkFile, unSignApkFile=unSignApkFile, keyAlias=keyAlias)
    return os.system(cmd)

# 检查游戏是否存在, 以及是否有index.js, 非加密的构建
def checkGameName(gameName):
    path = os.path.join(MTPProjectBuild, "assets", bundleNames[gameName]) 
    pathJs = os.path.join(MTPProjectBuild, "assets/main/index.js")
    return os.path.exists(path) and os.path.exists(pathJs) 

# 生成apk
def installApk(gameName):
    global CONFIG
    CONFIG = readConfig(gameName)
    assert CONFIG, "读取配置失败"
    start_time = time.time()
    if os.path.exists(DST_APKTEMP):
        os.system('rm -rf {}'.format(DST_APKTEMP))

    if not os.path.exists(MTPProjectBuild2):
        os.makedirs(MTPProjectBuild2)

    print("开始构建安装包: {}".format(gameName))
    assert checkGameName(gameName), "游戏 {} 不存在或者非Debug构建".format(gameName)
    cmd = 'cp -r {} {}'.format(SRC_APKTEMP, DST_APKTEMP)
    os.system(cmd)

    copys = [
        "assets/main",
        "assets/internal",
        "assets/resources",
        "main.js",
        "src",
        "jsb-adapter",
        "manifest",
    ]
    for bundleNameId in SUB_GAMES[gameName].split(','):
        bundleName = "bundleName_{}".format(bundleNameId)
        copys.append("assets/{}".format(bundleName))

    # copy scirpt
    for copy in copys:
        target = DST_APKTEMP
        if copy.startswith('assets'):
            target = os.path.join(DST_APKTEMP, 'assets')
        cmd = 'cp -r {}/{} {}/assets'.format(MTPProjectBuild, copy, target)
        print('copy: {}'.format(copy))
        assert os.system(cmd)==0, "copy {} 失败".format(copy)

    # 修改对应包的内容
    replaceJSGame(gameName)
    replaceVersion(gameName)
    replaceAppName(gameName)
    replaceAndroidManifest(gameName)
    replaceBuildConfig(gameName)

    apkPath = os.path.join(MTPProjectBuild2, 'temp.apk')
    print('正在生成apk')
    cmd = 'java -jar {} b {} -o {}'.format(apktool, DST_APKTEMP, apkPath)
    assert os.system(cmd)==0, "生成apk失败"

    print('正在签名apk')
    signApkFile = apkPath.replace('.apk', '-S.apk')
    assert sign(apkPath, signApkFile)==0, "签名apk失败"

    print('正在安装apk')
    cmd = 'rm -rf {} && adb install -r {}'.format(apkPath, signApkFile)
    assert os.system(cmd)==0, "安装apk失败"

    print('正在启动游戏')
    cmd = 'adb shell am start -n {}/org.cocos2dx.javascript.AppActivity'.format(BUNDLE_IDS[gameName])
    assert os.system(cmd)==0, "启动游戏失败"

    end_time = time.time()
    print('cost time: {}s'.format(round(end_time-start_time)))

def reInstallApk(gameName):
    start_time = time.time()
    apkPath = os.path.join(MTPProjectBuild2, 'temp.apk')
    print('正在生成apk')
    cmd = 'java -jar {} b {} -o {}'.format(apktool, DST_APKTEMP, apkPath)
    assert os.system(cmd)==0, "生成apk失败"

    print('正在签名apk')
    signApkFile = apkPath.replace('.apk', '-S.apk')
    assert sign(apkPath, signApkFile)==0, "签名apk失败"

    print('正在安装apk')
    cmd = 'rm -rf {} && adb install -r {}'.format(apkPath, signApkFile)
    assert os.system(cmd)==0, "安装apk失败"

    print('正在启动游戏')
    cmd = 'adb shell am start -n {}/org.cocos2dx.javascript.AppActivity'.format(BUNDLE_IDS[gameName])
    assert os.system(cmd)==0, "启动游戏失败"

    end_time = time.time()
    print('cost time: {}s'.format(round(end_time-start_time)))

# reInstallApk("BBS")

installApk("BBS")

因为有保密协议,对部分数据进行了和谐, 因为咱们的项目比较多,用这个可以提高工作效率

标签:cocos,creator,format,gameName,apk,new,path,os
From: https://www.cnblogs.com/dzqdzq/p/17378479.html

相关文章

  • 安卓逆向系列教程(二)APK 和 DEX
    安卓逆向系列教程(二)APK和DEX作者:飞龙APKAPK是Android软件包的分发格式,它本身是个Zip压缩包。APK根目录下可能出现的目录和文件有:名称用途META-INF存放元数据AndroidManifest.xml编译后的全局配置文件assets存放资源文件,不会编译classes.dex编译并打包后的源代码lib存放二进......
  • [Termux]更换Termux源 安装Debian容器并 设置Debian镜像源且 安装code-server(附安卓/
    前言Termux开发者称已经不会在GooglePlay上更新该应用了,要么在Github下载要么去F-Driod下载,为了方便下载,本文已经给出下载链接...GitHub下载链接:https://github.com/termux/termux-app/releases/download/v0.118.0/termux-app_v0.118.0+github-debug_universal.apk(GitHub......
  • Windows中qtcreator怎么将编译路径更改为当前目录?
       像VC那样,将执行文件所在debug或release目录,放置到源文件的当前目录是不错的选择。因为便于查找。   要实现这个将编译路径更改为当前目录功能,其实很简单,只需要在项目属性页的“构建目录”所在的编辑框中输入“./”即可。   此时,会在源文件的当前目录中产生......
  • Apk签名_自签名_命令行_四字节对齐_apktool_apksigner_keytool
    生成秘钥库keytool-genkey-alias别名-keyalgRSA-validity证书有效天数-keystore名称.keystore如需查看:keytool-list-keystore"antma.keystore"注意使用jarsigner签名为v1无法安装在新的设备上以下工具都在Androidstudio的SDK中apksigner签名代码:apksigner......
  • APK签名
    0背景通过对Apk进行签名,开发者可以证明对Apk的所有权和控制权,可用于安装和更新其应用。而在Android设备上的安装Apk,如果是一个没有被签名的Apk,则会被拒绝安装。在安装Apk的时候,软件包管理器也会验证Apk是否已经被正确签名,并且通过签名证书和数据摘要验证是否合法......
  • pip 安装库是报错ERROR: Cannot unpack file C:\Users\LX\AppData\Local\Temp\p
    使用pip安装python库的时候出现报错:ERROR:CannotunpackfileC:\Users\LX\AppData\Local\Temp\pip-unpack-apk_4xkw\simple(downloadedfromC:\Users\LX\AppData\Local\Temp\pip-req-build-htbv29co,content-type:text/html;charset=utf-8);cannotdetectarch......
  • Gitee自动部署 cocoscreator web端
    Gitee自动部署参考文档:Gitee目前支持特性:推送代码到Gitee时,由配置的WebHook触发Jenkins任务构建。评论提交记录触发提交记录对应版本Jenkins任务构建提交PullRequest到Gitee项目时,由配置的WebHook触发Jenkins任务构建,支持PR动作:新建,更新,接受,关闭,审查通过,测试......
  • 如何通过Android studio 安装卸载apk
    原文地址zhuanlan.zhihu.com残枫cps​目录收起adb配置安装删除apkadb配置自己在安装AndroidStudio的adb时遇到了配置好了环境变量,在cmd中可以执行adb命令行,而在AndroidStudio中的Terminal中却无法执行,显示adb不是内部或外部命令.一、配置环境变量首先要找到adb.exe的......
  • apko不依赖dockerfile基于apk 包构建oci 镜像的工具
    可以方便的基于apk包进行oci镜像的构建以及发布包含的特性可重复执行同时确认二进制文件一致快速小sbom支持服务支持,基于s4安装可以通过goinstall以及docker模式运行goinstallchainguard.dev/apko@latest使用配置contents:repositories:-https://mirrors.aliyun.com/alpin......
  • melange+ apko 基于的alpine docker 镜像集成玩法
    主要是一个集成,同时可以提供方便的alpineapk包管理以及维护参考集成  备注:以上中使用git进行配置管理,使用melange进行apk包的构建,同时可以结合s3将数据存储到s3中,如果构建基于alpine镜像的可以直接使用构建的私服,同时也可以结合apko进行oci镜像构建,也比较高效方便参......