Im using unity3d and I want to add some attributes the android manifest that unity generated, this is my code, modified from this comment https://stackoverflow.com/a/54894488/2126254
public class ModifyUnityAndroidAppManifest : IPostGenerateGradleAndroidProject
{
public void OnPostGenerateGradleAndroidProject(string basePath)
{
var androidManifest = new AndroidManifest(GetManifestPath(basePath));
XmlAttribute ReplaceBackupAttr = androidManifest.GenerateAttribute(
"tools", "replace", "android:allowBackup", androidManifest.ToolsXmlNamespace);
XmlAttribute AllowBackupAttr = androidManifest.GenerateAttribute(
"android", "allowBackup", "true", androidManifest.AndroidXmlNamespace);
androidManifest.SetAttribute(ReplaceBackupAttr);
androidManifest.SetAttribute(AllowBackupAttr);
androidManifest.Save();
}
public int callbackOrder { get { return 1; } }
private string _manifestFilePath;
private string GetManifestPath(string basePath)
{
... // irrelevnat
}
}
internal class AndroidXmlDocument : XmlDocument
{
private string m_Path;
protected XmlNamespaceManager nsMgr;
public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
public readonly string ToolsXmlNamespace = "http://schemas.android.com/apk/res/tools";
public AndroidXmlDocument(string path)
{
m_Path = path;
using (var reader = new XmlTextReader(m_Path))
{
reader.Read();
Load(reader);
}
nsMgr = new XmlNamespaceManager(NameTable);
nsMgr.AddNamespace("android", AndroidXmlNamespace);
nsMgr.AddNamespace("tools", ToolsXmlNamespace);
}
public string Save()
{
return SaveAs(m_Path);
}
public string SaveAs(string path)
{
using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}
internal class AndroidManifest : AndroidXmlDocument
{
internal readonly XmlElement ApplicationElement;
public AndroidManifest(string path) : base(path)
{
ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
}
internal XmlAttribute GenerateAttribute(string prefix, string key, string value, string XmlNamespace)
{
XmlAttribute attr = CreateAttribute(prefix, key, XmlNamespace);
attr.Value = value;
return attr;
}
internal void SetAttribute(XmlAttribute Attribute)
{
ApplicationElement.Attributes.Append(Attribute);
}
}
My issue is that after I add the 2 attributes (replace and allowBackup), the tools Namespace is also appended to the end of the tag
<application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner" tools:replace="android:allowBackup" android:allowBackup="true" xmlns:tools="http://schemas.android.com/apk/res/tools">
How can I fix this? I tried setting XmlNamespace to null, but this causes the prefix (tools,android) not to be printed.