{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/unity-auto-build-scripts/",
  "atlas": "https://www.pystone.net/?node=unity-auto-build-scripts#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/unity-auto-build-scripts.md",
  "context": "https://www.pystone.net/notes/unity-auto-build-scripts.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:unity-auto-build-scripts",
  "slug": "unity-auto-build-scripts",
  "title": "【Unity】自动打包脚本整理",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【Unity】自动打包脚本整理",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "68a1102b38fc9c0e0ee521b62085d795f6ff2f6a4ce0188d7c9da5e3ff65a684",
  "assets": [
    {
      "reference": "assets/0017 - 【Unity】自动打包脚本整理__resource-004-1726d9f5666b.png",
      "url": "/media/f3eeae3b46ebad806c1b.png",
      "mediaType": "image/png",
      "contentHash": "f3eeae3b46ebad806c1bd34014a9c08e8431621ccf563c2acd20bab4b375e001",
      "byteLength": 6423,
      "width": 615,
      "height": 56
    },
    {
      "reference": "assets/0017 - 【Unity】自动打包脚本整理__resource-002-3119e8950063.png",
      "url": "/media/507eba1f74b2ea8d1869.png",
      "mediaType": "image/png",
      "contentHash": "507eba1f74b2ea8d18691254a839ffb5f2bd40826555c5b8f110692f0231614a",
      "byteLength": 10749,
      "width": 364,
      "height": 101
    },
    {
      "reference": "assets/0017 - 【Unity】自动打包脚本整理__resource-003-ae666f8dc70e.png",
      "url": "/media/b5a21baa78635a9e8b42.png",
      "mediaType": "image/png",
      "contentHash": "b5a21baa78635a9e8b422762772dc11a72b3df01bedc7de638cd943fdd1ac2b3",
      "byteLength": 10197,
      "width": 660,
      "height": 91
    },
    {
      "reference": "assets/0017 - 【Unity】自动打包脚本整理__resource-001-e33e6a5fb87a.png",
      "url": "/media/ddcad6ba8bd6a6308c33.png",
      "mediaType": "image/png",
      "contentHash": "ddcad6ba8bd6a6308c337b0c61c38c3ae6ad5cc1ff1be761f5fbd0afc8b5e1f5",
      "byteLength": 53851,
      "width": 580,
      "height": 567
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【Unity】自动打包脚本整理",
      "anchor": "unity自动打包脚本整理",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#unity%E8%87%AA%E5%8A%A8%E6%89%93%E5%8C%85%E8%84%9A%E6%9C%AC%E6%95%B4%E7%90%86"
    },
    {
      "depth": 2,
      "text": "相关技术概念",
      "anchor": "相关技术概念",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E7%9B%B8%E5%85%B3%E6%8A%80%E6%9C%AF%E6%A6%82%E5%BF%B5"
    },
    {
      "depth": 3,
      "text": "ABB",
      "anchor": "abb",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#abb"
    },
    {
      "depth": 3,
      "text": "OBB",
      "anchor": "obb",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#obb"
    },
    {
      "depth": 2,
      "text": "Android SDK 设置相关",
      "anchor": "android-sdk-设置相关",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#android-sdk-%E8%AE%BE%E7%BD%AE%E7%9B%B8%E5%85%B3"
    },
    {
      "depth": 3,
      "text": "UnityEditor界面设置SDK",
      "anchor": "unityeditor界面设置sdk",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#unityeditor%E7%95%8C%E9%9D%A2%E8%AE%BE%E7%BD%AEsdk"
    },
    {
      "depth": 3,
      "text": "脚本设置SDK",
      "anchor": "脚本设置sdk",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E8%84%9A%E6%9C%AC%E8%AE%BE%E7%BD%AEsdk"
    },
    {
      "depth": 3,
      "text": "Best Practice: 使用并修改Unity内置的SDK",
      "anchor": "best-practice-使用并修改unity内置的sdk",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#best-practice-%E4%BD%BF%E7%94%A8%E5%B9%B6%E4%BF%AE%E6%94%B9unity%E5%86%85%E7%BD%AE%E7%9A%84sdk"
    },
    {
      "depth": 3,
      "text": "注：Unity猜测SDK会导致构建时间过长的问题",
      "anchor": "注unity猜测sdk会导致构建时间过长的问题",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E6%B3%A8unity%E7%8C%9C%E6%B5%8Bsdk%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%9E%84%E5%BB%BA%E6%97%B6%E9%97%B4%E8%BF%87%E9%95%BF%E7%9A%84%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "打包脚本",
      "anchor": "打包脚本",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E6%89%93%E5%8C%85%E8%84%9A%E6%9C%AC"
    },
    {
      "depth": 3,
      "text": "设置",
      "anchor": "设置",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E8%AE%BE%E7%BD%AE"
    },
    {
      "depth": 4,
      "text": "通用",
      "anchor": "通用",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E9%80%9A%E7%94%A8"
    },
    {
      "depth": 4,
      "text": "Android",
      "anchor": "android",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#android"
    },
    {
      "depth": 4,
      "text": "iOS平台",
      "anchor": "ios平台",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#ios%E5%B9%B3%E5%8F%B0"
    },
    {
      "depth": 3,
      "text": "打包代码",
      "anchor": "打包代码",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E6%89%93%E5%8C%85%E4%BB%A3%E7%A0%81"
    },
    {
      "depth": 4,
      "text": "Android",
      "anchor": "android-1",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#android-1"
    },
    {
      "depth": 4,
      "text": "iOS平台",
      "anchor": "ios平台-1",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#ios%E5%B9%B3%E5%8F%B0-1"
    },
    {
      "depth": 4,
      "text": "Windows平台",
      "anchor": "windows平台",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#windows%E5%B9%B3%E5%8F%B0"
    },
    {
      "depth": 3,
      "text": "Tool",
      "anchor": "tool",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#tool"
    },
    {
      "depth": 2,
      "text": "遇到的坑",
      "anchor": "遇到的坑",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91"
    },
    {
      "depth": 3,
      "text": "Build java.io.IOException: Unable to delete XXX\\signing-config.json",
      "anchor": "build-javaioioexception-unable-to-delete-xxxsigning-configjson",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#build-javaioioexception-unable-to-delete-xxxsigning-configjson"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/unity-auto-build-scripts/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:software-engineering-and-quality",
      "title": "软件工程与质量保障",
      "url": "https://www.pystone.net/notes/software-engineering-and-quality/",
      "atlas": "https://www.pystone.net/?node=software-engineering-and-quality#knowledge-atlas",
      "label": "软件工程与质量保障",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "CI-CD与质量保障/CI-CD工具链中的“【Unity】自动打包脚本整理”导航项",
      "citation": "https://www.pystone.net/notes/software-engineering-and-quality/#ci-cd%E4%B8%8E%E8%B4%A8%E9%87%8F%E4%BF%9D%E9%9A%9Cci-cd%E5%B7%A5%E5%85%B7%E9%93%BE"
    }
  ],
  "contentMarkdown": "# 【Unity】自动打包脚本整理\n\n﻿# 【Unity】自动打包脚本整理\n\n> 创建时间：2021/4/28 19:51\n\n* 相关技术概念\n    * ABB\n    * OBB\n  * Android SDK 设置相关\n    * UnityEditor界面设置SDK\n    * 脚本设置SDK\n    * Best Practice: 使用并修改Unity内置的SDK\n    * 注：Unity猜测SDK会导致构建时间过长的问题\n  * 打包脚本\n    * 设置\n      * 通用\n      * Android\n      * iOS平台\n    * 打包代码\n      * Android\n      * iOS平台\n      * Windows平台\n    * Tool\n  * 遇到的坑\n    * Build java.io.IOException: Unable to delete XXX\\signing-config.json\n  * Ref\n\n## 相关技术概念\n\n### ABB\n\n### OBB\n\n## Android SDK 设置相关\n\n在打包时，会检测AndroidSDK、NDK等工具的目录，验证目录的正确性，如下图的BuildLog所示：\n\n![](assets/0017 - 【Unity】自动打包脚本整理__resource-004-1726d9f5666b.png)\n\n### UnityEditor界面设置SDK\n\n![](assets/0017 - 【Unity】自动打包脚本整理__resource-002-3119e8950063.png)\n\n在Editor下，勾选\"with Unity\"即为选择UseEmbedded（使用Unity嵌入的Android相关工具）。如果不勾选，可以设置自定义路径，在Preference窗口设置路径的行为与接口`EditorPrefs.SetString(\"AndroidSdkRoot\", value);`设置的内容是一致的。\n\n无论在Editor界面点击Build打包，还是用batchmode调用BuildPlayer函数打包，Unity使用的SDK都会按照 “Edit->Preferences->External Tools” 当中的设置来进行。\n\n> 如果勾选了使用Unity自带的SDK，那么通过设置EditorPrefs方式修改也是没有用的。\n\n### 脚本设置SDK\n\nUnity 使用的SDK配置，是保存在EditorPrefs当中的。可以通过以下脚本修改。\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### Best Practice: 使用并修改Unity内置的SDK\n\nUnity Editor自己安装的SDK就比较方便并且稳定，如果SDK所包含的API Level不符合要求，修改也是比较容易的。\nUnity安装AndroidSDK后，会把SDK放在Unity安装目录下找到以下位置\n\n```csharp\nstring s = String.Empty;\n\n```\n\n使用SDK目录下自带的`sdkmanager.bat`脚本即可实现安装指定的API Level。\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### 注：Unity猜测SDK会导致构建时间过长的问题\n\n而如果使用batchmode运行，如果设置错误，会在环境变量当中查找，存在一个GuessDirectory的过程，代码如下：\n\n```csharp\nstring s = String.Empty;\n\n```\n\n检索可能涉及到的相关环境变量大致如下：\nWindows系统上：\n\n  * ANDROID_SDK_ROOT\n  * ANDROID_NDK_ROOT\n  * LOCALAPPDATA\n  * ProgramFiles\n  * ProgramFiles(x86)\n\n类Unix系统上：\n\n  * HOME/Library/Android/sdk\n\n这就会导致`Detecting Android SDK`的时间过长，或者甚至导致`Android SDK not found`而打包失败。如下图，是某次打包的Editor.log，检测AndroidSDK的耗时约6分钟：\n\n![](assets/0017 - 【Unity】自动打包脚本整理__resource-003-ae666f8dc70e.png)\n\n双击打包的bat脚本与使用Pipeline运行时的区别就在于环境变量不同，因此可能会出现双击bat打包成功，而使用Pipeline打包失败的现象。\n\nPipeline的参数化构建功能就是用来解决环境变量相关问题的。\n\n相关讨论或博客：\nhttps://forum.unity.com/threads/unity-command-line-doesnt-find-android-sdk.162976/\nhttps://blog.csdn.net/KindSuper_liu/article/details/89891780\nhttps://medium.com/@johanesriandy/week-one-setup-jenkins-to-build-android-app-using-gradle-1534755ca1bc\n\n## 打包脚本\n\n### 设置\n\n#### 通用\n\n```csharp\nstring s = String.Empty;\n\n```\n\n#### Android\n\n```csharp\nstring s = String.Empty;\n\n```\n\n#### iOS平台\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### 打包代码\n\n#### Android\n\n```csharp\nstring s = String.Empty;\n\n```\n\n#### iOS平台\n\n```csharp\nstring s = String.Empty;\n\n```\n\n#### Windows平台\n\n```csharp\nstring s = String.Empty;\n\n```\n\n### Tool\n\n```csharp\nstring s = String.Empty;\n\n```\n\n## 遇到的坑\n\n### Build java.io.IOException: Unable to delete XXX\\signing-config.json\n\n方案一：\nJust Delete the File signing_config.json and re-run\nAnd it will work fine\n\n方案二：\n授予Users完全控制权限：\n\n![](assets/0017 - 【Unity】自动打包脚本整理__resource-001-e33e6a5fb87a.png)\n\n## Ref\n\nhttps://chenghu.online/posts/a27a69b1/\n"
}
