PC パソコン

Causes and Solutions for Android Studio “Namespace not specified”

When you open a project in Android Studio or run Gradle Sync or a build,
an error called “Namespace not specified” may be displayed.

This error occurs when the Android Gradle Plugin cannot determine the
namespace of an app or library.
It may occur especially when the Android Gradle Plugin is updated to a newer version
or when an old Android project is opened in a newer version of Android Studio.

This article explains the main causes and solutions for the “Namespace not specified” error in Android Studio.

What Is “Namespace not specified”?

“Namespace not specified” is an error displayed when the required
namespace is not configured for an Android module.

For example, an error like the following may be displayed.

Namespace not specified. Specify a namespace in the module's build file.

In this case, check whether namespace is configured in
build.gradle or build.gradle.kts
for the app or library module.

Main Causes

The main causes of “Namespace not specified” in Android Studio include the following.

  • namespace is not configured
  • An old Android project is being opened with a newer Android Gradle Plugin
  • The Android Gradle Plugin has been updated
  • A library module does not have a namespace
  • Only some modules in a multi-module project do not have a namespace configured
  • An external library or local library is outdated
  • The wrong build.gradle or build.gradle.kts file is being edited
  • Groovy DSL and Kotlin DSL syntax are being confused

Point:
If “Namespace not specified” is displayed,
first check the module shown in the error
and confirm whether namespace is configured
inside the android block of that module.

Configure the namespace

The basic solution is to add
namespace
to the Gradle configuration file of the affected module.

In Kotlin DSL
build.gradle.kts,
for example, configure it as follows.

android {
    namespace = "com.example.myapplication"
}

In Groovy DSL
build.gradle,
for example, configure it as follows.

android {
    namespace 'com.example.myapplication'
}

Check the package name used by the project and configure an appropriate namespace.

Check Which build.gradle File to Configure

An Android project may contain a Gradle configuration file for the entire project
as well as Gradle configuration files for individual modules such as app.

namespace is normally configured in the
android block of
build.gradle or build.gradle.kts
on the Android module side.

For example, if the error occurs in the app module,
check a file such as the following.

app/build.gradle

Or,

app/build.gradle.kts

Even if you modify only the Gradle file in the project root,
the error may not be resolved if namespace is not configured in the affected module.

Check the package in AndroidManifest.xml

In older Android projects,
the package attribute in
AndroidManifest.xml
may have been used as the namespace.

For example, it may be written as follows.

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

With newer Android Gradle Plugin versions,
it may be necessary to explicitly configure
namespace
in the module’s Gradle settings.

In that case,
use the package in AndroidManifest.xml as a reference
and add namespace to the Gradle file.

Check the Difference from applicationId

namespace and
applicationId
are similar, but they do not have the same role.

For example, there may be a configuration like the following.

android {
    namespace = "com.example.myapplication"

    defaultConfig {
        applicationId = "com.example.myapplication"
    }
}

namespace is mainly used as the namespace for generated R classes,
BuildConfig, and similar items.

On the other hand,
applicationId
is used as the ID for identifying an Android app.

Although the same string may be used for both,
simply configuring applicationId
may not be treated as configuring namespace.

When Opening an Old Project in a Newer Android Studio

Projects created with an older Android Studio or older Android Gradle Plugin
may not have namespace configured.

If you open such a project in a newer Android Studio
and also update the Android Gradle Plugin to a newer version,
“Namespace not specified” may occur.

In this case,
check the Gradle settings for each Android module
and add the required namespace.

When the Android Gradle Plugin Has Been Updated

Updating the Android Gradle Plugin may cause
“Namespace not specified” to appear in a project
that previously built without problems.

Even if an older configuration worked using only the package in
AndroidManifest.xml,
a newer Android Gradle Plugin may require namespace on the Gradle side.

If the error occurs after updating the Android Gradle Plugin,
check each module’s
build.gradle
or
build.gradle.kts.

Check Library Modules

Even if namespace is configured in the app module,
an error may occur if namespace is not configured
in a library module within the project.

For example, this may apply if the following modules exist.

app
library
common

If library or common is an Android library module,
namespace may also be required in each module’s Gradle configuration file.

For example, configure it as follows.

android {
    namespace = "com.example.library"
}

Check the Module Shown in the Error

When “Namespace not specified” is displayed,
the messages before or after the error may indicate
which module has the problem.

The cause may not be the app module,
but another library module or external module.

First, check the entire error message
and identify the module that is missing namespace.

Check Multi-Module Projects

In a project containing multiple Android modules,
all Android modules need to be checked.

Even if you correct only one module,
Gradle Sync or the build may continue to fail
if namespace is not configured in another module.

