Ten minutes to teach you to build Jenkins to export Unity Android environment tutorial

foreword

If you want to configure a different packaging tool, you can package it yourself to see the effect if you want a running package at any time. This will use the tutorial on building Jenkins packaging and exporting Unity Android environment shared with you in this article.

preparation tools

To prepare the Jenkins environment, you can choose to download it from the Jenkins official website. I have prepared a Jenkins war package here.

Building steps

The first premise is that the local Unity has installed the UnityAndroid packaging environment to ensure that the Androidapk package can be correctly exported by Unity. Here, I will not introduce too much about how Unity exports the Android environment and solve it by itself.

Start Jenkins on the command line, java-jarjenkins.war, and you will see the following operation, which means the startup is successful. You can enter http://localhost:8080/ in the browser to test, and you will see the Jenkins website.

Build the project

Pay attention to the change of the windows path and the command line path. The above picture is the path of the batch processing of Unity's packaged apk. This batch processing is as shown in the figure below, and it should be placed under the Unity project.

Note: If the batch path cannot contain spaces, if there are spaces, you can add double quotes to ensure that the batch can correctly export apk. The batch code is as follows, and pay attention to save it in utf8:

@echo off
echo "Start build apk..."
C:\"Program Files"\Unity1748f1\Unity\Editor\Unity.exe -projectPath D:\study\gitoschina\jenkinsTest\HelloUnity -quit -batchmode -executeMethod PerformBuild.CommandLineBuildAndroid -logFile build.log
REM %1 -projectPath %2 -quit -batchmode -executeMethod APKBuild.Build -logFile build.log
if not %errorlevel%==0 ( goto fail ) else ( goto success )
:success
echo Build APK OK
goto end
:fail
echo Build APK Fail
goto end
:end
pause

Add the automatically built code in the project Editor directory:

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.iOS, BuildOptions.None);
    }
    static string GetBuildPathAndroid()
    {
        string dirPath = Application.dataPath.Replace("/Assets", "") + "/../build/android/"+Application.productName+".apk";
        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)
        {
            Debug.LogError("Please add scene to buildsetting...");
            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);
    }
}

Click Jenkins build

Generate apk locally

The above is the whole content of building Jenkins to package and export the Unity Android environment. I hope it can be helpful to everyone.

Guess you like

Origin blog.csdn.net/bycw666/article/details/123632416