Created
September 23, 2019 07:12
-
-
Save RoryDungan/c449399e2cccca8a67003d5d47bb17af to your computer and use it in GitHub Desktop.
Unity Android build keystore tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Assets.Editor | |
{ | |
/// <summary> | |
/// Utiltiy for making Android builds that automatically sets up the | |
/// keystore. | |
/// </summary> | |
public class BuildUtility | |
{ | |
private const string AndroidBuildLocationPref = "AndroidBuildLocation"; | |
private const string iOSBuildLocationPref = "iOSBuildLocation"; | |
private const string AndroidBuildConfigFile = "android-build-settings.json"; | |
[MenuItem("Tools/Build Android", false, 100)] | |
public static void BuildAndroidMenu() | |
{ | |
BuildAndroidPrompt(false); | |
} | |
[MenuItem("Tools/Build Android (development)", false, 101)] | |
public static void BuildAndroidMenuDev() | |
{ | |
BuildAndroidPrompt(true); | |
} | |
[MenuItem("Tools/Set up Android keystore", false, 102)] | |
public static void SetupKeystoreMenu() | |
{ | |
SetupKeystore(GetBuildConfig()); | |
} | |
private static void SetupKeystore(BuildConfig config) | |
{ | |
PlayerSettings.Android.keystoreName = config.KeystoreName; | |
PlayerSettings.Android.keystorePass = config.KeystorePassword; | |
PlayerSettings.Android.keyaliasName = config.AliasName; | |
PlayerSettings.Android.keyaliasPass = config.AliasPassword; | |
} | |
private static BuildConfig GetBuildConfig() | |
{ | |
var buildConfigFile = File.ReadAllText(AndroidBuildConfigFile); | |
return JsonUtility | |
.FromJson<BuildConfig>(buildConfigFile); | |
} | |
public static void BuildAndroidPrompt(bool debug) | |
{ | |
var buildLocation = GetLastAndroidBuildLocation(); | |
var buildFolder = string.IsNullOrEmpty(buildLocation) | |
? string.Empty | |
: Path.GetDirectoryName(buildLocation); | |
var buildName = string.IsNullOrEmpty(buildLocation) | |
? string.Empty | |
: Path.GetFileName(buildLocation); | |
var path = EditorUtility | |
.SaveFilePanel("Build", buildFolder, buildName, "apk"); | |
if (!string.IsNullOrEmpty(path)) | |
{ | |
EditorPrefs.SetString(AndroidBuildLocationPref, path); | |
MakeAndroidBuild(path, debug); | |
} | |
} | |
private static void MakeAndroidBuild(string buildPath, bool debug) | |
{ | |
var config = GetBuildConfig(); | |
SetupKeystore(config); | |
var buildResult = BuildPipeline.BuildPlayer( | |
config.Levels, | |
buildPath, | |
BuildTarget.Android, | |
debug | |
? BuildOptions.Development & BuildOptions.AllowDebugging | |
: BuildOptions.None | |
); | |
Debug.Log("Build finished, result: " + buildResult); | |
} | |
private static string GetLastAndroidBuildLocation() | |
{ | |
return EditorPrefs.GetString(AndroidBuildLocationPref); | |
} | |
private class BuildConfig | |
{ | |
public string KeystoreName; | |
public string KeystorePassword; | |
public string AliasName; | |
public string AliasPassword; | |
public string[] Levels; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment