Unity3d+Jenkins 自動編譯iOS、Android版本

一、在Unity3d中, 建立導出 iOS、Android 項目腳本 PerformBuild.cs ,放在Editor目錄下(必須),以下:javascript

using UnityEditor;
using System.IO;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
 
class PerformBuild
{
    static string[] GetBuildScenes()
    {
        List<string> names = new List<string>();
 
        foreach(EditorBuildSettingsScene e in EditorBuildSettings.scenes)
        {
            if(e==null)
                continue;
 
            if(e.enabled)
                names.Add(e.path);
        }
        return names.ToArray();
    }
 
    static string GetBuildPath()
    {
        string dirPath = Application.dataPath +"/../../../build/iPhone";
        if(!System.IO.Directory.Exists(dirPath)){
            System.IO.Directory.CreateDirectory(dirPath);
        }
        return dirPath;
    }
 
    [UnityEditor.MenuItem("Tools/PerformBuild/Test Command Line Build iPhone Step")]
    static void CommandLineBuild ()
    {
        Debug.Log("Command line build\n------------------\n------------------");
 
        string[] scenes = GetBuildScenes();
        string path = GetBuildPath();
        if(scenes == null || scenes.Length==0 || path == null)
            return;
 
        Debug.Log(string.Format("Path: \"{0}\"", path));
        for(int i=0; i<scenes.Length; ++i)
        {
            Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
        }
 
        Debug.Log("Starting Build!");
        BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iPhone, BuildOptions.None);
    }
 
    static string GetBuildPathAndroid()
    {
        string dirPath = Application.dataPath +"/../../../build/android";
        if(!System.IO.Directory.Exists(dirPath)){
            System.IO.Directory.CreateDirectory(dirPath);
        }
        return dirPath;
    }
 
    [UnityEditor.MenuItem("Tools/PerformBuild/Test Command Line Build Step Android")]
    static void CommandLineBuildAndroid ()
    {
        Debug.Log("Command line build android version\n------------------\n------------------");
 
        string[] scenes = GetBuildScenes();
        string path = GetBuildPathAndroid();
        if(scenes == null || scenes.Length==0 || path == null)
            return;
 
        Debug.Log(string.Format("Path: \"{0}\"", path));
        for(int i=0; i<scenes.Length; ++i)
        {
            Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
        }
 
        Debug.Log("Starting Android Build!");
        BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
    }
}
View Code

完成以後在Tools菜單下 會有 測試菜單 ,能夠試一下是否能夠成功導出項目。php

二、建立Jenkins配置文件 build.xml  和 參數文件  settings.propertieshtml

build.xml 以下:(有其餘需求,編輯此文件便可)java

 <?xml version="1.0" encoding="UTF-8"?>
