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

That works very well for me, even for changing the occurrences, in all the modules concerned, for the folders names I want to modify in the package but, for me, it works fine only if I follow exactly the following steps :

  • A - If you want to change the name of the root folder (in other words the folder's name who is normaly used in the package to contain the principal activity), you can do it before or after the following steps (B to C) by exiting from "Android Studio" and rename manually this root folder. Click after on "Import project..." and then on the new name of your project (this import process will automatically readjust the 'Gradle' files with the project files.)
  • B - before the following C step, don't forget to modify manually the folder's names you want to change in the package by changing them in the 3 following files: 'AndroidManifest.xml', 'MainActivity.java' (or the name you eventually choose for your first activity) and the 'build.gradle (Module: app)' file.
  • C - Follow the steps 1 to 6 described above ("In your Project pane, click on the little gear icon...") -> https://stackoverflow.com/a/29092698/2001610
  • D - Only if you want to change the name of the root folder now, you can follow the 'A' process, as described above.
查看更多
听够珍惜
3楼-- · 2018-12-31 01:42

Updated answer: May 2015

OK I have been struggling with cloning & renaming projects in Android Studio, but finally I achieved it. Here are the steps to follow:

  1. Copy the project folder, rename it & open it with Android Studio
  2. Rename module directory from explorer
  3. Rename projectName.iml and content
  4. Rename idea/.name content
  5. In your Project pane, click on the little gear icon -> uncheck "Compact Empty Middle Package"
  6. Refactor src directories for new package name (rename package, "not rename directory")
  7. In build.gradle rename application id
  8. settings.gradle rename module

That's it...

查看更多
唯独是你
4楼-- · 2018-12-31 01:42

I tried the two top-voted solutions but found some issues even though both work to some extent.

  • List item: The new package-drag-drop method leaves some unchanged and creates some undesired effects
  • List item: The rename package only changes the last part of package name

After some experiments I found the following method works well for me.

If you just need to change the last part of package name, use the method outlined by GreyBeardedGeek, namely

Right-click on the package in the Project pane. Choose Refactor -> Rename from the context menu

If you need to change the whole package name, do the following.

Right-click on the package in the Project pane. Choose Refactor -> Move from the context menu

This will create a new package folder (when necessary) but will keep the last part of your package name as before. If you need to change the last part, do the rename accordingly.

Note also that you may need to modify package names in e.g. build.gradle, manifest, and/or any xml resource files, or even in your code if hardcoded. After all that, do Sync/Clean/Rebuild project as necessary.

查看更多
路过你的时光
5楼-- · 2018-12-31 01:43

I recommend to use the Sublime Text(or Notepad++). Replace com.one.lastname -> com.two.newname and com/one/lastname -> com/two/newname in ...\Projects[MyProject]. and don't forget to rename ...\Projects\MyProject\app\src\main\java\com\one\lastname, ...\Projects\MyProject\app\src\test\java\com\one\lastname and ...\Projects\MyProject\app\src\androidTest\java\com\one\lastname! That's all:) screenshot

查看更多
步步皆殇っ
6楼-- · 2018-12-31 01:46

I've found a way easier solution to this problem which also changed the generated imports like com.test.testpackagechange.R and only takes about a minute.

Your first step is to open Android Studio and open the replace all window (Mac: cmd + shift + R, Windows I assume: ctrl + shift + R). Type in your old package name and in the one below your new package name. Click Find. This may take a while because it is also looking through the generated items. If it has over 1000 hits, just click continue.

enter image description here

After you've done that push Replace All to replace your old package name with your new one.

enter image description here

Now close Android Studio and go to Finder on Mac or Windows Explorer on Windows. Change the name of the folders to your new package name, like this:

enter image description here

Now open Android Studio again. Gradle will sync and your package name should be changed to the new one.

I've found this to be the easiest one and the one that covers all areas like generated files.

查看更多
宁负流年不负卿
7楼-- · 2018-12-31 01:48

Select the package that will be refactored. RefactorMove"Move xxx to new package".

查看更多
登录 后发表回答