[android] How do I rename the android package name?

Pressing Shift+F6 seems only to rename the last directory.
For example, in the project com.example.test it will offer to rename test only. The same applies if I navigate to package name in .java or Manifest file and press Shift+F6.

So is there a way to rename the package?

This question is related to android intellij-idea

The answer is


I found another solution for renaming a package in the entire project:

  1. Open a file in the package.
  2. IntelliJ displays the breadcrumbs of the file, above the opened file.
  3. On the package you want renamed: Right click > Refactor > Rename.

This renames the package/directory throughout the entire project.


Super simple approach:

  1. Find R.java in the build/generated directory
  2. Create new package directories, again under build/generated/etc
  3. Move R.java to the newly created package.
  4. A dialog will appear. Choose to refactor the existing code with the new package name.
  5. Clean and Rebuild

Done!


What I did was the following :

I simply created the package with the desired name , in the src folder , next to the current package with current name.

I dragged all contents of current package to new package , intellij popped a dialog box asking me if I want to refactor inside package references and project references to new package , I clicked 'yes' and TA-DAAA , worked like a charm.


Deselect Hide Empty Middle Packages in Project Explorer Windows settings menu than you will be able to refactor each directory


Solution 1: I changed src subfolder names, manifest file, activity headers and run/debug configuration. And still got building errors. If your project is not so big, just create a new project, copy-paste old files and change package names in headers of .java files and android manifest.

Solution 2: Inside IntelliJ 12, goto someFile.java and find your old package name

package com.myself.project2

click to which word that you want to change. Press Shift+F6.
Dialog comes, type new name.

Then goto Run/Edit configurations. Inside Launch radio button change package name to new one. It should work.

Other renaming options of IntelliJ are: http://www.jetbrains.com/idea/webhelp/rename-refactorings.html


In Android Studio 1.1, the simplest way is to open your project manifest file, then point to each part of the package name that you want to change it and press SHIFT + F6 , then choose rename package and write the new name in the dialog box. That's all.


What I did was the following

  1. Change manifest.
  2. Close androidStudio
  3. Open folder with Windows Explorer. Change folder names.
  4. Open androidStudio, and do a search and replace.

Done!


I solved this issue by changing the directory name manually from the command line. Intellij then recognized the new package name automatically. I then had to do a search and replace for the package name in each file that imported it. This seems like an ugly workaround, but Intellij seemed unwilling to change the package name otherwise.


Lets address the two use cases

**Rename a package name or Trim a package **

com.mycompany.mystupidapplicationname to com.mycompany.brandname

or

com.someothercompany.mystupidapplicationname to com.someothercompany.mystupidapplicationname

or

com.someothercompany.mystupidapplicationname to someothercompany.brandname

  1. Select Package in the drop down in Android Studio

Select package in dropdown

  1. Right click on the package which contains your MainActivity and refractor it

enter image description here

  1. Select Rename Package and everything will work like a charm

But this will only rename the last part of package name

  1. To rename the first or second part of package name . Create a new dummy file in the part of package which you want to rename. the file will be created inside mycompany.com.myapplicationame. Then move the file inside mycompany(right click => move) to make mycompany appear in tree view. This will make that part part of the tree view and will you help you to do a right click and rename that part.

enter image description here

  1. Delete the dummy file

  2. To Trim a package , simply right click and move the package part inside its grand parent


  1. Goto your AndroidManifest.xml.
  2. place your cursor in the package name like shown below don't select it just place it.

Just place your cursor on it

  1. Then press shift+F6 you will get a popup window as shown below select Rename package.

enter image description here

  1. Enter your new name and select Refactor. (Note since my cursor is on "something" only something is renamed.)

That's it done.


I found a good work around for this problem. Taking the example mentioned in the question, following are the steps for changing the package name from com.example.test to com.example2.test :

  1. create a temporary directory, say temp inside the directory example (alongside directory test).
  2. Go back to the Intellij project, select the folder com.example and Click Shift + F6. Now it offers to rename the text example. Here you can enter the new text you want and IntelliJ will do the rest for you.
  3. Delete the temporary directory temp from the file system.

This idea can be used to refactor any part of the package name.

Good luck!!


Eclipse: Right click on the project > Android tools > Rename application package.

As simple as that...

enter image description here

In Android Studio: open the build.gradle file > rename the applicationId under defaultConfig > synchronize


If you want to rename full android package, this is the best way to do that:

  1. Mark as checked - Comapct Emty Middle Packages

  2. Right click on the packcage you want to change, and then Refactor->Rename->Rename all

You can find video tutorial on this link: https://www.youtube.com/watch?v=A-rITYZQj0A


The best way to solve this is going to the AndroidManifest.xml: package="com.foocomp.fooapp:

  • Set the cursor on "foocomp"
  • Press Shift+F6
  • Rename Package to whatever you want
  • Repeat for "fooapp".

Works for me.

Also, replace in Path in Whole Project as it didn't change everything. Then Clean, Rebuild and it works --> In Android Studio / IntelliJ you should unmark "Compact Empty Middle Packages"


Most of the answers even the most voted answers didn't do the job properly, they seem to work and the builds work however, a closer look at the file structure and references will show you that not much was done. IntelliJ actually does this whole process automatically.


This modification needs three steps :

  1. Change the package name in the manifest
  2. Refactor the name of your package with right click -> refactor -> rename in the tree view, then Android studio will display a window, select "rename package"
  3. Change manually the application Id in the build.gradle file : android / defaultconfig / application ID

Then clean / rebuild the project


Most of the answers even the most voted answers didn't do the job properly, they seem to work and the builds work however, a closer look at the file structure and references will show you that not much was done. IntelliJ actually does this whole process automatically.

1) Go to Project Tab and make sure Packages is the active Tab in the drop down like so:

Select Packages in the drop down

find your com.example package (note test as in com.example.test is not there) right-click and choose Refactor->Rename...

It will issue a warning that multiple directories... (which is actually the desired effect), click on Rename package.

Input the new package name and Refactor...

It will do searches and show you refactoring results.

Click on the Do Refactor button and it will do everything and even rename the folders and files involved... every reference is corrected to this new name.

2) Then Finally change the AndroidManifest.xml file

:)


In addition to @Cristian's answer above, I had to do two more steps to get it working correctly. I will sum all of it here:

  1. Change the package name manually in the manifest file.
  2. Click on your R.java class and the press F6 (Refactor->Move...). It will allow you to move the class to another package, and all references to that class will be updated.
  3. Change manually the application Id in the build.gradle file : android / defaultconfig / application ID [source].
  4. Create a new package with the desired name by right clicking on the java folder -> new -> package and select and drag all your classes to the new package. Android Studio will refactor the package name everywhere [source].

Unfortunately all above didn't work for me. After having lots of trials, What worked for me in Android Studio:

  1. do a 'move' on the package to a new package name you want.(right click on package and select Refactor -> Move) If Refactor -> Move didn't work for you, then create a package with the name you want and move manually in the existing package to the new one, and then delete the old empty package.

  2. Change package name in manifest (manually)

  3. Have a replace for the old package name with the new package name globally (in the full path by going to Edit -> Find -> Replace In Path)