Advertisement
Advertisement


Rename package in Android Studio


Question

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.

2019/02/27
1
1410
2/27/2019 2:04:16 PM

Accepted Answer

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 pane, click on the little gear icon ( Gears icon )

  2. Uncheck / De-select the Compact Empty Middle Packages option

    Compact Empty Middle Packages

  3. Your package directory will now be broken up in individual directories

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

  • Right-click 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

  1. Done! Anyway, Android Studio needs to make this process a little simpler.
2020/08/17
2944
8/17/2020 10:32:53 AM

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.

2018/11/12

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

2018/01/31

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

Step 1 : Select the Project option from 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 you new packagename

enter image description here

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

enter image description here

Step 5 :Rename the package name in 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 you new package name in replace box, then press find

enter image description here

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

enter image description here

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

2017/06/21

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

    Enter image description here

  3. Then press Shift + F6 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.

2019/01/01

Right-click on the package at the Project Panel.

Choose Refactor -> Rename from the context menu.

2015/10/03