Exemple #1
0
        private static void PostprocessAndroidBuild()
        {
            Debug.Log("ARCore: Cleaning up Keyless dependencies.");

            // Run the pre-process step with <c>Keyless</c> disabled so project files get reset.
            // Then run the ExternalDependencyManager dependency resolution which will remove
            // the Keyless dependencies.
            PreprocessAndroidBuild(false);
            AndroidDependenciesHelper.DoPlayServicesResolve();
        }
Exemple #2
0
        private static void PreprocessAndroidBuild(bool enabledKeyless)
        {
            AndroidDependenciesHelper.SetAndroidPluginEnabled(
                enabledKeyless, _androidKeylessPluginGuid);
            AndroidDependenciesHelper.UpdateAndroidDependencies(
                enabledKeyless, _androidKeylessDependenciesGuid);

            if (enabledKeyless)
            {
                Debug.Log("ARCore: Including Keyless dependencies in this build.");
                AndroidDependenciesHelper.DoPlayServicesResolve();
            }
        }
        private static bool IsCloudAnchorModeEnabled()
        {
            foreach (ARCoreSessionConfig config in
                     AndroidDependenciesHelper.GetAllSessionConfigs().Keys)
            {
                if (config.CloudAnchorMode != CloudAnchorMode.Disabled)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #4
0
        private static CloudAnchorMode GetActiveCloudAnchorMode()
        {
            CloudAnchorMode mode = CloudAnchorMode.Disabled;

            foreach (ARCoreSessionConfig config in
                     AndroidDependenciesHelper.GetAllSessionConfigs().Keys)
            {
                if (config.CloudAnchorMode > mode)
                {
                    mode = config.CloudAnchorMode;
                }
            }

            return(mode);
        }
Exemple #5
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();
            }
        }