예제 #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();
        }
        /// <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));
            }
        }