Rename package in Android Studio

2018-12-31 01:09发布

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.

30条回答
人间绝色
2楼-- · 2018-12-31 01:24

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.
查看更多
无色无味的生活
3楼-- · 2018-12-31 01:25
  • The first part consists of creating a new package under java folder and selecting then dragging all your source files from the old package to this new package. After that you need to remane 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.

查看更多
笑指拈花
4楼-- · 2018-12-31 01:28

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

查看更多
还给你的自由
5楼-- · 2018-12-31 01:29

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 :)

查看更多
几人难应
6楼-- · 2018-12-31 01:31
  1. Goto your AndroidManifest.xml.
  2. place your cursor in the package name like shown below don't select it just place it.

enter image description here

  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.

查看更多
爱死公子算了
7楼-- · 2018-12-31 01:31

Packages serve two purposes. One is to uniquely identify your app in the Google Play Store. The other is to name the package for the R.java class which is generated when you build your project. You can think of the first purpose as the external package and the second as the internal package. Assuming you want to change the external package so you can identify in the Play store, there's a convenient way to do this.

In Android Studio,

 choose File -> Project Structure -> Choose your app's module -> Click on the 
 Flavors tab -> change the Application id.

Now, when you build your project, your APK and manifest will use this new package name.

查看更多
登录 后发表回答