Check the android block
in the Gradle configuration file of each module.

If an External Library Is the Cause

If an external library or plugin you are using is outdated,
namespace may not be configured on the library side,
causing an error with a newer Android Gradle Plugin.

In this case,
simply adding namespace to your own app module may not resolve the problem.

Check whether a newer version of the library is available
and whether you can update to a version compatible with the newer Android Gradle Plugin.

Check Local Libraries

If you are using an old Android library obtained from Git or another source
as a module within the project,
namespace may also be required in that library’s Gradle configuration.

Check the library module’s
build.gradle
or
build.gradle.kts,
and configure namespace inside the
android block.

Check the Difference Between Groovy DSL and Kotlin DSL

The way namespace is written differs depending on the DSL
used in the Gradle configuration file.

In Kotlin DSL, it is written as follows.

android {
    namespace = "com.example.myapplication"
}

In Groovy DSL, it may be written as follows.

android {
    namespace 'com.example.myapplication'
}

If you copied sample code from the web,
check whether your project uses
build.gradle
or
build.gradle.kts.

Check Where namespace Is Written

namespace is normally written inside the
android block
of the Gradle configuration file.

android {
    namespace = "com.example.myapplication"

    compileSdk = 35
}

If it is written in an incorrect location,
such as outside the android block,
it may not be recognized correctly.

Check the namespace Value

For namespace,
specify a format that can be used as a Java or Kotlin package name.

For example, configure it as follows.

com.example.myapplication

In an existing project,
checking the package declaration in the Java or Kotlin source code
or the package in the conventional
AndroidManifest.xml
makes it easier to determine the namespace that should be configured.

Check the package in Java or Kotlin

If you do not know the namespace value,
check the package written in the Java or Kotlin source files.

For example, it may be written as follows.

package com.example.myapplication

Use this kind of existing package structure as a reference
when configuring namespace.

Run Gradle Sync Again

After adding or correcting namespace,
run Gradle Sync again.

Run
Sync Project with Gradle Files
or a similar option from Android Studio.

If namespace is configured correctly,
Gradle Sync may complete successfully.

Run Clean Project

If old build information remains even after correcting namespace,
running Clean Project may improve the situation.

Run
Clean Project
from the Build menu or another location in Android Studio,
and then run Gradle Sync or Rebuild Project as necessary.

Restart Android Studio

If the error display does not change even after correcting the Gradle settings,
restart Android Studio and run Gradle Sync again.

Due to a temporary state on the IDE side,
the changes may not be reflected immediately.

What to Check for Each Situation

Depending on the situation in which “Namespace not specified” is displayed,
you can narrow down what should be checked to some extent.

Situation Main Points to Check
Occurs in the app module namespace in app/build.gradle or app/build.gradle.kts
Occurred after updating AGP namespace in each Android module
Occurs in an old project package in AndroidManifest.xml, namespace on the Gradle side
Occurs in a library module Library-side build.gradle, namespace
Occurs in an external library Library update, AGP compatibility
Still fails after adding namespace Module where it was added, location where it was written, DSL

Steps to Check If the Problem Is Not Resolved

If you do not know the cause,
checking the following in order makes it easier to narrow down the problem.

  1. Check the full “Namespace not specified” error message
  2. Check which module is producing the error
  3. Check the affected module’s build.gradle or build.gradle.kts
  4. Check whether namespace exists inside the android block
  5. Check whether the namespace value is correct
  6. For an old project, check the package in AndroidManifest.xml
  7. Check whether only applicationId is configured
  8. For a multi-module project, check all Android modules
  9. Check whether library modules also have namespace
  10. Check whether the Groovy DSL or Kotlin DSL syntax is correct
  11. If an external library is the cause, check whether an updated version is available
  12. Run Gradle Sync again
  13. Try Clean Project or restarting Android Studio as necessary

Important:
“Namespace not specified” basically indicates
that namespace is not configured
in the Gradle settings of the affected Android module.
Because the cause may be not only the app module,
but also a library module or an old external library,
it is important to check the module shown in the error.

Summary

Android Studio’s
“Namespace not specified”
is an error displayed when the Android Gradle Plugin
cannot determine the namespace of the affected module.

It may occur especially when an old Android project is opened in a newer Android Studio
or when the Android Gradle Plugin is updated.

First, check the module where the error occurs,
and confirm that an appropriate
namespace
is configured inside the android block
of that module’s
build.gradle
or
build.gradle.kts.

If correcting the app module does not resolve the problem,
also check library modules, external libraries,
and the Android Gradle Plugin update status
to efficiently narrow down the cause.