<project name="wx_client" default="main" basedir=".">
  <property environment="env"/>
  <property name="RAW_BUILD_NUMBER" value="${env.BUILD_NUMBER}"/>
  <script language="javascript"><![CDATA[
    var buildNum = parseInt(project.getProperty("RAW_BUILD_NUMBER"))
    var minorBuildNumber = buildNum % 100;
    var majorBuildNumber = (Math.floor(buildNum / 100)) % 100;
    var uberBuildNumber = (Math.floor(buildNum / 10000)) % 100;
    project.setProperty("minorBuildNumber", minorBuildNumber);
    project.setProperty("majorBuildNumber", majorBuildNumber);
    project.setProperty("uberBuildNumber", uberBuildNumber);
  ]]></script>
  <property name="VERSION_STRING" value="${uberBuildNumber}.${majorBuildNumber}.${minorBuildNumber}"/>
  <property file="settings.properties"/>
  <property name="dev.dir.absolute" location="${dev.dir}"/>
  <tstamp>
    <format property="TODAY_STAMP" pattern="yyyy_MM_dd" locale="en,US"/>
  </tstamp>
  <tstamp>
    <format property="MONTH_STAMP" pattern="M" locale="en,US"/>
  </tstamp>
  <property name="VERSION_NUM" value="${uberBuildNumber}.${majorBuildNumber}.${minorBuildNumber}"/>
  <property name="VERSION_STRING" value="honorbound_${VERSION_NUM}"/>
  
  <target name="main" depends="ios, android, zipDsym" />
  
  <target name="android" depends="compile-android" />
  
  <target name="ios" depends="compile-ios" />
  
  <target name="clean">
    <echo message="Cleaning Build Space"/>
    <delete dir="${build.dir}"/>
    <mkdir dir="${build.dir}"/>
  </target>
  
  <target name="compile-ios" depends="clean">
    
    <echo message="Compiling unity project for version ${VERSION_STRING}"/>
    
    <echo message="${unity.bin} -projectPath ${dev.dir.absolute} -executeMethod PerformBuild.CommandLineBuild -batchmode -quit"/>
    <exec executable="${unity.bin}" failonerror="true">
      <arg line="-projectPath ${dev.dir.absolute}"/>
      <arg line="-executeMethod PerformBuild.CommandLineBuild"/>
      <arg line="-batchmode"/>
      <arg line="-quit"/>
    </exec>
    
    <replace file="${build.dir}/iPhone/Info.plist" preservelastmodified="true">
      <replacefilter token="999.999.999" value="${VERSION_NUM}" />
    </replace>
    
    <exec executable="${security.bin}" failonerror="true">
      <arg value="unlock-keychain"/>
      <arg line="-p ${keychain.password}"/>
    </exec>
    
    
    <echo message="Building ${build.dir}/iPhone/Unity-iPhone.xcodeproj/ Sym Root ${build.dir}/iPhone/"/>
    <exec executable="${xcode.bin}" failonerror="true">
      <arg line="-project ${build.dir}/iPhone/Unity-iPhone.xcodeproj/"/>
      <arg line="PROVISIONING_PROFILE=${provisioning.guid}"/>
      <arg line="-sdk iphoneos"/>
      <arg line='CODE_SIGN_IDENTITY="${codesign.identity}"'/>
      <arg line='GCC_GENERATE_DEBUGGING_SYMBOLS=YES'/>
      <arg line='DEBUG_INFORMATION_FORMAT=dwarf-with-dsym'/>
      <arg line='DWARF_DSYM_FILE_SHOULD_ACCOMPANY_PRODUCT=YES'/>
      <arg line='DEBUGGING_SYMBOLS=YES'/>
      
      <arg line="-configuration Release"/>
    </exec>
    
    <exec executable="${xcrun.bin}" failonerror="true">
      <arg line="-sdk iphoneos"/>
      <arg line="PackageApplication"/>
      <arg line="-v ${build.dir}/iPhone/build/${APP_NAME}.app"/>
      <arg line="-o ${build.dir}/iPhone/build/${APP_NAME}.ipa"/>
    </exec>
    
    <copy file="${build.dir}/iPhone/build/${APP_NAME}.ipa" tofile="${ios.build.deploy.folder}/${VERSION_STRING}.ipa"/>
    
    
  </target>
  
  <target name="compile-android" depends="clean">
    
    <echo message="Compiling unity project for android version ${VERSION_STRING}"/>
    
    
    <echo message="${unity.bin} -projectPath ${dev.dir.absolute} -executeMethod PerformBuild.CommandLineBuildAndroid -batchmode -quit"/>
    <exec executable="${unity.bin}" failonerror="true">
      <arg line="-projectPath ${dev.dir.absolute}"/>
      <arg line="-executeMethod PerformBuild.CommandLineBuildAndroid"/>
      <arg line="-batchmode"/>
      <arg line="-quit"/>
    </exec>
    
    <copy file="${build.dir}/android" tofile="${android.build.deploy.folder}/${VERSION_STRING}.apk"/>
    <exec executable="curl" dir="${android.build.deploy.folder}">
      <arg line="--form file=@${VERSION_STRING}.apk '${BUILD_URL}/save_file.php?appname=${APP_NAME}'" />
    </exec>
  </target>
  
  
  <target name="zipDsym">
    <echo message="Zipping dSym"/>
    <exec executable="zip" dir="${build.dir}/iPhone/build" failonerror="true">
      <arg value="-r"/>
      <arg value="${APP_NAME}.app.dSYM.zip"/>
      <arg value="${APP_NAME}.app.dSYM"/>
    </exec>
  </target>
  
</project>
View Code

settings.properties 以下: (注意把下面該改爲本身項目的改一下)android

注:xcode5 去  /Users/<home>/Library/MobileDevice/Provisioning Profiles 目錄找證書GUIDios

 

unity.bin = /Applications/Unity/Unity.app/Contents/MacOS/Unity
xcode.bin = /usr/bin/xcodebuild
xcrun.bin = /usr/bin/xcrun
security.bin = /usr/bin/security
provisioning.guid = SOME-GUID-GOES-HERE(證書GUID)
codesign.identity = iPhone Distribution: Joe Developer
build.dir = ../UnityClient/build (PerformBuild到的目錄)
dev.dir = ../UnityClient/ (Unity3d項目目錄)
keychain.password = Woohoo
APP_NAME=honorbound
ios.build.deploy.folder = /Users/Shared/Jenkins/builds/ios
android.build.deploy.folder = /Users/Shared/Jenkins/builds/android
(最後兩個是打包好的ipa 和 apk 要放到的地方)
View Code

三、打開Jenkins,按以下步驟新建一個Job:git

   3.一、github

3.二、找到  構建 -> 增長構建步驟 -> Invoke Antxcode

選擇Invoke Ant

 

3.三、選擇高級app

3.四、輸入 build File 路徑 和 Properties 路徑,而後保存job

四、如今就能夠點擊 當即構建  開始編譯你的項目了,開始前請先退出Unity3d,不然會報錯。

    此外 iOS 須要MAC相關環境

 

參考資料:

一、http://blog.juiceboxmobile.com/2013/03/08/unity3d-ios-android-jenkins-autobuild/

二、https://gist.github.com/jmcguirk/5113641

三、http://docs.unity3d.com/Documentation/Manual/CommandLineArguments.html

相關文章
相關標籤/搜索