重命名Android Studio中的程序包

本文翻译自:Rename package in Android Studio

How do you rename packages in the new IDE Android Studio, based on IntelliJ IDEA? 如何在新的基于IntelliJ IDEA的IDE Android Studio中重命名程序包?

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. 我在Eclipse工作了两年,在Eclipse中是一键式操作。


#1楼

参考:https://stackoom.com/question/18VVl/重命名Android-Studio中的程序包


#2楼

Right-click on the package at the Project Panel . 在“ 项目面板”上的软件包上单击鼠标右键。

Choose Refactor -> Rename from the context menu. 从上下文菜单中选择“重构”->“重命名”。


#3楼

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/ . 如果您的软件包名称由两个以上的点分隔,例如com.hello.world ,而且您未在com/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: 您所有的类都放在com/hello/world/ ,您可以按照以下步骤在Android Studio或IntelliJ中重构包名称:

  • [FIRST] Add something under your directories( com/ , com/hello/ ). [FIRST]在目录( com/com/hello/ )下添加一些内容。 You can achieve this by first add two files to package com.hello.world, say 您可以通过首先将两个文件添加到com.hello.world包中来实现此目的,例如
  com.hello.world.PackageInfo1.java com.hello.world.PackageInfo2.java 

then refactor them by moving them to com and com.hello respectively. 然后通过分别将它们移动到com和com.hello来重构它们。 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. 您将看到com和com.hello坐在那里的Project( Alt+1Command+1表示快捷方式),并且重命名目录重构正在那里按您的期望进行。

  • 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. 您在这里唯一要注意的是,当对话框询问您时,您必须选择目录而不是Packages。

  • 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. 此外,我想您需要在AndroidManifest.xml中手动重命名包名称,以便该文件中的其他名称可以使前缀受益。

  • [ALSO] , it might need you to replace all com.hello.world.R to the new XXX.XXX.XXX.R ( Command+Shift+R for short) [ALSO] ,可能需要您将所有com.hello.world.R替换为新的XXX.XXX.XXX.R (简称Command+Shift+R

  • 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. 好好享受。

#4楼

Another good method is: First create a new package with the desired name by right clicking on the Java folder → NewPackage . 另一个好方法是:首先创建一个新包与Java的文件夹→ 新建包装右键单击所需的名称。

Then, select and drag all your classes to the new package. 然后,选择所有类并将其拖到新包中。 Android Studio will refactor the package name everywhere. Android Studio将在任何地方重构程序包名称。

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. 如果使用此方法,则必须手动将AndroidManifest.xmlbuild.gradle文件更改为新软件包。


#5楼

IntelliJ IDEA has an option called "Compact Empty Middle Packages". IntelliJ IDEA有一个称为“紧凑空中间包”的选项。 Select the option icon of the Project tab and de/activate this. 选择“ 项目”选项卡的选项图标,然后取消/激活它。

See: How can I change top level package name in IntelliJ IDEA? 请参阅: 如何在IntelliJ IDEA中更改顶级程序包名称?


#6楼

  • 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 . 第一部分包括在java文件夹下创建一个新程序包,然后选择并将所有源文件从old package拖动到该new package After that you need to remane the package name in android manifest to the name of the new package. 之后,你需要remane在Android的包名manifest到新包的名称。

  • 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 . 在步骤2中,这里是你需要do.You需要改变旧的包名什么applicationId模块下build.gradle除了在你的Android工作室在不断变化的包名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. 因此,在总结,点击build.gradle这是下面的“AndroidManifest.xml中”和修改的值applicationId到新包的名字。

  • Then, at the very top, under build . 然后,在最顶层进行build clean your project, then rebuild . clean您的项目,然后rebuild It should be fine from here. 从这里开始应该没问题。

发布了0 篇原创文章 · 获赞 73 · 访问量 55万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105247534
今日推荐