cocos2dx3.8 android打包脚本编写

cocos集成了打包命令 cocos compile -p android

在这里并没有采用这个方案,而是编写自己的脚本, 理由如下

  1. 脚本掌握在自己手中可以第一时间解决和发现bug
  2. 游戏项目总会出现各种各样定制的需求,官方不可能给出全部的解决方案

 

为了便于管理和扩展 我们在项目根目录下新建了两个文件夹

  1. build/android:打包脚本目录,
  2. publish/android:apk输出目录 

android的打包分两步:

  1. 编译so
  2. 生成apk

so的编译脚本

#!/usr/bin/env bash

# set .bash_profile or .profile
if [ -f ~/.bash_profile ]; then
PROFILE_NAME=~/.bash_profile
else
PROFILE_NAME=~/.profile
fi
source $PROFILE_NAME

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
APP_ROOT="$DIR/../../.."
APP_ANDROID_ROOT="$DIR"
export COCOS2DX_ROOT=$DIR/../../cocos2d-x
export NDK_DEBUG=1

echo "- config:"
echo "  NDK_ROOT    = $NDK_ROOT"
echo "  COCOS2DX_ROOT       = $COCOS2DX_ROOT"
echo "  APP_ROOT            = $APP_ROOT"
echo "  APP_ANDROID_ROOT    = $APP_ANDROID_ROOT"

echo "- cleanup"
find "$APP_ANDROID_ROOT" -type d | xargs chmod 755 $1
if [ -d "$APP_ANDROID_ROOT"/bin ]; then
    rm -rf "$APP_ANDROID_ROOT"/bin/*.apk
fi
mkdir -p "$APP_ANDROID_ROOT"/bin
chmod 755 "$APP_ANDROID_ROOT"/bin

if [ -d "$APP_ANDROID_ROOT"/assets ]; then
    rm -rf "$APP_ANDROID_ROOT"/assets/*
fi
mkdir -p "$APP_ANDROID_ROOT"/assets
chmod 755 "$APP_ANDROID_ROOT"/assets

# build
echo "Using prebuilt externals"
"$NDK_ROOT"/ndk-build $ANDROID_NDK_BUILD_FLAGS NDK_DEBUG=$NDK_DEBUG $NDK_BUILD_FLAGS -C "$APP_ANDROID_ROOT" $* \
"NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos:${COCOS2DX_ROOT}/external:${COCOS2DX_ROOT}/cocos/scripting"

  apk的生成脚本: 首先拷贝资源(可根据实际渠道拷贝不同的资源)  代码(可根据实际项目需求编译成字节码并混淆加密) 然后ant打包, 如果是release包还要配置签名, 这里提供一个基本的生成debug.apk的脚本

build_android.sh

#!/bin/bash

projectPath="$1"

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${dir}

ant -buildfile ${projectPath}/build.xml

  然后编写脚本调用build_android.sh 传递参数,   为了便于以后的扩展,这里用python编写

# coding=utf-8
# !/usr/bin/python

import os, shutil
import datetime

class BuildAndroid:
    def __init__(self):
        self.dir = os.path.split(os.path.realpath(__file__))[0]
        self.projectPath = "../../frameworks/runtime-src/proj.android"
        self.outputPath = self.dir + "/../../publish/android"
        self.rootPath = "../.."
        self.appName = "XXXXX"
    def build(self):
        rootResPath = self.rootPath + "/res/"
        rootSrcPath = self.rootPath + "/src/"
        projectResPath = self.projectPath + "/assets/res/"
        projectSrcPath = self.projectPath + "/assets/src/"

        if os.path.isdir(projectResPath):
            shutil.rmtree(projectResPath)
        if os.path.isdir(projectSrcPath):
            shutil.rmtree(projectSrcPath)
        shutil.copytree(rootResPath, projectResPath)
        shutil.copytree(rootSrcPath, projectSrcPath)
        
        os.system("sh build_android.sh " + self.projectPath)
        if not os.path.isdir(self.outputPath):
            os.mkdir(self.outputPath)
        #获得当前时间
        now = datetime.datetime.now()
        outputFile = self.outputPath + "/" +self.appName + "_" + now.strftime("%Y%m%d%H%M") + ".apk"
        shutil.copy(self.projectPath + "/bin/" + self.appName + "-debug.apk", outputFile)
        print("[Success] " + outputFile)
    def run(self):
        os.chdir(self.dir)
        self.build()
buildAndroid = BuildAndroid()
buildAndroid.run()

  

 这样一个基本的打包脚本就编写完成了,我们可以根据自己的项目需求来扩展python脚本

posted @ 2015-09-26 20:59  ColaZhang  阅读(2176)  评论(0编辑  收藏  举报