コード例 #1
0
        /// <summary>
        /// Copy srcaar files to aar files that are excluded from Unity's build process.
        /// </summary>
        /// <param name="dependencies">Dependencies to inject.</param>
        /// <returns>true if successful, false otherwise.</returns>
        private static bool CopySrcAars(ICollection <Dependency> dependencies)
        {
            bool succeeded = true;
            var  aarFiles  = new List <string>();

            // Copy each .srcaar file to .aar while configuring the plugin importer to ignore the
            // file.
            foreach (var aar in LocalMavenRepository.FindAarsInLocalRepos(dependencies))
            {
                var  dir            = Path.GetDirectoryName(aar);
                var  filename       = Path.GetFileNameWithoutExtension(aar);
                var  targetFilename = Path.Combine(dir, filename + ".aar");
                bool configuredAar  = File.Exists(targetFilename);
                if (!configuredAar)
                {
                    bool copiedAndLabeledAar = AssetDatabase.CopyAsset(aar, targetFilename);
                    if (copiedAndLabeledAar)
                    {
                        var unlabeledAssets = new HashSet <string>();
                        PlayServicesResolver.LabelAssets(
                            new [] { targetFilename },
                            complete: (unlabeled) => { unlabeledAssets.UnionWith(unlabeled); });
                        copiedAndLabeledAar = unlabeledAssets.Count == 0;
                    }
                    if (copiedAndLabeledAar)
                    {
                        try {
                            PluginImporter importer = (PluginImporter)AssetImporter.GetAtPath(
                                targetFilename);
                            importer.SetCompatibleWithAnyPlatform(false);
                            importer.SetCompatibleWithPlatform(BuildTarget.Android, false);
                            configuredAar = true;
                        } catch (Exception ex) {
                            PlayServicesResolver.Log(String.Format(
                                                         "Failed to disable {0} from being included by Unity's " +
                                                         "internal build.  {0} has been deleted and will not be " +
                                                         "included in Gradle builds. ({1})", aar, ex),
                                                     level: LogLevel.Error);
                        }
                    }
                    else
                    {
                        PlayServicesResolver.Log(String.Format(
                                                     "Unable to copy {0} to {1}.  {1} will not be included in Gradle " +
                                                     "builds.", aar, targetFilename), level: LogLevel.Error);
                    }
                    if (configuredAar)
                    {
                        aarFiles.Add(targetFilename);
                        // Some versions of Unity do not mark the asset database as dirty when
                        // plugin importer settings change so reimport the asset to synchronize
                        // the state.
                        AssetDatabase.ImportAsset(targetFilename, ImportAssetOptions.ForceUpdate);
                    }
                    else
                    {
                        if (File.Exists(targetFilename))
                        {
                            AssetDatabase.DeleteAsset(targetFilename);
                        }
                        succeeded = false;
                    }
                }
            }
            foreach (var aar in aarFiles)
            {
                if (!LocalMavenRepository.PatchPomFile(aar))
                {
                    succeeded = false;
                }
            }
            return(succeeded);
        }
コード例 #2
0
        /// <summary>
        /// Copy srcaar files to aar files that are excluded from Unity's build process.
        /// </summary>
        /// <param name="dependencies">Dependencies to inject.</param>
        /// <returns>true if successful, false otherwise.</returns>
        private static bool CopySrcAars(ICollection <Dependency> dependencies)
        {
            bool succeeded = true;
            var  aarFiles  = new List <KeyValuePair <string, string> >();

            // Copy each .srcaar file to .aar while configuring the plugin importer to ignore the
            // file.
            foreach (var aar in LocalMavenRepository.FindAarsInLocalRepos(dependencies))
            {
                // Only need to copy for .srcaar
                if (Path.GetExtension(aar).CompareTo(".srcaar") != 0)
                {
                    continue;
                }

                var aarPath = aar;
                if (FileUtils.IsUnderPackageDirectory(aar))
                {
                    // Physical paths work better for copy operations than
                    // logical Unity paths.
                    var physicalPackagePath = FileUtils.GetPackageDirectory(aar,
                                                                            FileUtils.PackageDirectoryType.PhysicalPath);
                    aarPath = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
                        aar, physicalPackagePath);
                }
                var dir = FileUtils.ReplaceBaseAssetsOrPackagesFolder(
                    Path.GetDirectoryName(aar),
                    GooglePlayServices.SettingsDialog.LocalMavenRepoDir);
                var filename       = Path.GetFileNameWithoutExtension(aarPath);
                var targetFilename = Path.Combine(dir, filename + ".aar");

                // Avoid situations where we can have a mix of file path
                // separators based on platform.
                aarPath        = FileUtils.NormalizePathSeparators(aarPath);
                targetFilename = FileUtils.NormalizePathSeparators(
                    targetFilename);

                bool configuredAar = File.Exists(targetFilename);
                if (!configuredAar)
                {
                    var error = PlayServicesResolver.CopyAssetAndLabel(
                        aarPath, targetFilename);
                    if (String.IsNullOrEmpty(error))
                    {
                        try {
                            PluginImporter importer = (PluginImporter)AssetImporter.GetAtPath(
                                targetFilename);
                            importer.SetCompatibleWithAnyPlatform(false);
                            importer.SetCompatibleWithPlatform(BuildTarget.Android, false);
                            configuredAar = true;
                        } catch (Exception ex) {
                            PlayServicesResolver.Log(String.Format(
                                                         "Failed to disable {0} from being included by Unity's " +
                                                         "internal build.  {0} has been deleted and will not be " +
                                                         "included in Gradle builds. ({1})", aar, ex),
                                                     level: LogLevel.Error);
                        }
                    }
                    else
                    {
                        PlayServicesResolver.Log(String.Format(
                                                     "Unable to copy {0} to {1}.  {1} will not be included in Gradle " +
                                                     "builds. Reason: {2}", aarPath, targetFilename, error),
                                                 level: LogLevel.Error);
                    }
                }
                if (configuredAar)
                {
                    aarFiles.Add(new KeyValuePair <string, string>(aarPath, targetFilename));
                    // Some versions of Unity do not mark the asset database as dirty when
                    // plugin importer settings change so reimport the asset to synchronize
                    // the state.
                    AssetDatabase.ImportAsset(targetFilename, ImportAssetOptions.ForceUpdate);
                }
                else
                {
                    if (File.Exists(targetFilename))
                    {
                        AssetDatabase.DeleteAsset(targetFilename);
                    }
                    succeeded = false;
                }
            }
            foreach (var keyValue in aarFiles)
            {
                succeeded &= LocalMavenRepository.PatchPomFile(keyValue.Value, keyValue.Key);
            }
            return(succeeded);
        }