public override void OnPreprocessBuild(BuildTarget target, string path)
        {
            var isARCoreRequired = ARCoreProjectSettings.Instance.IsARCoreRequired;

            Debug.LogFormat(
                "Building \"{0}\" app. Use 'Edit > Project Settings > ARCore' to adjust ARCore settings.\n" +
                "See {1} for more information.",
                isARCoreRequired ? "AR Required" : "AR Optional",
                "https://developers.google.com/ar/develop/unity/enable-arcore");

            AssetHelper.GetPluginImporterByName("google_ar_required.aar")
            .SetCompatibleWithPlatform(BuildTarget.Android, isARCoreRequired);
            AssetHelper.GetPluginImporterByName("google_ar_optional.aar")
            .SetCompatibleWithPlatform(BuildTarget.Android, !isARCoreRequired);
        }
Beispiel #2
0
        private void _PreprocessAndroidBuild()
        {
            // Get the Jdk path.
            var jdkPath = UnityEditor.EditorPrefs.GetString("JdkPath");

            if (string.IsNullOrEmpty(jdkPath))
            {
                Debug.Log("JDK path Unity pref is not set. Falling back to JAVA_HOME environment variable.");
                jdkPath = System.Environment.GetEnvironmentVariable("JAVA_HOME");
            }

            if (string.IsNullOrEmpty(jdkPath))
            {
                throw new BuildFailedException("A JDK path needs to be specified for the Android build.");
            }

            bool cloudAnchorsEnabled = !string.IsNullOrEmpty(ARCoreProjectSettings.Instance.CloudServicesApiKey);

            var cachedCurrentDirectory = Directory.GetCurrentDirectory();
            var pluginsFolderPath      = Path.Combine(cachedCurrentDirectory,
                                                      AssetDatabase.GUIDToAssetPath(k_PluginsFolderGuid));
            string cloudAnchorsManifestAarPath = Path.Combine(pluginsFolderPath, "cloud_anchor_manifest.aar");
            var    jarPath = Path.Combine(jdkPath, "bin/jar");

            if (cloudAnchorsEnabled)
            {
                // If the Api Key didn't change then do nothing.
                if (!_IsApiKeyDirty(jarPath, cloudAnchorsManifestAarPath,
                                    ARCoreProjectSettings.Instance.CloudServicesApiKey))
                {
                    return;
                }

                // Replace the project's cloud anchor AAR with the newly generated AAR.
                Debug.Log("Enabling Cloud Anchors in this build.");

                var tempDirectoryPath = Path.Combine(cachedCurrentDirectory, FileUtil.GetUniqueTempPathInProject());

                try
                {
                    // Move to a temp directory.
                    Directory.CreateDirectory(tempDirectoryPath);
                    Directory.SetCurrentDirectory(tempDirectoryPath);

                    var manifestTemplatePath = Path.Combine(cachedCurrentDirectory, AssetDatabase.GUIDToAssetPath(k_ManifestTemplateGuid));

                    // Extract the "template AAR" and remove it.
                    string output;
                    string errors;
                    ShellHelper.RunCommand(jarPath, string.Format("xf \"{0}\"", manifestTemplatePath),
                                           out output, out errors);

                    // Replace Api key template parameter in manifest file.
                    var manifestPath = Path.Combine(tempDirectoryPath, "AndroidManifest.xml");
                    var manifestText = File.ReadAllText(manifestPath);
                    manifestText = manifestText.Replace("{{CLOUD_ANCHOR_API_KEY}}", ARCoreProjectSettings.Instance.CloudServicesApiKey);
                    File.WriteAllText(manifestPath, manifestText);

                    // Compress the new AAR.
                    var fileListBuilder = new StringBuilder();
                    foreach (var filePath in Directory.GetFiles(tempDirectoryPath))
                    {
                        fileListBuilder.AppendFormat(" {0}", Path.GetFileName(filePath));
                    }

                    ShellHelper.RunCommand(jarPath,
                                           string.Format("cf cloud_anchor_manifest.aar {0}", fileListBuilder.ToString()),
                                           out output, out errors);

                    if (!string.IsNullOrEmpty(errors))
                    {
                        throw new BuildFailedException(
                                  string.Format("Error creating jar for cloud anchor manifest: {0}", errors));
                    }

                    File.Copy(Path.Combine(tempDirectoryPath, "cloud_anchor_manifest.aar"),
                              cloudAnchorsManifestAarPath, true);
                }
                finally
                {
                    // Cleanup.
                    Directory.SetCurrentDirectory(cachedCurrentDirectory);
                    Directory.Delete(tempDirectoryPath, true);

                    AssetDatabase.Refresh();
                }

                AssetHelper.GetPluginImporterByName("cloud_anchor_manifest.aar")
                .SetCompatibleWithPlatform(BuildTarget.Android, true);
            }
            else
            {
                Debug.Log("A cloud anchor API key has not been set.  Cloud anchors are disabled in this build.");
                File.Delete(cloudAnchorsManifestAarPath);
                AssetDatabase.Refresh();
            }
        }
