AndroidManifestHelper.cs 3.49 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Android NetworkDiscovery Multicast fix
// https://github.com/vis2k/Mirror/pull/2887
using UnityEditor;
using UnityEngine;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using System.Xml;
using System.IO;
#if UNITY_ANDROID
using UnityEditor.Android;
#endif


[InitializeOnLoad]
public class AndroidManifestHelper : IPreprocessBuildWithReport, IPostprocessBuildWithReport
#if UNITY_ANDROID
	, IPostGenerateGradleAndroidProject
#endif
{
    public int callbackOrder { get { return 99999; } }

#if UNITY_ANDROID
    public void OnPostGenerateGradleAndroidProject(string path)
	{
        string manifestFolder = Path.Combine(path, "src/main");
        string sourceFile = manifestFolder + "/AndroidManifest.xml";
        // Load android manifest file
        XmlDocument doc = new XmlDocument();
        doc.Load(sourceFile);

        string androidNamepsaceURI;
        XmlElement element = (XmlElement)doc.SelectSingleNode("/manifest");
        if (element == null)
        {
            UnityEngine.Debug.LogError("Could not find manifest tag in android manifest.");
            return;
        }

        // Get android namespace URI from the manifest
        androidNamepsaceURI = element.GetAttribute("xmlns:android");
        if (string.IsNullOrEmpty(androidNamepsaceURI))
        {
            UnityEngine.Debug.LogError("Could not find Android Namespace in manifest.");
            return;
        }
        AddOrRemoveTag(doc,
               androidNamepsaceURI,
               "/manifest",
               "uses-permission",
               "android.permission.CHANGE_WIFI_MULTICAST_STATE",
               true,
               false);
        AddOrRemoveTag(doc,
               androidNamepsaceURI,
               "/manifest",
               "uses-permission",
               "android.permission.INTERNET",
               true,
               false);
        doc.Save(sourceFile);
    }
#endif

    static void AddOrRemoveTag(XmlDocument doc, string @namespace, string path, string elementName, string name, bool required, bool modifyIfFound, params string[] attrs) // name, value pairs
    {
        var nodes = doc.SelectNodes(path + "/" + elementName);
        XmlElement element = null;
        foreach (XmlElement e in nodes)
        {
            if (name == null || name == e.GetAttribute("name", @namespace))
            {
                element = e;
                break;
            }
        }

        if (required)
        {
            if (element == null)
            {
                var parent = doc.SelectSingleNode(path);
                element = doc.CreateElement(elementName);
                element.SetAttribute("name", @namespace, name);
                parent.AppendChild(element);
            }

            for (int i = 0; i < attrs.Length; i += 2)
            {
                if (modifyIfFound || string.IsNullOrEmpty(element.GetAttribute(attrs[i], @namespace)))
                {
                    if (attrs[i + 1] != null)
                    {
                        element.SetAttribute(attrs[i], @namespace, attrs[i + 1]);
                    }
                    else
                    {
                        element.RemoveAttribute(attrs[i], @namespace);
                    }
                }
            }
        }
        else
        {
            if (element != null && modifyIfFound)
            {
                element.ParentNode.RemoveChild(element);
            }
        }
    }

    public void OnPostprocessBuild(BuildReport report) {}
	public void OnPreprocessBuild(BuildReport report) {}
}