/// <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 void StartDownload()
        {
            Debug.Log("Downloading bundletool...");
            var bundletoolUri = string.Format(
                "https://github.com/google/bundletool/releases/download/{0}/bundletool-all-{0}.jar",
                Bundletool.BundletoolVersion);

            _downloadRequest =
                GooglePlayInstantUtils.StartFileDownload(bundletoolUri, Bundletool.GetBundletoolJarPath());
        }
Esempio n. 3
0
        /// <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);
        }
        private void Update()
        {
            if (_downloadRequest == null)
            {
                return;
            }

            if (_downloadRequest.isDone)
            {
                EditorUtility.ClearProgressBar();

                if (GooglePlayInstantUtils.IsNetworkError(_downloadRequest))
                {
                    var downloadRequestError = _downloadRequest.error;
                    _downloadRequest.Dispose();
                    _downloadRequest = null;

                    Debug.LogErrorFormat("Bundletool download error: {0}", downloadRequestError);
                    if (EditorUtility.DisplayDialog("Download Failed",
                                                    string.Format("{0}\n\nClick \"{1}\" to retry.", downloadRequestError, WindowUtils.OkButtonText),
                                                    WindowUtils.OkButtonText,
                                                    WindowUtils.CancelButtonText))
                    {
                        StartDownload();
                    }
                    else
                    {
                        EditorApplication.delayCall += Close;
                    }

                    return;
                }

                // Download succeeded.
                var bundletoolJarPath = Bundletool.GetBundletoolJarPath();
                GooglePlayInstantUtils.FinishFileDownload(_downloadRequest, bundletoolJarPath);
                _downloadRequest.Dispose();
                _downloadRequest = null;

                Debug.LogFormat("Bundletool downloaded: {0}", bundletoolJarPath);
                var message = string.Format(
                    "Bundletool has been downloaded to your project's \"Library\" directory: {0}", bundletoolJarPath);
                if (EditorUtility.DisplayDialog("Download Complete", message, WindowUtils.OkButtonText))
                {
                    EditorApplication.delayCall += Close;
                }

                return;
            }

            // Download is in progress.
            if (EditorUtility.DisplayCancelableProgressBar(
                    "Downloading bundletool", null, _downloadRequest.downloadProgress))
            {
                EditorUtility.ClearProgressBar();
                _downloadRequest.Abort();
                _downloadRequest.Dispose();
                _downloadRequest = null;
                Debug.Log("Cancelled bundletool download.");
            }
        }