Beispiel #3
0
        private void PreprocessAndroidBuild()
        {
            string cachedCurrentDirectory = Directory.GetCurrentDirectory();
            string pluginsFolderPath      = Path.Combine(cachedCurrentDirectory,
                                                         AssetDatabase.GUIDToAssetPath(_pluginsFolderGuid));
            string customizedManifestAarPath =
                Path.Combine(pluginsFolderPath, "customized_manifest.aar");

            string jarPath = Path.Combine(AndroidDependenciesHelper.GetJdkPath(), "bin/jar");

            var tempDirectoryPath =
                Path.Combine(cachedCurrentDirectory, FileUtil.GetUniqueTempPathInProject());

            try
            {
                // Move to a temp directory.
                Directory.CreateDirectory(tempDirectoryPath);
                Directory.SetCurrentDirectory(tempDirectoryPath);

                CreateClassesJar(jarPath, tempDirectoryPath);

                CheckCompatibilityWithAllSesssionConfigs(
                    ARCoreProjectSettings.Instance, GetAllSessionConfigs());
                XDocument customizedManifest =
                    GenerateCustomizedAndroidManifest(ARCoreProjectSettings.Instance);
                var manifestPath = Path.Combine(tempDirectoryPath, "AndroidManifest.xml");
                customizedManifest.Save(manifestPath);

                // Compress the new AAR.
                var fileListBuilder = new StringBuilder();
                foreach (var filePath in Directory.GetFiles(tempDirectoryPath))
                {
                    fileListBuilder.AppendFormat(" {0}", Path.GetFileName(filePath));
                }

                string command = string.Format(
                    "cf customized_manifest.aar {0}", fileListBuilder.ToString());

                string output;
                string errors;
                ShellHelper.RunCommand(jarPath, command, out output, out errors);

                if (!string.IsNullOrEmpty(errors))
                {
                    throw new BuildFailedException(
                              string.Format(
                                  "Error creating aar for manifest: {0}", errors));
                }

                File.Copy(Path.Combine(tempDirectoryPath, "customized_manifest.aar"),
                          customizedManifestAarPath, true);
            }
            finally
            {
                // Cleanup.
                Directory.SetCurrentDirectory(cachedCurrentDirectory);
                Directory.Delete(tempDirectoryPath, true);

                AssetDatabase.Refresh();
            }

            AssetHelper.GetPluginImporterByName("customized_manifest.aar")
            .SetCompatibleWithPlatform(BuildTarget.Android, true);
        }
        private void StripAndBackupClientAar()
        {
            // Strip the <queries> tag from the arcore_client.aar when it's incompatible with
            // Unity's built-in gradle version.
            string cachedCurrentDirectory = Directory.GetCurrentDirectory();
            string pluginsFolderPath      = Path.Combine(cachedCurrentDirectory,
                                                         AssetDatabase.GUIDToAssetPath(_pluginsFolderGuid));

            string[] plugins       = Directory.GetFiles(pluginsFolderPath);
            string   clientAarPath = string.Empty;

            foreach (var pluginPath in plugins)
            {
                if (pluginPath.Contains(_clientAarName) &&
                    !pluginPath.Contains(".meta") &&
                    AssetHelper.GetPluginImporterByName(Path.GetFileName(pluginPath))
                    .GetCompatibleWithPlatform(BuildTarget.Android))
                {
                    clientAarPath = pluginPath;
                    break;
                }
            }

            if (string.IsNullOrEmpty(clientAarPath))
            {
                throw new BuildFailedException(
                          string.Format("Cannot find a valid arcore client plugin under '{0}'",
                                        pluginsFolderPath));
            }

            string clientAarFileName = Path.GetFileName(clientAarPath);
            string jarPath           = AndroidDependenciesHelper.GetJdkPath();

            if (string.IsNullOrEmpty(jarPath))
            {
                throw new BuildFailedException("Cannot find a valid JDK path in this build.");
            }

            jarPath = Path.Combine(jarPath, "bin/jar");
            var tempDirectoryPath =
                Path.Combine(cachedCurrentDirectory, FileUtil.GetUniqueTempPathInProject());

            // Back up existing client AAR.
            string backupFilename = clientAarFileName + _backupExtension;

            Debug.LogFormat("Backing up {0} in {1}.", clientAarFileName, backupFilename);
            File.Copy(clientAarPath, Path.Combine(pluginsFolderPath, backupFilename), true);
            _hasBackup = true;

            Debug.LogFormat("Stripping the <queries> tag from {0} in this build.",
                            clientAarFileName);
            try
            {
                // Move to a temp directory.
                Directory.CreateDirectory(tempDirectoryPath);
                Directory.SetCurrentDirectory(tempDirectoryPath);

                // Extract the existing AAR in the temp directory.
                string output;
                string errors;
                ShellHelper.RunCommand(
                    jarPath, string.Format("xf \"{0}\"", clientAarPath), out output,
                    out errors);

                // Strip the <queries> tag from AndroidManifest.xml.
                var manifestPath = Path.Combine(tempDirectoryPath, "AndroidManifest.xml");
                var manifestText = File.ReadAllText(manifestPath);
                manifestText = System.Text.RegularExpressions.Regex.Replace(
                    manifestText, "(/s)?<queries>(.*)</queries>(\n|\r|\r\n)", string.Empty,
                    System.Text.RegularExpressions.RegexOptions.Singleline);
                File.WriteAllText(manifestPath, manifestText);

                // Compress the modified AAR.
                string command = string.Format("cf {0} .", clientAarFileName);
                ShellHelper.RunCommand(
                    jarPath,
                    command,
                    out output,
                    out errors);

                if (!string.IsNullOrEmpty(errors))
                {
                    throw new BuildFailedException(
                              string.Format(
                                  "Error creating jar for stripped arcore client manifest: {0}", errors));
                }

                // Override the existing client AAR with the modified one.
                File.Copy(Path.Combine(tempDirectoryPath, clientAarFileName),
                          clientAarPath, true);
            }
            finally
            {
                // Cleanup.
                Directory.SetCurrentDirectory(cachedCurrentDirectory);
                Directory.Delete(tempDirectoryPath, true);

                AssetDatabase.Refresh();
            }

            AssetHelper.GetPluginImporterByName(clientAarFileName)
            .SetCompatibleWithPlatform(BuildTarget.Android, true);
        }
        private void SetApiKeyOnAndroid()
        {
            string cachedCurrentDirectory = Directory.GetCurrentDirectory();
            string pluginsFolderPath      = Path.Combine(cachedCurrentDirectory,
                                                         AssetDatabase.GUIDToAssetPath(_pluginsFolderGuid));
            string cloudAnchorsManifestAarPath = Path.Combine(pluginsFolderPath, _aarFileName);

            bool cloudAnchorsEnabled =
                !string.IsNullOrEmpty(ARCoreProjectSettings.Instance.CloudServicesApiKey);

            if (cloudAnchorsEnabled)
            {
                string jarPath = AndroidDependenciesHelper.GetJdkPath();
                if (string.IsNullOrEmpty(jarPath))
                {
                    throw new BuildFailedException("Cannot find a valid JDK path in this build.");
                }

                jarPath = Path.Combine(jarPath, "bin/jar");

                // Replace the project's cloud anchor AAR with the newly generated AAR.
                Debug.Log("Enabling Cloud Anchors with API Key Authentication in this build.");

                var tempDirectoryPath =
                    Path.Combine(cachedCurrentDirectory, FileUtil.GetUniqueTempPathInProject());

                try
                {
                    // Move to a temp directory.
                    Directory.CreateDirectory(tempDirectoryPath);
                    Directory.SetCurrentDirectory(tempDirectoryPath);

                    var manifestTemplatePath = Path.Combine(
                        cachedCurrentDirectory,
                        AssetDatabase.GUIDToAssetPath(_manifestTemplateGuid));

                    // Extract the "template AAR" and remove it.
                    string output;
                    string errors;
                    ShellHelper.RunCommand(
                        jarPath, string.Format("xf \"{0}\"", manifestTemplatePath), out output,
                        out errors);

                    // Replace Api key template parameter in manifest file.
                    var manifestPath = Path.Combine(tempDirectoryPath, "AndroidManifest.xml");
                    var manifestText = File.ReadAllText(manifestPath);
                    manifestText = manifestText.Replace(
                        "{{CLOUD_ANCHOR_API_KEY}}",
                        ARCoreProjectSettings.Instance.CloudServicesApiKey);
                    File.WriteAllText(manifestPath, manifestText);

                    // Compress the new AAR.
                    var fileListBuilder = new StringBuilder();
                    foreach (var filePath in Directory.GetFiles(tempDirectoryPath))
                    {
                        fileListBuilder.AppendFormat(" {0}", Path.GetFileName(filePath));
                    }

                    string command = string.Format(
                        "cf {0} {1}", _aarFileName, fileListBuilder.ToString());

                    ShellHelper.RunCommand(
                        jarPath,
                        command,
                        out output,
                        out errors);

                    if (!string.IsNullOrEmpty(errors))
                    {
                        throw new BuildFailedException(
                                  string.Format(
                                      "Error creating jar for cloud anchor manifest: {0}", errors));
                    }

                    File.Copy(Path.Combine(tempDirectoryPath, _aarFileName),
                              cloudAnchorsManifestAarPath, true);
                }
                finally
                {
                    // Cleanup.
                    Directory.SetCurrentDirectory(cachedCurrentDirectory);
                    Directory.Delete(tempDirectoryPath, true);

                    AssetDatabase.Refresh();
                }

                AssetHelper.GetPluginImporterByName(_aarFileName)
                .SetCompatibleWithPlatform(BuildTarget.Android, true);
            }
            else
            {
                Debug.Log(
                    "Cloud Anchor API key has not been set in this build.");
                File.Delete(cloudAnchorsManifestAarPath);
                AssetDatabase.Refresh();
            }
        }