Skip to content

Instantly share code, notes, and snippets.

@mopsicus
Created September 27, 2021 07:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mopsicus/1d825521b6db402910dbaca742f49651 to your computer and use it in GitHub Desktop.
Update AndroidManifest.xml before build to set hardwareAccelerated to true
using System.IO;
using System.Text;
using System.Xml;
using UnityEditor.Android;
public class PreBuildManifestUpdate : IPostGenerateGradleAndroidProject {
/// <summary>
/// Method runs after gradle generated, before build
/// </summary>
public void OnPostGenerateGradleAndroidProject(string basePath) {
AndroidManifest manifest = new AndroidManifest(GetManifestPath(basePath));
manifest.SetHardwareAcceleration();
manifest.Save();
}
/// <summary>
/// Callback
/// </summary>
public int callbackOrder {
get {
return 1;
}
}
/// <summary>
/// Cached manifest path
/// </summary>
private string _manifestFilePath = null;
/// <summary>
/// Get manifest file
/// </summary>
private string GetManifestPath(string basePath) {
if (string.IsNullOrEmpty(_manifestFilePath)) {
StringBuilder pathBuilder = new StringBuilder(basePath);
pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
_manifestFilePath = pathBuilder.ToString();
}
return _manifestFilePath;
}
}
internal class AndroidXmlDocument : XmlDocument {
/// <summary>
/// Cached path
/// </summary>
private string _path = null;
/// <summary>
/// Namespace manager
/// </summary>
protected XmlNamespaceManager _namespaceManager = null;
/// <summary>
/// XML namespace
/// </summary>
public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
/// <summary>
/// Constructor
/// </summary>
/// <param name="path">Path for document</param>
public AndroidXmlDocument(string path) {
_path = path;
using (var reader = new XmlTextReader(_path)) {
reader.Read();
Load(reader);
}
_namespaceManager = new XmlNamespaceManager(NameTable);
_namespaceManager.AddNamespace("android", AndroidXmlNamespace);
}
/// <summary>
/// Save file
/// </summary>
public string Save() {
return SaveAs(_path);
}
/// <summary>
/// Save file as
/// </summary>
/// <param name="path">Path to save</param>
public string SaveAs(string path) {
using (XmlTextWriter writer = new XmlTextWriter(path, new UTF8Encoding(false))) {
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}
internal class AndroidManifest : AndroidXmlDocument {
/// <summary>
/// Application element
/// </summary>
private readonly XmlElement _application = null;
/// <summary>
/// Constructor
/// </summary>
/// <param name="path">Path to manifest</param>
public AndroidManifest(string path) : base(path) {
_application = SelectSingleNode("/manifest/application") as XmlElement;
}
/// <summary>
/// Create new attribute to manifest
/// </summary>
private XmlAttribute CreateAndroidAttribute(string key, string value) {
XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
attr.Value = value;
return attr;
}
/// <summary>
/// Get main activity
/// </summary>
internal XmlNode GetActivityWithLaunchIntent() {
return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and intent-filter/category/@android:name='android.intent.category.LAUNCHER']", _namespaceManager);
}
/// <summary>
/// Enable hardware acceleration
/// </summary>
internal void SetHardwareAcceleration() {
Logger.Info("Set hardware acceleration done");
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment