/// <summary> /// Builds an Android App Bundle at the specified location. Assumes that all dependencies are already in-place, /// e.g. aapt2 and bundletool. /// </summary> /// <returns>True if the build succeeded, false if it failed or was cancelled.</returns> public static bool Build(string aabFilePath) { bool buildResult; Debug.LogFormat("Building app bundle: {0}", aabFilePath); // As of February 2019, every released version of Unity natively supporting AAB has used Android Gradle // Plugin 3.2.0 which includes bundletool 0.5.0. Bundletool 0.6.0+ is needed for uncompressNativeLibraries // and version 0.6.1+ is needed for uncompressNativeLibraries with instant apps. // One can #define PLAY_INSTANT_ENABLE_NATIVE_ANDROID_APP_BUNDLE to build using the native AAB builder. #if PLAY_INSTANT_ENABLE_NATIVE_ANDROID_APP_BUNDLE #if PLAY_INSTANT_HAS_NATIVE_ANDROID_APP_BUNDLE EditorUserBuildSettings.buildAppBundle = true; var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(aabFilePath, BuildOptions.None); buildResult = PlayInstantBuilder.Build(buildPlayerOptions); #else throw new System.Exception("Cannot enable native app bundle build on an unsupported Unity version."); #endif #else #if PLAY_INSTANT_HAS_NATIVE_ANDROID_APP_BUNDLE // Disable Unity's built-in AAB build on newer Unity versions before performing the custom AAB build. EditorUserBuildSettings.buildAppBundle = false; // Note: fall through here to the actual build. #endif buildResult = AppBundleBuilder.Build(aabFilePath); #endif if (buildResult) { // Do not log in case of failure. The method we called was responsible for logging. Debug.LogFormat("Finished building app bundle: {0}", aabFilePath); } return(buildResult); }
/// <summary> /// Checks the latest version of Android SDK Build-Tools that is installed, prompting to upgrade if necessary. /// Returns true if the latest version of Android SDK Build-Tools supports the "aapt2 convert" command, /// and false otherwise. /// </summary> public static bool CheckConvert() { var newestBuildToolsVersion = AndroidBuildTools.GetNewestBuildToolsVersion(); if (newestBuildToolsVersion == null) { PlayInstantBuilder.DisplayBuildError(string.Format("Failed to locate {0}", BuildToolsDisplayName)); return(false); } if (AndroidBuildTools.IsBuildToolsVersionAtLeast(newestBuildToolsVersion, BuildToolsMinimumVersion)) { return(true); } var message = string.Format( "App Bundle creation requires {0} version {1} or later.\n\nClick \"OK\" to install {0} version {2}.", BuildToolsDisplayName, BuildToolsMinimumVersion, BuildToolsLatestVersion); if (PlayInstantBuilder.DisplayBuildErrorDialog(message)) { AndroidSdkPackageInstaller.InstallPackage(BuildToolsPackageName, BuildToolsDisplayName); } return(false); }
/// <summary> /// Builds an Android App Bundle at a user-specified file location. /// </summary> public static void Build() { #if !PLAY_INSTANT_ENABLE_NATIVE_ANDROID_APP_BUNDLE if (!AndroidAssetPackagingTool.CheckConvert()) { return; } if (!Bundletool.CheckBundletool()) { return; } #endif if (!PlayInstantBuilder.CheckBuildAndPublishPrerequisites()) { return; } // TODO: add checks for preferred Scripting Backend and Target Architectures. var aabFilePath = EditorUtility.SaveFilePanel("Create Android App Bundle", null, null, "aab"); if (string.IsNullOrEmpty(aabFilePath)) { // Assume cancelled. return; } Build(aabFilePath); }
private static void DisplayBuildError(string errorType, string errorMessage) { if (!WindowUtils.IsHeadlessMode()) { EditorUtility.ClearProgressBar(); } PlayInstantBuilder.DisplayBuildError(string.Format("{0} failed: {1}", errorType, errorMessage)); }
/// <summary> /// Builds an APK to a temporary location using the scenes selected in Unity's main Build Settings. /// </summary> public static void BuildAndRun() { if (!PlayInstantBuilder.CheckBuildPrerequisites()) { return; } var jarPath = Path.Combine(AndroidSdkManager.AndroidSdkRoot, InstantAppsJarPath); if (!File.Exists(jarPath)) { Debug.LogErrorFormat("Build and Run failed to locate ia.jar file at: {0}", jarPath); var message = string.Format( "Failed to locate version 1.2 or later of the {0}.\n\nClick \"OK\" to install the {0}.", InstantAppsSdkDisplayName); if (PlayInstantBuilder.DisplayBuildErrorDialog(message)) { InstallPlayInstantSdk(); } return; } #if UNITY_2018_3_OR_NEWER EditorUserBuildSettings.buildAppBundle = false; #endif var apkPath = Path.Combine(Path.GetTempPath(), "temp.apk"); Debug.LogFormat("Build and Run package location: {0}", apkPath); var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(apkPath, EditorUserBuildSettings.development ? BuildOptions.Development : BuildOptions.None); if (!PlayInstantBuilder.BuildAndSign(buildPlayerOptions)) { // Do not log here. The method we called was responsible for logging. return; } var window = PostBuildCommandLineDialog.CreateDialog("Install and run app"); window.modal = false; window.summaryText = "Installing app on device"; window.bodyText = "The APK built successfully.\n\n"; window.autoScrollToBottom = true; window.CommandLineParams = new CommandLineParameters { FileName = JavaUtilities.JavaBinaryPath, Arguments = string.Format( "-jar {0} run {1}", CommandLine.QuotePath(jarPath), CommandLine.QuotePath(apkPath)) }; window.CommandLineParams.AddEnvironmentVariable( AndroidSdkManager.AndroidHome, AndroidSdkManager.AndroidSdkRoot); window.Show(); }
/// <summary> /// Builds an APK to a temporary location using the scenes selected in Unity's main Build Settings. /// </summary> public static void BuildAndRun() { if (!Directory.Exists(AndroidSdkManager.AndroidSdkRoot)) { PlayInstantBuilder.LogError("Failed to locate the Android SDK. Check Preferences -> External Tools to set the path."); return; } var jarPath = Path.Combine(AndroidSdkManager.AndroidSdkRoot, InstantAppsJarPath); if (!File.Exists(jarPath)) { Debug.LogErrorFormat("Build and Run failed to locate ia.jar file at: {0}", jarPath); var message = string.Format( "Failed to locate version 1.2 or later of the {0}.\n\nClick \"OK\" to install the {0}.", PlayInstantSdkInstaller.InstantAppsSdkName); if (PlayInstantBuilder.DisplayBuildErrorDialog(message)) { PlayInstantSdkInstaller.SetUp(); } return; } var apkPath = Path.Combine(Path.GetTempPath(), "temp.apk"); Debug.LogFormat("Build and Run package location: {0}", apkPath); var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(apkPath, EditorUserBuildSettings.development ? BuildOptions.Development : BuildOptions.None); if (!PlayInstantBuilder.BuildAndSign(buildPlayerOptions)) { // Do not log here. The method we called was responsible for logging. return; } var window = PostBuildCommandLineDialog.CreateDialog("Install and run app"); window.modal = false; window.summaryText = "Installing app on device"; window.bodyText = "The APK built successfully. Waiting for scripts to reload...\n"; window.autoScrollToBottom = true; window.CommandLineParams = new CommandLineParameters { FileName = JavaUtilities.JavaBinaryPath, Arguments = string.Format( "-jar {0} run {1}", CommandLine.QuotePathIfNecessary(jarPath), CommandLine.QuotePathIfNecessary(apkPath)) }; window.CommandLineParams.AddEnvironmentVariable( AndroidSdkManager.AndroidHome, AndroidSdkManager.AndroidSdkRoot); window.Show(); }
/// <summary> /// Builds an APK and stores it in a user specified ZIP file. /// </summary> public static void Build() { if (!PlayInstantBuilder.CheckBuildAndPublishPrerequisites()) { return; } var zipFilePath = EditorUtility.SaveFilePanel("Create APK in ZIP File", null, null, "zip"); if (string.IsNullOrEmpty(zipFilePath)) { // Assume cancelled. return; } #if UNITY_2018_3_OR_NEWER EditorUserBuildSettings.buildAppBundle = false; #endif var baseApkDirectory = Path.GetTempPath(); var baseApkPath = Path.Combine(baseApkDirectory, BaseApkFileName); Debug.LogFormat("Building APK: {0}", baseApkPath); var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(baseApkPath, BuildOptions.None); if (!PlayInstantBuilder.BuildAndSign(buildPlayerOptions)) { // Do not log here. The method we called was responsible for logging. return; } // Zip creation is fast enough so call jar synchronously rather than wait for post build AppDomain reset. var zipFileResult = ZipUtils.CreateZipFile(zipFilePath, baseApkDirectory, BaseApkFileName); if (zipFileResult == null) { Debug.LogFormat("Created ZIP file: {0}", zipFilePath); } else { PlayInstantBuilder.DisplayBuildError(string.Format("Zip creation failed: {0}", zipFileResult)); } }
/// <summary> /// Builds an APK and stores it in a user specified ZIP file. /// </summary> public static void Build() { var zipFilePath = EditorUtility.SaveFilePanel("Create APK in ZIP File", null, null, "zip"); if (string.IsNullOrEmpty(zipFilePath)) { // Assume cancelled. return; } var baseApkDirectory = Path.GetTempPath(); var baseApkPath = Path.Combine(baseApkDirectory, BaseApkFileName); Debug.LogFormat("Building APK: {0}", baseApkPath); var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(baseApkPath, BuildOptions.None); if (!PlayInstantBuilder.BuildAndSign(buildPlayerOptions)) { // Do not log here. The method we called was responsible for logging. return; } // Zip creation is fast enough so call jar synchronously rather than wait for post build AppDomain reset. var arguments = string.Format( "cvf {0} -C {1} {2}", CommandLine.QuotePathIfNecessary(zipFilePath), CommandLine.QuotePathIfNecessary(baseApkDirectory), BaseApkFileName); var result = CommandLine.Run(JavaUtilities.JarBinaryPath, arguments); if (result.exitCode == 0) { Debug.LogFormat("Created ZIP containing base.apk: {0}", zipFilePath); } else { PlayInstantBuilder.LogError(string.Format("Zip creation failed: {0}", result.message)); } }
/// <summary> /// Builds an Android App Bundle at the specified location. Assumes that all dependencies are already in-place, /// e.g. aapt2 and bundletool. /// </summary> /// <returns>True if the build succeeded, false if it failed or was cancelled.</returns> public static bool Build(string aabFilePath) { bool buildResult; Debug.LogFormat("Building app bundle: {0}", aabFilePath); #if UNITY_2018_4_OR_NEWER EditorUserBuildSettings.buildAppBundle = true; var buildPlayerOptions = PlayInstantBuilder.CreateBuildPlayerOptions(aabFilePath, BuildOptions.None); buildResult = PlayInstantBuilder.Build(buildPlayerOptions); #elif UNITY_2018_3_OR_NEWER EditorUserBuildSettings.buildAppBundle = false; buildResult = AppBundleBuilder.Build(aabFilePath); #else buildResult = AppBundleBuilder.Build(aabFilePath); #endif if (!buildResult) { // Do not log in case of failure. The method we called was responsible for logging. Debug.LogFormat("Finished building app bundle: {0}", aabFilePath); } return(buildResult); }
private void OnGUI() { // Edge case that takes place when the plugin code gets re-compiled while this window is open. if (_windowInstance == null) { _windowInstance = this; } var descriptionTextStyle = new GUIStyle(GUI.skin.label) { fontStyle = FontStyle.Italic, wordWrap = true }; EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Android Build Type", EditorStyles.boldLabel, GUILayout.Width(FieldWidth)); var index = EditorGUILayout.Popup(_isInstant ? 1 : 0, PlatformOptions); _isInstant = index == 1; EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (_isInstant) { _instantUrl = GetLabelAndTextField("Instant Apps URL (Optional)", _instantUrl); var packageName = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android) ?? "package-name"; EditorGUILayout.LabelField( "Instant apps are launched from web search, advertisements, etc via a URL. Specify the URL here " + "and configure Digital Asset Links. Or, leave the URL blank and one will automatically be " + "provided at:", descriptionTextStyle); EditorGUILayout.SelectableLabel(string.Format( "https://{0}/{1}", InstantAppsHostName, packageName), descriptionTextStyle); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Scenes in Build", EditorStyles.boldLabel); EditorGUILayout.Space(); EditorGUILayout.LabelField( "The scenes in the build are selected via Unity's \"Build Settings\" window. " + "This can be overridden by specifying a comma separated scene list below.", descriptionTextStyle); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); var defaultScenes = string.IsNullOrEmpty(_scenesInBuild) ? string.Join(", ", PlayInstantBuilder.GetEditorBuildEnabledScenes()) : "(overridden)"; EditorGUILayout.LabelField( string.Format("\"Build Settings\" Scenes: {0}", defaultScenes), EditorStyles.wordWrappedLabel); if (GUILayout.Button("Update", GUILayout.Width(100))) { GetWindow(Type.GetType("UnityEditor.BuildPlayerWindow,UnityEditor"), true); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); _scenesInBuild = GetLabelAndTextField("Override Scenes (Optional)", _scenesInBuild); _assetBundleManifestPath = GetLabelAndTextField("AssetBundle Manifest (Optional)", _assetBundleManifestPath); EditorGUILayout.LabelField( "If you use AssetBundles, provide the path to your AssetBundle Manifest file to ensure that " + "required types are not stripped during the build process.", descriptionTextStyle); } else { EditorGUILayout.LabelField( "The \"Installed\" build type is used when creating a traditional installed APK. " + "Select \"Instant\" to build a Google Play Instant APK.", descriptionTextStyle); } EditorGUILayout.Space(); EditorGUILayout.Space(); // Disable the Save button unless one of the fields has changed. GUI.enabled = IsAnyFieldChanged(); if (GUILayout.Button("Save")) { if (_isInstant) { SelectPlatformInstant(); } else { SelectPlatformInstalled(); } } GUI.enabled = true; }
/// <summary> /// Build an app bundle at the specified path, overwriting an existing file if one exists. /// </summary> /// <returns>True if the build succeeded, false if it failed or was cancelled.</returns> public static bool Build(string aabFilePath) { var binaryFormatFilePath = Path.GetTempFileName(); Debug.LogFormat("Building Package: {0}", binaryFormatFilePath); // Do not use BuildAndSign since this signature won't be used. if (!PlayInstantBuilder.Build( PlayInstantBuilder.CreateBuildPlayerOptions(binaryFormatFilePath, BuildOptions.None))) { // Do not log here. The method we called was responsible for logging. return(false); } // TODO: currently all processing is synchronous; consider moving to a separate thread try { DisplayProgress("Running aapt2", 0.2f); var workingDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "play-instant-unity")); if (workingDirectory.Exists) { workingDirectory.Delete(true); } workingDirectory.Create(); var sourceDirectoryInfo = workingDirectory.CreateSubdirectory("source"); var destinationDirectoryInfo = workingDirectory.CreateSubdirectory("destination"); var protoFormatFileName = Path.GetRandomFileName(); var protoFormatFilePath = Path.Combine(sourceDirectoryInfo.FullName, protoFormatFileName); var aaptResult = AndroidAssetPackagingTool.Convert(binaryFormatFilePath, protoFormatFilePath); if (aaptResult != null) { DisplayBuildError("aapt2", aaptResult); return(false); } DisplayProgress("Creating base module", 0.4f); var unzipFileResult = ZipUtils.UnzipFile(protoFormatFileName, sourceDirectoryInfo.FullName); if (unzipFileResult != null) { DisplayBuildError("Unzip", unzipFileResult); return(false); } File.Delete(protoFormatFilePath); ArrangeFiles(sourceDirectoryInfo, destinationDirectoryInfo); var baseModuleZip = Path.Combine(workingDirectory.FullName, BaseModuleZipFileName); var zipFileResult = ZipUtils.CreateZipFile(baseModuleZip, destinationDirectoryInfo.FullName, "."); if (zipFileResult != null) { DisplayBuildError("Zip creation", zipFileResult); return(false); } // If the .aab file exists, EditorUtility.SaveFilePanel() has already prompted for whether to overwrite. // Therefore, prevent Bundletool from throwing an IllegalArgumentException that "File already exists." File.Delete(aabFilePath); DisplayProgress("Running bundletool", 0.6f); var buildBundleResult = Bundletool.BuildBundle(baseModuleZip, aabFilePath); if (buildBundleResult != null) { DisplayBuildError("bundletool", buildBundleResult); return(false); } DisplayProgress("Signing bundle", 0.8f); var signingResult = ApkSigner.SignZip(aabFilePath); if (signingResult != null) { DisplayBuildError("Signing", signingResult); return(false); } } finally { if (!WindowUtils.IsHeadlessMode()) { EditorUtility.ClearProgressBar(); } } return(true); }