How do you set a namespace in build.gradle?

I am trying to build my mobile game, but I keep getting an error that says gradle build failed. When I click on the error and scroll I see the following info:

A problem occurred configuring project ‘:launcher’.

Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
> Namespace not specified. Specify a namespace in the module’s build file. See App-Modul konfigurieren  |  Android Studio  |  Android Developers 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 Use the Android Gradle plugin Upgrade Assistant  |  Android Studio  |  Android Developers for general information about using the AGP Upgrade Assistant.

I have the build.gradle file open in VS, how do I set a namespace?

You specify this by either having package attribute in the manifest, or by setting namespace inside android block

android {
namespace 'com.example.m'
...
}

I put namespace “com.unity3d.player” at the top of the android block, but it is still giving me the same error. Is there anything i have to change in the manifest file?

What is the name of the namespace we should specified? I don’t understand the purpose of it. And we can’t just spacified the same name as our package name

1 Like

Im facing the same issue, did you find a solution?

Unity has two build.gradle files, you can modify them using Custom Gradle Templates.

  • launcherTemplate.gradle, has your application ID:
android {
    namespace 'com.yourCompany.your_app_name'
    ...
}
  • mainTemplate.gradle, has the unity player ID:
android {
    namespace 'com.unity3d.player'
    ...
}

Newer versions of Unity already have the corresponding namespace in the .gradle files, you don’t have to add them manually.

1 Like

I want to add that recently when I would upgrade from older project to new version of unity, I would delete all gradle and xml related to android build and then re-enabled the player setting option to make unity regenerate new one. Sometimes it add namespace automatically and the build would mostly success normally