Rename package in Android Studio

Questions : Rename package in Android Studio

How do you rename packages in the new IDE Android Studio, based on IntelliJ IDEA?

Is there an automatic refactoring included?

I want to make bulk refactoring, but I don’t know how. I worked two years with Eclipse and in Eclipse it’s a one-click operation.

Total Answers: 53 Answers 53


Popular Answers:

  1. In Android Studio, you can do this:

    For example, if you want to change com.example.app to my.awesome.game, then:

    1. In your Project panel, click on the little gear icon ( Gears icon )

    2. Uncheck the Compact Empty Middle Packages option

      Compact Empty Middle Packages

    3. Your package directory will now be broken down into individual directories

    4. Individually select each directory you want to rename, and:

    • Right-click on it

    • Select Refactor

    • Click on Rename

    • In the pop-up dialog, click on Rename Package instead of Rename Directory

    • Enter the new name and hit Refactor

    • Click Do Refactor in the bottom

    • Allow a minute to let Android Studio update all changes

    • Note: When renaming com in Android Studio, it might give a warning. In such case, select Rename All

      Enter image description here

    1. Now open your Gradle Build File (build.gradle – Usually app or mobile). Update the applicationId in the defaultConfig to your new Package Name and Sync Gradle, if it hasn’t already been updated automatically:

      Refactor Directories

    2. You may need to change the package= attribute in your manifest.

    3. Clean and Rebuild.

      Clean and Rebuild

    4. Done! Anyway, Android Studio needs to make this process a little simpler.

  2. Another good method is: First create a new package with the desired name by right clicking on the Java folder → NewPackage.

    Then, select and drag all your classes to the new package. Android Studio will refactor the package name everywhere.

    Finally, delete the old package.

    Done.

    Very important:

    You have to manually change AndroidManifest.xml and build.gradle file to the new package if you use this method.

  3. Changing the application ID (which is now independent of the package name) can be done very easily in one step. You don’t have to touch AndroidManifest. Instead do the following:

    1. right click on the root folder of your project.
    2. Click “Open Module Setting”.
    3. Go to the Flavours tab.
    4. Change the applicationID to whatever package name you want. Press OK.

    Note this will not change the package name. The decoupling of Package Name and Application ID is explained here: http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename

  4. The approach used by me for renaming the package name is as follows:-

    Step 1 : Select the Project option from the left menu of Android Studio

    enter image description here

    Step 2 : Right-click on java and add a new package and set the desired package name

    enter image description here

    Step 3 : Enter your new package name

    enter image description here

    Step 4 :Copy all the files from your old package and paste into the new package

    enter image description here

    Step 5 :Rename the package name in the manifest file

    enter image description here

    Step 6 :Rename the package name in build.gradle file

    enter image description here

    Step 7 :Then right-click the old package and delete it with all its data, and delete that directory as well

    enter image description here

    Step 8 :Then Rebuild your project

    enter image description here

    Step 9 :Then you will find some errors of old import packagename in your project Select the old package name in any file and press CTRL + Shift + R , and enter your new package name in replace box, then press find

    enter image description here

    Step 10 :Then a popup appears like below and select the All files option from it

    enter image description here

    Step 11 :Rebuild your project again, bingo your project packagename has been changed 🙂

    1. Go to your AndroidManifest.xml file.

    2. Place your cursor in the package name as shown below. Don’t select it, just place it.

      Enter image description here

    3. Then press Shift + F6 and you will get a popup window as shown below. Select Rename package.

      Enter image description here

    4. Enter your new name and select Refactor. (Note: since my cursor is on “something”, only something is renamed.)

    That’s it done.

  5. Right-click on the package at the Project Panel.

    Choose Refactor -> Rename from the context menu.

  6. Change the Package Name


    To rename your package name, all you have to do is go to your AndroidManifest.xml file and put your mouse cursor in front of the part of the package name you want to change.


    enter image description here


    Right-Click > Refactor > Rename


    enter image description here


    In the new window press Rename package


    enter image description here


    Change name and press Refactor


    enter image description here


    …and press Do Refactor at the bottom.


    enter image description here


    Your package name usually is in format com.domain.appname; in this example we changed the appname part, but you can do the same steps for the domain too.

    Done! You have changed your package name!

    1. Open the file:

      app → manifests → AndroidManifest.xml

      Enter image description here

      Highlight each part in the package name that you want to modify (don’t highlight the entire package name) then:

      • Mouse right-click → Refactor → Rename → Rename package
      • type the new name and press (Refactor)

      Do these steps in each part of the package name.

      Enter image description here

    2. Open (Gradle Script) >> (build.gradle(Modul:app))

      and update the applicationId to your package name

      Enter image description here

    3. Open the menu (build) and choose (Rebuild Project).

  7. Android Studio 2022


    Step by Step Implementation

    Step 1: To rename package name in Android studio open your project in Android mode first as shown in the below image.

    Step 2: Now click on the setting gear icon and deselect Compact Middle Packages.

    Step 3: Now the packages folder is broken into parts as shown in the below image.

    Step 4: Now right-click on the first package name (com) and Refactor > Rename. A warning message will be displayed but go ahead and click on the Rename current button.

    Step 5: Rename the directory name as your requirement and click on the Refactor button.

    Note: Go to Build > Rebuild Project to display the updated name.

    Now you can see your directory name changes from com -> gfg as shown in the below image.

    Step 6: Do the same for the domain extension and App folder name according to your requirement.

    Now you can see the package name has been changed from com.example.pulltorefreshwithlistview to gfg.geeksforgeeks.listview as shown in the below image.

    Step 7: Now go to the build.gradle (Module: app) in Gradle Scripts. Here change the applicationId and click on Sync Now. And you are successfully renamed your package name.


    Source: How to Rename Package Name in Android Studio?

  8. I have seen the top voted answers but i found is a little bit different to do this, i try to do the most complete tutorial.

    From the Android Studio click over the gear icon ( Gears icon ) and then select the option: "Compact Empty Middle Packages", to see the folders separated in a tree view.

    introducir la descripción de la imagen aquí

    Now select the folder, click right button to open the contextual menu, select Refactor and then Rename

    introducir la descripción de la imagen aquí

    You will be advised to refactor the package:

    introducir la descripción de la imagen aquí

    Then a window will show the coincidences inside the proyect, select "Do Refactor":

    introducir la descripción de la imagen aquí

    We don´t have to change manually the AndroidManifest.xml or build.gradle files, Refactoring the package will do the job!.

  9. IntelliJ IDEA has an option called “Compact Empty Middle Packages”. Select the option icon of the Project tab and de/activate this.

    See: How can I change top level package name in IntelliJ IDEA?

  10. Select the package that will be refactored. RefactorMove“Move xxx to new package”.

  11. If your package name is more than two dot separated, say com.hello.world and moreover, you did not put anything in com/ and com/hello/. All of your classes are putting into com/hello/world/, you might DO the following steps to refactoring your package name(s) in Android Studio or IntelliJ:

    • [FIRST] Add something under your directories(com/, com/hello/). You can achieve this by first add two files to package com.hello.world, say
     com.hello.world.PackageInfo1.java com.hello.world.PackageInfo2.java 

    then refactor them by moving them to com and com.hello respectively. You will see com and com.hello sitting there at the Project(Alt+1 or Command+1 for shortcut) and rename directories refactoring is waiting there as you expected.

    • Refactor to rename one or more of these directories to reach your aim. The only thing you should notice here is you must choose the directories rather than Packages when a dialog ask you.

    • If you’ve got lots of classes in your project, it will take you a while to wait for its auto-scan-and-rename.

    • Besides, you need to rename the package name inside the AndroidManifest.xml manually, I guess, such that other names in this file can benefit the prefix.

    • [ALSO], it might need you to replace all com.hello.world.R to the new XXX.XXX.XXX.R(Command+Shift+R for short)

    • Rebuild and run your project to see whether it work. And use “Find in Path” to find other non-touch names you’d like to rename.

    • Enjoy it.
  12. Quick and easy way in 3 steps:

    1- open MainActivity or any other java or Kotlin file.

    At the top there is the package declaration such as:

    package com.example.myapp;

    select the package portion that you want to change and press Shift+F6. I personally want to change the example.

    In the warning dialog, select Rename package and then insert the desired package name.

    2- Open AndroidManifest.xml and inside <manifest> tag change the package to the desired package name.

    3- open build.gradle(Module: app) and change the applicationId to the desired package name.

  13. Be sure to go to Sheharyar’s great Answer. There are so many answers and comments that follow it that it would be easy to get confused and give up, but don’t. That answer works.

    In short, you do three things:

    (1) Deselect Compact Empty Middle Packages.

    (2) Refactor then Rename each old directory node by choosing Change Package (not Change Directory) to match the new package name. (Be sure to do a preview of changes.)

    (3) Edit the build.gradle file and make APPLICATION_ID match the new package name.

  14. I’ve found a way easier solution to this problem which also changed the generated imports like com.test.testpackagechange.R and only takes about a minute.

    Your first step is to open Android Studio and open the replace all window (Mac: cmd + shift + R, Windows I assume: ctrl + shift + R). Type in your old package name and in the one below your new package name. Click Find. This may take a while because it is also looking through the generated items. If it has over 1000 hits, just click continue.

    enter image description here

    After you’ve done that push Replace All to replace your old package name with your new one.

    enter image description here

    Now close Android Studio and go to Finder on Mac or Windows Explorer on Windows. Change the name of the folders to your new package name, like this:

    enter image description here

    Now open Android Studio again. Gradle will sync and your package name should be changed to the new one.

    I’ve found this to be the easiest one and the one that covers all areas like generated files.

    1. Select the package name in the Java folder.
    2. Shift+F6
    3. Change the package name and click OK.

    Your package name will be changed from all the Java files and the manifest file. You have to manually change the package name from build.gradle.

  15. Updated answer: May 2015
  16. choose File -> Project Structure -> Choose your app's module -> Click on the Flavors tab -> change the Application id.
  17. I found another way that works or an extra step to some of the answers here especially if you want to change the domain as well. It works in Android Studio 1.4. This is what I did:

    1. Open Manifest.xml and change the package name to what you want.
    2. Open your app build.gradle file and change the Application Id in defaultConfig to the same name as in manifest and rebuild the project.
    3. If still an issue, open a file under the package name, go to the package breadcrumbs (i.e. package declaration at head of file) and set your cursor to the domain you want to change and hit “Shift + F6”, it would come out with a dialog with multiple use warnings, click on “Rename packages” and then click on “Do Refactor” it should rename everything including the R.Java files.

    So for example if you want to rename “com.example.app” to “com.YourDomain.app”, open a file under the package to be renamed, in the package breadcrumbs, set your cursor to “example” part of the domain and hit Shift + F6 and rename package to “YourDomain”.

  18. I tried the two top-voted solutions but found some issues even though both work to some extent.

    • List item: The new package-drag-drop method leaves some unchanged and creates some undesired effects
    • List item: The rename package only changes the last part of the package name

    After some experiments, I found the following method works well for me.

    If you just need to change the last part of the package name, use the method outlined by GreyBeardedGeek, namely

    Right-click on the package in the Project pane. Choose Refactor -> Rename from the context menu

    If you need to change the whole package name, do the following.

    Right-click on the package in the Project pane. Choose Refactor -> Move from the context menu

    This will create a new package folder (when necessary) but will keep the last part of your package name as before. If you need to change the last part, do the rename accordingly.

    Note also that you may need to modify package names in e.g. build.gradle, manifest, and/or any xml resource files, or even in your code if hardcoded. After all that, do Sync/Clean/Rebuild project as necessary.

  19. How to rename com.example.app to com.android.app:

    1. in package com.example.app select example

    2. Shift + F6

    3. choose rename package

    4. rename example to android

    5. confirm do refactor

  20. The best way is to write the new package name and drag from the older package name.

    The second way, if you click Refactor then move option then rename the package name, it will rename the package name and then rebuild.

    In Build.gradle you have to do manually, if you Refactor then it will not rename in Build.gradle.

  21. Right click on package -> refactor and change the name.

    You can also change it in the manifest. Sometimes if you change the package name, but after creating the .apk file it shows a different package name. At that time check “applicationId” in the build.gradle file.

    • The first part consists of creating a new package under the java folder and selecting then dragging all your source files from the old package to this new package. After that, you need to rename the package name in android manifest` to the name of the new package.

    • In step 2, here is what you need to do. You need to change the old package name in applicationId under the module build.gradle in your android studio in addition to changing the package name in the manifest. So in summary, click on build.gradle which is below the “AndroidManifest.xml” and modify the value of applicationId to your new package name.

    • Then, at the very top, under build. clean your project, then rebuild. It should be fine from here.

  22. I recommend to use Sublime Text (or Notepad++). Replace com.one.lastname -> com.two.newname and com/one/lastname -> com/two/newname in …Projects[MyProject]. And don’t forget to rename …ProjectsMyProjectappsrcmainjavacomonelastname, …ProjectsMyProjectappsrctestjavacomonelastname and …ProjectsMyProjectappsrcandroidTestjavacomonelastname!

    That’s all:)

    Screenshot

  23. There are many answers given, but still, I am giving my attempt.

    Step 1 : As Show in the fig above select the Setting Option… enter image description here

    Step 2: Select Compact Middle Packages option… enter image description here

    Step 3: Now Expand the package as shown enter image description here

    Step 4: After expanding it will look something like enter image description here

    Step 5: Select any on the subpackage (in or example or googledevsmsverify) and click Shift+f6 button… enter image description here

    I selected middle package example so it will display as above fig simple rename the package and click on the refactor button

    enter image description here

    Step 6: you will see the following screen after clicking Refactor button, click on the DO Refactor, and wait until build the gradle… enter image description here

    Step 7: Goto build.gradle(Mobile:app) and change the package name of applicationID as shown enter image description here

    Step 8: Just check the package in the manifest.xml file…

    enter image description here

    All Done the package is change…@Ambilpura

  24. This I believe what you are looking for is Refactor > Move. You can also press F6.

    You will get two popups. Make sure you select rename package on both. After that you will get a pop up to place the new package name.

    Please note that if your package name is, for example, com.example.android.projectname then this will let you change com.example.android to something else.

    One more thing, this will update the applicationId in your build.gradle as well. Just make sure that the checkboxes for “search in comments and strings” and “search for text occurrences” are both checked for it to work.

    1. Select option enter image description here

    2. Uncheck the Compact Empty Middle Packages option.

    3. Select which directory you want to change(I have selected ‘crl’ shown in step 6).
    4. Shift+F6
    5. Rename Package
    6. Renaming directory crl to crl1 enter image description here

    7. Finally click on Do Refactor button marked in image below enter image description here enter code here

    8. After Changes done enter image description here

    1. Press Ctrl + Shift + R

    2. Replace the old package with the new one.

    3. Right click on the package name.

    4. Refactor > Rename and rename to the new one

  25. ctrl + Shift + R has always worked for me. Simply replace all and choose All Files.

  26. Please try the following steps:

    1. Click on the setting gear icon and deselect Compact Empty Middle Package
    2. Now we can see each package folder is broken into parts
    3. Now right click on the first package folder >>> refactor >>> rename
    4. Now a warning will be displayed but you go ahead and click Rename Package
    5. After that enter your domain name for the package name
    6. Click on ‘Do Refactor’
    7. Now it has changed the package domain name of the App. Now Change the domain extension and App folder name according to your requirement
    8. Now open build.gradle (Module: app) in Gradle Scripts. Here change the application id and click Sync Now.

    *******Finally it’s done ******

  27. enter image description here

    3 Methods to change package name in Android Studio

    I think this is a new one. I tried it and it’s working. If the above doesn’t work for you. Try this. On the site I mentioned above, there are 2 other methods too with image tutorials. I also checked.

    First I am going to talk about changing the Package name. In this example, We will change the package name “com.androidride.myapplication” to “info.xyz.yourapplication”.

    1. Right click on the package name you want to change and select Refactor – > Move.
    2. A new dialog appears, Choose Move package “com.androidride.myapplication” to another package and click on Ok.
    3. A warning dialog appears, click on yes.
    4. Enter new package name in the new dialog. But only “info.xyz” leave yourapplication.
    5. Now you will see a warning dialog says Package info.xyz does not exist. Do you want to create it? click on yes.
    6. Click on “Do Refactor”. Now your package name changed to info.xyz.myapplication.
    7. Right click on info.xyz.myapplication and Refactor -> Rename.
    8. Click on Rename package.
    9. Rename dialog: change myapplication to yourapplication.
    10. Click on Do refactor.
    11. Delete old package name directories and change application id in build.gradle, change it into info.xyz.yourapplication.
    12. That’s all.
  28. Right Click on the Package name -> Refractor ->Rename -> enter the name of the package ->Click “Refractor”. Do not edit any of the build files except “build.gradle” under the “app” folder.

    From Renaming android studio project – Vishnu Sivan

    • Go to project path > where you located Java files and package
    • Create new folders, for example com → super_package → activities
    • Android Studio will refresh project structure
    • Move all Java files to new folders
    • Remove old folders
    • Menu EditFind → Replace in path:
    • Change old package name to new
    • Change also in manifest and build gradle

    Done!

  29. The question asked:

    Is there an automatic refactoring included?

    I want to make bulk refactoring, but I don’t know how. I worked two years with Eclipse and in Eclipse it’s a one-click operation.

    Well, almost one-click with automatic refactoring, but just one small step first, i.e., just the small step to create the new package. Here is the full solution below.

    You can rename a package by creating a new package (including the required folder path) and then drag-and-drop over from the old package (and let Android Studio take care of the refactoring of the various references from the old package to the new), as follows:

    1. Right click on java under app. Right click on java under app
    2. Select New and Package enter image description here
    3. Select the location for the new package as ../app/src/main/java enter image description here
    4. Enter the full name of the new package enter image description here
    5. The new package will be created. Now, drag-and-drop over from the old package to the new. Preview the refactoring if you’d like to check that Android Studio is doing it properly. If it all looks good, proceed with the refactoring.

    NB: since application ID and package name are decoupled these days, note that the above is only for renaming the package name. For changing the application ID, you can do that in your build.gradle.

  30. Go to AndroidManifest.xml There you will find the package as the attribute of the manifest tag. Just select the part you want to change, right-click-> refactor -> rename

    Maybe earlier it was hard, but as of today, it is not anymore.

    You might also want to change the app id in

    defaultConfig { applicationId } 

    Short & Sweet

  31. That works very well for me, even for changing the occurrences, in all the modules concerned, for the folders names I want to modify in the package but, for me, it works fine only if I follow exactly the following steps :

    • A – If you want to change the name of the root folder (in other words the folder’s name who is normaly used in the package to contain the principal activity), you can do it before or after the following steps (B to C) by exiting from “Android Studio” and rename manually this root folder. Click after on “Import project…” and then on the new name of your project (this import process will automatically readjust the ‘Gradle’ files with the project files.)
    • B – before the following C step, don’t forget to modify manually the folder’s names you want to change in the package by changing them in the 3 following files: ‘AndroidManifest.xml’, ‘MainActivity.java’ (or the name you eventually choose for your first activity) and the ‘build.gradle (Module: app)’ file.
    • C – Follow the steps 1 to 6 described above (“In your Project pane, click on the little gear icon…”) -> https://stackoverflow.com/a/29092698/2001610
    • D – Only if you want to change the name of the root folder now, you can follow the ‘A’ process, as described above.
  32. After you follow one of these techniques to get your package renamed, you might start seeing errors.

    If / when your R.java is not getting generated properly, you’ll get a lot of errors in your project saying error: cannot find symbol class R and also error: package R does not exist.

    Remember to check your Application manifest xml. The manifest package name must match the actual package name. It seems the R.java is generated from the Application Manifest and can cause these errors if there’s a mismatch.

    Remember to check the package attribute matches in your <manifest package="com.yourcompany.coolapp">

  33. I needed to run File->Invalidate Caches/Restart… to remove remnants of the original package

  34. To rename the package name in Android studio, Click on the setting icon in the project section and untick the Compact empty Middle Packages, after that the package will split into multiple folder names, then right-click on the folder you need to change the name, click on refactor-> Rename-> Type the name you want to change in -> Refactor -> Refactor Directory, then import R.java file in the whole project. Working for me.

    • First, you need to go to the settings icon which is Appear in left. Click on that dropdown and uncheck Compact Empty Middle Packages

    Go to Your AndroidManifest.xml

    1. Select All of your Package and then Right click there
    2. Refactor and then rename that how you want.
    3. and then go to your build.gradle and then change Applicationid with packagename that what you have in Android Manifest.

    It is worked For Me

  35. for example if you want change package from com.example.a to com.example.b

    1)change project style folder from android to package (in project side)

    2)right click top of folder and click replace in path

    3)select in project button

    4)in first input write com.example.a

    5)in second input write com.example.b

    6)when you see warning select replace all to replace value

    after finish replace you can see your package name is com.example.b

  36. There is also a good keyboard shortcut key just click n select the Class / Package which you want to rename and press ALT + two times R or Shift+ F6

    which reflect :-> Refactor menu and Rename function in Android Studio. After rename the existing package it shows in bottom side where the effect of this reflection / rename if you click on Refector button it will change the Class name / Package name where is used in whole Project.

  37. """ Notes: 1 - This script it for python3 2 - Create a backup first 3 - Place on the root folder of the android project you want to change 4 - run `python change_android_studio_pkg_name.py` 5 - Type the old package name 6 - Type the new package name 7 - Clean and Rebuild on Android Studio 8 - SRC folder names won't be changed but app will work """ import os, glob, traceback from pathlib import Path def rwf(fp, mode="r", data="" ): if mode == "r": with open(fp, "r", encoding='utf8') as f: return(f.read()) elif mode == "w": with open(fp, "w", encoding='utf8') as f: f.write(data) old_pkg = input("Old PKGn").strip() new_pkg = input("New PKGn").strip() if not old_pkg or not new_pkg: exit("Args Missing") dirname, filename = os.path.split(os.path.abspath(__file__)) file_types = ['xml', 'txt', 'json', 'gradle', 'java'] _fs = glob.iglob(f"{dirname}/**", recursive=True) for file_path in _fs: ext = Path(file_path).suffix.replace(".", "").lower() if os.path.isfile(file_path) and ext in file_types: try: content = rwf(file_path) new_content = content.replace(old_pkg, new_pkg) rwf(file_path, "w", new_content) except: print(traceback.format_exc()) pass

Tasg: android-studio,