How to rename a package in Android Studio

- Andrés Cruz

ES En español

How to rename a package in Android Studio

Wanting to rename the package name of an Android project can be due to many factors, the main one being that a decision was made to give the application another name and, quite possibly, the application name is part of the package name.

However, this can cause problems if we do not do it properly and we could render the project useless in the process; performing the renaming the right way is very easy and consists of very few steps as we will see below.

Modern Approach: Jetpack Compose and Namespace

In modern development with Jetpack Compose, Android Studio has separated the concept of Namespace (where your generated resource classes like R.java reside) and the Application ID (the unique identifier in the Google Play Store).

If you are using a modern structure, the change is much cleaner. It is no longer strictly necessary for the physical folder structure to match the application ID, although it remains a good practice.

To change the name in a Compose project, simply update these two fields in your build.gradle.kts:

android { // The namespace controls where your R files and internal linking are generated
    namespace = "net.desarrollolibre.composeapp"

    defaultConfig {
        // El applicationId es lo que ve el usuario y la Play Store
        applicationId = "net.desarrollolibre.composeapp"
        versionCode = 1
        versionName = "1.0"
    }
}

When using Compose, remember that after changing the namespace, you must update all the imports of your R class in your Kotlin files if you were calling resources manually. Android Studio usually offers an automatic fix (Alt + Enter) to update these imports across the entire project at once.

Finally, always perform a Clean Project and Rebuild Project from the Build menu to ensure that no traces of the previous name remain in the compiler's cache.

Legacy Approach: XML and View-based Projects

Renaming the package name in Android Studio (Dolphin and higher versions)

To rename the package physically in the file tree, we go to our project panel. Right-click on the gear icon and deselect the Compact Middle Packages option (in previous versions called Compact Empty Middle Packages):

Compact Empty Middle Packages Option

Now we can change the name of the packages independently. If the package name is divided into several parts (example: com.example.app), we must rename each of these parts: right-click on the package folder, Refactor and then Rename.

Compact Empty Middle Packages option disabled

It is important to choose Rename package when the IDE asks to ensure that all references in the AndroidManifest.xml and Java/Kotlin classes are updated. Then we press the Do Refactor button.

Rename option

Once this is done, we go to the module-level build.gradle.kts (or build.gradle) file and update the applicationId:

// build.gradle.kts
android {
    defaultConfig {
        applicationId = "net.desarrollolibre.nueva.app"
        // ...
    }
}

Learn how to rename your Android project's package step by step. Updated guide for the traditional method (XML) and the modern approach in Jetpack Compose (Namespace vs. Application ID).

I agree to receive announcements of interest about this Blog.

Andrés Cruz

ES En español