64

I have a package structure: com.report.domain.model

I would like to change it to com.reporting.domain.model. When I select RefactorRename on package com.report.domain.model, I only see an option to rename 'model'. I have tried switching between Project view and Package, but to no avail. I know how to make this change in Eclipse via Navigator view, but how can I do the same in IntelliJ IDEA?

2 Answers 2

87

In the Project Window, click on the Gears icon in the title bar Gears icon and deselect "Compact Empty Middle Packages". Then you can separately select the report package and rename it.

See also: http://www.jetbrains.com/idea/webhelp/project-tool-window.html#d242351e401

N.B. As of IntelliJ IDEA 2016.3, there is a bug in the menu and when the option is selected, it has a different label "Hide Empty Middle Packages", see https://youtrack.jetbrains.com/issue/IDEA-168362.

0
55

To change the full package name:

User jkovacs has already answered this, but I'm going to cover this in a bit more detail. 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
    • 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

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

    Refactor Directories

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

2
  • You're definitely right it needs to be made simpler. 4 years later and still waiting... I did a three-level package rename today (let's call it A.B.C to D.E.F) and I found that (a) references to the class "R" were not updated, (b) some references in XML files were changed to be like "D.B.C.A.B.C", lots of things to update manually. I guess as people don't do this very often, they don't want to put much effort into making it smoother. Jul 17, 2019 at 20:06
  • Actually I don't think it's correct to select "rename all". I have a project with multiple modules, I was trying to rename "com" in just one of the modules (e.g. module 1 has com.foo, module 2 has com.bar, I want to change module 1 to org.baz, without affecting module 2). When I selected "rename all", all of the com packages were renamed, not only the ones in the module I clicked to refactor. When I did it again and selected "rename current", it worked properly. Jul 17, 2019 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.