アプリを最終的なアプリ パッケージ(APK)にビルドする際、Android ビルドツールは、アプリで生成された R クラス(アプリのリソースへのアクセスに使用される)の名前空間にこの名前空間を使用します。たとえば、上記のビルドファイルでは、R クラスは com.example.myapp.R に作成されます。
build.gradle.kts ファイルの namespace プロパティに設定する名前は、アクティビティと他のアプリコードを配置する、プロジェクトのベース パッケージ名に常に一致させる必要があります。他のサブパッケージをプロジェクトに含めることはできますが、それらのファイルでは namespace プロパティの名前空間を使用して R クラスをインポートする必要が生じます。
簡単なワークフローにするため、デフォルトのとおり、名前空間をアプリケーション ID と同じにしてください。
名前空間を変更する
ほとんどの場合は、デフォルトのとおり、名前空間とアプリケーション ID は同じにすることをおすすめします。ただし、コードを再編成する場合や、名前空間の競合を避ける必要がある場合には、いずれかの時点で名前空間を変更する必要があります。
このような場合、アプリケーション ID とは独立に(モジュールの build.gradle.kts ファイル内の namespace プロパティを更新して)名前空間を変更します。その前に、アプリケーション ID を明示的に定義して、名前空間を変更してもアプリケーション ID が変更されないようにしておいてください。名前空間がアプリケーション ID に与える影響については、アプリケーション ID を設定するをご覧ください。
namespace と Gradle の applicationId で名前が異なる場合は、ビルドの最後に、ビルドツールによってアプリケーション ID がアプリの最終マニフェスト ファイルにコピーされます。そのため、ビルド後に AndroidManifest.xml ファイルを検査すると、package 属性はアプリケーション ID に設定されています。マージ マニフェストの package 属性は、Google Play ストアと Android プラットフォームが実際にアプリを識別するために使用するものです。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Configure the app module\n\nThis page describes useful app settings in the module-level `build.gradle.kts`\nfile. In addition to giving an overview of important properties set in the\n`build.gradle.kts` file, learn how to:\n\n- Change the application ID for different build configurations.\n- Safely adjust the namespace independent of the application ID.\n\nSet the application ID\n----------------------\n\nEvery Android app has a unique application ID that looks like a Java\nor Kotlin package\nname, such as *com.example.myapp*. This ID uniquely identifies your app on the\ndevice and in the Google Play Store.\n| **Important:** Once you publish your app, you should never change the application ID. If you change the application ID, Google Play Store treats the upload as a completely different app. If you want to upload a new version of your app, you must use the same application ID and [signing certificate](/studio/publish/app-signing) as when originally published.\n\nYour application ID is defined by the `applicationId` property in your module's\n`build.gradle.kts` file, as shown here. Update the value of the `applicationId`\nby replacing \u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e with your\napp's ID: \n\n### Kotlin\n\n```kotlin\nandroid {\n defaultConfig {\n applicationId = \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n minSdk = 15\n targetSdk = 24\n versionCode = 1\n versionName = \"1.0\"\n }\n ...\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n defaultConfig {\n applicationId \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n minSdkVersion 15\n targetSdkVersion 24\n versionCode 1\n versionName \"1.0\"\n }\n ...\n}\n```\n\nAlthough the application ID looks like a traditional Kotlin or Java package\nname, the naming rules for the application ID\nare a bit more restrictive:\n\n- It must have at least two segments (one or more dots).\n- Each segment must start with a letter.\n- All characters must be alphanumeric or an underscore \\[a-zA-Z0-9_\\].\n\nWhen you [create a new project in Android\nStudio](/training/basics/firstapp/creating-project), the `applicationId` is\nautomatically assigned the package name you chose during setup. You\ncan technically toggle the two properties independently from then on, but it\nis not recommended.\n\nIt is recommended that you do the following when setting the application ID:\n\n- Keep the application ID the same as the namespace. The distinction between the two properties can be a bit confusing, but if you keep them the same, you have nothing to worry about.\n- Don't change the application ID after you publish your app. If you change it, Google Play Store treats the subsequent upload as a new app.\n- Explicitly define the application ID. If the application ID is not explicitly defined using the `applicationId` property, it automatically takes on the same value as the namespace. This means that changing the namespace changes the application ID, which is usually not what you want.\n\n| **Note:** The application ID used to be directly tied to your code's package name, so some Android APIs use the term \"package name\" in their method names and parameter names. This is actually your application ID. For example, the [`Context.getPackageName()`](/reference/android/content/Context#getPackageName()) method returns your application ID. There's never a need to share your code's true package name outside your app code.\n| **Caution:** If you are using [`WebView`](/reference/android/webkit/WebView), consider using your package name as a prefix in your application ID. Otherwise, you might encounter problems as described in [Issue #37026699](https://issuetracker.google.com/issues/37026699).\n\n### Change the application ID for testing\n\nBy default, the build tools apply an application ID to your\n[instrumentation test](/training/testing/unit-testing/instrumented-unit-tests)\nAPK using the application ID for the given build variant appended with\n`.test`. For example, a test APK for the\n\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e`.free` build variant\nhas the application ID\n\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e`.free.test`.\n\nAlthough it shouldn't be necessary, you can change the application ID by\ndefining the `testApplicationId` property in your `defaultConfig` or\n`productFlavor` block.\n\nSet the namespace\n-----------------\n\nEvery Android module has a namespace, which is used as the Kotlin or Java\npackage name for\nits generated [`R`](/reference/android/R) and `BuildConfig` classes.\n\nYour namespace is defined by the `namespace` property in your module's\n`build.gradle.kts` file, as shown in the following code snippet. The `namespace`\nis initially set to the package name you choose when you\n[create your project](/training/basics/firstapp/creating-project). \n\n### Kotlin\n\n```kotlin\nandroid {\n namespace = \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n ...\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n namespace \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n ...\n}\n```\n\nWhile building your app into the final application package (APK), the Android\nbuild tools use the namespace as the namespace for your app's generated `R`\nclass, which is used to access your\n[app resources](/guide/topics/resources/overview).\nFor example, in the preceding build file, the `R` class is created at\n\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e`.R`.\n\nThe name you set for the `build.gradle.kts` file's `namespace` property\nshould always match your project's base package name, where you keep your\nactivities and other app code. You can have other sub-packages in\nyour project, but those files must import the `R` class using the\nnamespace from the `namespace` property.\n\nFor a simpler workflow, keep your namespace the same as your\napplication ID, as they are by default.\n| **Caution:** While the `namespace` property represents your app's Java or Kotlin package name, once the APK is compiled, the `package` attribute in the merged manifest file represents your app's universally unique [application ID](#set-application-id).\n\n### Change the namespace\n\nIn most cases, you should keep the namespace and application ID the\nsame, as they are by default. However, you may need to change the namespace at\nsome point if you're reorganizing your code or to avoid namespace collisions.\n\nIn these cases, change the namespace by updating the\n`namespace` property in your module's `build.gradle.kts` file independent of the\napplication ID. Before you do so, make sure that your application ID is\nexplicitly defined, so that changing the namespace doesn't likewise change the\napplication ID. For more information on how the namespace can affect the\napplication ID, see [Set the application ID](#set-application-id).\n\nIf you have different names for the `namespace`\nand the Gradle `applicationId`, the build tools copy\nthe application ID into your app's final manifest file at the end of the build.\nSo if you inspect your `AndroidManifest.xml` file after a build,\nthe `package` attribute is set to the\napplication ID. The merged manifest's `package` attribute is where the\nGoogle Play Store and the Android platform actually look to identify your app.\n\n### Change the namespace for testing\n\nThe default namespace for the `androidTest` and `test` source sets is the main\nnamespace, with `.test` added at the end. For example, if the\n`namespace` property in the `build.gradle` file is\n\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e,\nthe testing namespace is by default set to\n\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e`.test`.\nTo change the namespace for testing, use the\n[`testNamespace`](/reference/tools/gradle-api/7.1/com/android/build/api/dsl/TestedExtension#testNamespace:kotlin.String)\nproperty, as shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nandroid {\n namespace = \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n testNamespace = \"\u003cvar label=\"TEST_APP_ID\" translate=\"no\"\u003ecom.example.mytestapp\u003c/var\u003e\"\n ...\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n namespace \"\u003cvar label=\"APP_ID\" translate=\"no\"\u003ecom.example.myapp\u003c/var\u003e\"\n testNamespace \"\u003cvar label=\"TEST_APP_ID\" translate=\"no\"\u003ecom.example.mytestapp\u003c/var\u003e\"\n ...\n}\n```\n\n**Caution:** Don't set `testNamespace` and\n`namespace` to the same value, otherwise namespace\ncollisions occur.\n\nTo learn more about testing, see [Test apps on Android](/training/testing)."]]