Skip to content

Conversation

@timbotimbo
Copy link
Collaborator

This PR fixes 2 issues regarding the namespace definitions in UnityLibrary.

1 - duplicate namespace

The android exporter contains a section to add a namespace to the build.grade file in Unitylibrary.

// Add the namespace 'com.unity3d.player' to unityLibrary\build.gradle
// for compatibility with Gradle 8
FileInfo buildGradleFile = new FileInfo(Path.Combine(exportPath, "build.gradle"));
if(!buildGradleFile.Exists) {
ProjectExportHelpers.ShowErrorMessage($"Unexpected error: '{buildGradleFile.FullName} not found");
return;
}
string buildGradleContents = File.ReadAllText(buildGradleFile.FullName);
Regex regexAndroidBlock = new Regex(Regex.Escape("android {"));
buildGradleContents = regexAndroidBlock.Replace(buildGradleContents, "android {\n\tnamespace 'com.unity3d.player'", 1);
File.WriteAllText(buildGradleFile.FullName, buildGradleContents);
Debug.Log($"Added namespace 'com.unity3d.player' to {buildGradleFile.FullName} for Gradle 8 compatibility");

Exporting with Unity 2022.3.58 results in a duplicate namespace. But Unity 2022.3.21 does NOT.

android {
    namespace 'com.unity3d.player'
    namespace "com.unity3d.player"

Somewhere between 2022.3.31 and 2022.3.58 Unity updated to add their namespace, and this export doesn't check if it already exists.

2 - missing xrmanifest namespace

When you use Gradle 8 and a Unity version that does not trigger (1) , you get a namespace error for xrmanifest:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':unityLibrary:xrmanifest.androidlib'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file: C:\Users\HNK\Downloads\flutter_embed_unity-main\flutter_embed_unity\example\android\unityLibrary\xrmanifest.androidlib\build.gradle. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

     If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.


Solution

  • Add a check if the file already contains 'namespace'.
  • Use the same replace logic for unityLibrary/build.gradle and unityLibrary/xrmanifest.androidlib/build.gradle.

@timbotimbo timbotimbo changed the title Fix duplicate and missing namespace in UnityLibrary build.gradle. [Android] Fix duplicate and missing namespace in UnityLibrary build.gradle. Feb 23, 2025
@jamesncl
Copy link
Collaborator

Thank you for this PR, and for #37 - these a great improvements. Sorry it has taken me so long to realise you had opened them! I'm adding these to the next version in branch rel/1.2.7.

@jamesncl jamesncl merged commit 3968628 into learntoflutter:main May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants