Rename package in Android Studio

后端 未结 30 1971
深忆病人
深忆病人 2020-11-21 05:21

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 refactorin

相关标签:
30条回答
  • 2020-11-21 06:09

    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 the Compact Empty Middle Packages option

      Compact Empty Middle Packages

    3. Your package directory will now be broken up into 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

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

    0 讨论(0)
  • 2020-11-21 06:10

    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.

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

    You will be advised to refactor the package:

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

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

    0 讨论(0)
  • 2020-11-21 06:10

    The common mistake that one can make is one cannot rename the package structure i.e. it is not possible to change com.name.android to com.Renamed.android when one tries to modify at the com.name.android level.

    In order to have the same desired change go one level up i.e com.name and here when you refactor change it to Renamed. This will work always.

    0 讨论(0)
  • 2020-11-21 06:10

    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.

    0 讨论(0)
  • 2020-11-21 06:10

    Quick and easy way:

    1- open MainActivity.java or any available java 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.

    0 讨论(0)
  • 2020-11-21 06: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.
    0 讨论(0)
提交回复
热议问题