Skip to content

Instantly share code, notes, and snippets.

@vpfrimmer
Last active January 12, 2024 01:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpfrimmer/49f9eb68db937c37cfea4de8afdab2df to your computer and use it in GitHub Desktop.
Save vpfrimmer/49f9eb68db937c37cfea4de8afdab2df to your computer and use it in GitHub Desktop.
IPostGenerateGradleAndroidProject to replace the instal location to "auto" in the generated AndroidManifest - Unity 2022.x
using System.IO;
using UnityEditor;
using UnityEditor.Android;
using UnityEngine;
/// <summary>
/// Used as a workaround for Unity 2022.x who isn't able to set the installlocation value to "auto" in the AndroidManifest.xml file.
/// Should be put in the Assets/Editor folder, otherwise you'll get errors on build.
/// The issue can be followed here : https://issuetracker.unity3d.com/issues/android-install-location-changes-when-exporting-project
/// </summary>
class CustomGradleProcessor : IPostGenerateGradleAndroidProject
{
public int callbackOrder { get { return 0; } }
public void OnPostGenerateGradleAndroidProject(string path)
{
Debug.Log("CustomGradleProcessor.OnPostGenerateGradleAndroidProject at path " + path);
// Dirty hack to get the path to the AndroidManifest.xml file
path = path.Replace("unityLibrary", "launcher\\src\\main\\AndroidManifest.xml");
// Read the AndroidManifest.xml file and replace the preferExternal value
string text = File.ReadAllText(path);
text = text.Replace("preferExternal", "auto");
// Write it all back
File.WriteAllText(path, text);
Debug.Log($"preferExternal replaced at {path}");
}
}
@ShayneRC
Copy link

Thanks for posting this! I got here from the Unity issue tracker and this is exactly what I needed without having to do another Unity upgrade.

@mgstauffer
Copy link

It works! You saved my bacon, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment