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