Example #1
0
        /// <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();
        }
Example #2
0
        /// <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();
        }
Example #3
0
        public static void BuildAndRun()
        {
            if (!PlayInstantBuildConfiguration.IsPlayInstantScriptingSymbolDefined())
            {
                Debug.LogError("Build and Run halted since selected platform is Installed");
                const string message = "The currently selected Android Platform is \"Installed\".\n\n" +
                                       "Click \"OK\" to open the \"Configure Instant or Installed\" " +
                                       "window where the platform can be changed to \"Instant\".";
                if (EditorUtility.DisplayDialog(BuildAndRunErrorTitle, message, OkButtonText, CancelButtonText))
                {
                    PlayInstantSettingsWindow.ShowWindow();
                }

                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 (EditorUtility.DisplayDialog(BuildAndRunErrorTitle, message, OkButtonText, CancelButtonText))
                {
                    PlayInstantSdkInstaller.SetUp();
                }

                return;
            }

            var failedPolicies = new List <string>(PlayInstantSettingPolicy.GetRequiredPolicies()
                                                   .Where(policy => !policy.IsCorrectState())
                                                   .Select(policy => policy.Name));

            if (failedPolicies.Count > 0)
            {
                Debug.LogErrorFormat("Build and Run halted due to incompatible settings: {0}",
                                     string.Join(", ", failedPolicies.ToArray()));
                var message = string.Format(
                    "{0}\n\nClick \"OK\" to open the settings window and make required changes.",
                    string.Join("\n\n", failedPolicies.ToArray()));
                if (EditorUtility.DisplayDialog(BuildAndRunErrorTitle, message, OkButtonText, CancelButtonText))
                {
                    PlayerAndBuildSettingsWindow.ShowWindow();
                }

                return;
            }

            var apkPath = Path.Combine(Path.GetTempPath(), "temp.apk");

            Debug.LogFormat("Build and Run package location: {0}", apkPath);

            var buildPlayerOptions = CreateBuildPlayerOptions(apkPath);

#if UNITY_2018_1_OR_NEWER
            var buildReport = BuildPipeline.BuildPlayer(buildPlayerOptions);
            switch (buildReport.summary.result)
            {
            case BuildResult.Cancelled:
                Debug.Log("Build cancelled");
                return;

            case BuildResult.Succeeded:
                // BuildPlayer can fail and still return BuildResult.Succeeded so detect by checking totalErrors.
                if (buildReport.summary.totalErrors > 0)
                {
                    // No need to display a message since Unity will already have done this.
                    return;
                }

                // Actual success: continue on to the run step.
                break;

            case BuildResult.Failed:
                LogError(string.Format("Build failed with {0} error(s)", buildReport.summary.totalErrors));
                return;

            default:
                LogError("Build failed with unknown error");
                return;
            }
#else
            var buildPlayerResult = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (!string.IsNullOrEmpty(buildPlayerResult))
            {
                // Check for intended build cancellation.
                if (buildPlayerResult == "Building Player was cancelled")
                {
                    Debug.Log(buildPlayerResult);
                }
                else
                {
                    LogError(buildPlayerResult);
                }

                return;
            }
#endif

            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}", jarPath, apkPath),
            };
            window.CommandLineParams.AddEnvironmentVariable(
                AndroidSdkManager.AndroidHome, AndroidSdkManager.AndroidSdkRoot);
            window.Show();
        }