|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +namespace Firebase.Editor { |
| 18 | + using System; |
| 19 | + using System.IO; |
| 20 | + using System.Xml; |
| 21 | + using UnityEngine; |
| 22 | + using UnityEditor; |
| 23 | + using UnityEditor.Build; |
| 24 | + using UnityEditor.Build.Reporting; |
| 25 | + |
| 26 | + internal class AnalyticsFixPropertyRemover : IPreprocessBuildWithReport { |
| 27 | + private static string DefaultAndroidManifestContents = String.Join("\n", |
| 28 | + "<?xml version=\"1.0\" encoding=\"utf-8\"?>", |
| 29 | + "<manifest", |
| 30 | + " xmlns:android=\"http://schemas.android.com/apk/res/android\"", |
| 31 | + " package=\"com.unity3d.player\"", |
| 32 | + " xmlns:tools=\"http://schemas.android.com/tools\">", |
| 33 | + " <application>", |
| 34 | + " <activity android:name=\"com.unity3d.player.UnityPlayerActivity\"", |
| 35 | + " android:theme=\"@style/UnityThemeSelector\">", |
| 36 | + " <intent-filter>", |
| 37 | + " <action android:name=\"android.intent.action.MAIN\" />", |
| 38 | + " <category android:name=\"android.intent.category.LAUNCHER\" />", |
| 39 | + " </intent-filter>", |
| 40 | + " <meta-data android:name=\"unityplayer.UnityActivity\" android:value=\"true\" />", |
| 41 | + " </activity>", |
| 42 | + " </application>", |
| 43 | + "</manifest>" |
| 44 | + ); |
| 45 | + |
| 46 | + private static string SearchTag = "AnalyticsFixPropertyRemover"; |
| 47 | + private static string CommentToAdd = "This was added by the AnalyticsFixPropertyRemover. " + |
| 48 | + "If you want to prevent the generation of this, have \"" + SearchTag + |
| 49 | + "\" included in a comment"; |
| 50 | + |
| 51 | + public int callbackOrder { get { return 0; } } |
| 52 | + |
| 53 | + public void OnPreprocessBuild(BuildReport report) { |
| 54 | + // Only run this logic when building for Android. |
| 55 | + if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Locate the AndroidManifest file. |
| 60 | + string androidPluginsDir = Path.Combine(Application.dataPath, "Plugins", "Android"); |
| 61 | + string androidManifestPath = Path.Combine(androidPluginsDir, "AndroidManifest.xml"); |
| 62 | + |
| 63 | + // If the AndroidManifest file doesn't exist, generate it. |
| 64 | + if (!File.Exists(androidManifestPath)) { |
| 65 | + File.WriteAllText(androidManifestPath, DefaultAndroidManifestContents); |
| 66 | + } |
| 67 | + |
| 68 | + // Check for the SearchTag, and if present there is nothing to do. |
| 69 | + if (File.ReadAllText(androidManifestPath).Contains(SearchTag)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + XmlDocument doc = new XmlDocument(); |
| 74 | + doc.Load(androidManifestPath); |
| 75 | + |
| 76 | + // Find the "application" node |
| 77 | + XmlNode applicationNode = doc.SelectSingleNode("/manifest/application"); |
| 78 | + |
| 79 | + if (applicationNode != null) { |
| 80 | + // Create the new "property" node |
| 81 | + XmlElement propertyNode = doc.CreateElement("property"); |
| 82 | + |
| 83 | + // Create and add the attribute (with namespace) |
| 84 | + XmlAttribute toolsAttribute = doc.CreateAttribute("tools", "node", "http://schemas.android.com/tools"); |
| 85 | + toolsAttribute.Value = "removeAll"; |
| 86 | + propertyNode.Attributes.Append(toolsAttribute); |
| 87 | + |
| 88 | + // Create a comment node, and add it before the property node |
| 89 | + XmlComment comment = doc.CreateComment(CommentToAdd); |
| 90 | + |
| 91 | + // Add the new node to the "application" node |
| 92 | + applicationNode.AppendChild(propertyNode); |
| 93 | + applicationNode.InsertBefore(comment, propertyNode); |
| 94 | + |
| 95 | + // Save the modified XML document |
| 96 | + doc.Save(androidManifestPath); |
| 97 | + } |
| 98 | + else { |
| 99 | + Debug.LogError("Could not find the 'application' node in the AndroidManifest.xml file."); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments