如何在 Eclipse 中更改 Android 项目的包名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3697899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:26:01  来源:igfitidea点击:

How to change package name of Android Project in Eclipse?

androideclipsepackage

提问by Dheepak

I have an Android project created in Eclipse. I want to modify the package name and application of the project. How do I do that in Eclipse?

我有一个在Eclipse 中创建的 Android 项目。我想修改项目的包名和应用。我如何在 Eclipse 中做到这一点?

采纳答案by Paresh Mayani

As usual, by pressing F2on the package name, you can rename or change the package name, and also by right-clicking and then select Renameoption, you can change or rename the package name.

像往常一样,通过按F2包名,您可以重命名或更改包名,也可以通过右键单击然后选择Rename选项,您可以更改或重命名包名。

When you press F2, it will show you the dialog box as:

当您按下 时F2,它会显示如下对话框:

Alt text

替代文字

In this dialog, don't forget to check the "Update references" checkbox because by making "check" to this check-box, it will make changes to all the references of the package which are referred by other components of project.

在此对话框中,不要忘记选中“更新引用”复选框,因为通过选中此复选框,它将更改项目其他组件引用的包的所有引用。

回答by vamsu

This is a bug in the Eclipse Android tools.

这是Eclipse Android 工具中的一个错误

To fix: Right click on the project, go to Android tools -> Rename application package.

修复方法:右键单击项目,转到 Android 工具 -> 重命名应用程序包。

And also check AndroidManifest.xmlif it updated correctly. In my case it didn't, and that should solve this problem.

并检查AndroidManifest.xml它是否正确更新。在我的情况下它没有,这应该可以解决这个问题。

回答by TechnoTony

None of these worked for me, they all introduced errors.

这些都不适合我,它们都引入了错误。

The following worked for me:

以下对我有用:

  • Right click the project and select Android Tools >> Rename Application Package.
  • Enter the new Package name
  • Accept all the automatic changes it wants to make
  • Say yes to update the launch configuration
  • 右键单击项目并选择 Android 工具 >> 重命名应用程序包。
  • 输入新的包名称
  • 接受它想要进行的所有自动更改
  • 说是更新启动配置

回答by Rani Fayez Ahmad - Superlinux

One extremely importantnotice:

一个非常重要的通知:

NEVERuse a direct package names as in something similar to passing a string value containing the package name. Use the method getPackageName(). This will make the renaming dynamic. Do whatever to reach the method getPackageName().

永远不要使用直接的包名,就像传递包含包名的字符串值一样。使用方法 getPackageName()。这将使重命名动态。不惜一切代价到达方法 getPackageName()。

In Eclipse Juno, the correct way of renaming is:

在 Eclipse Juno 中,正确的重命名方法是:

  1. Go and edit the manifest.
  2. Remove every old package name in the manifest.
  3. Put instead of the old package name, the new package name in every location inside the manifest. You might have classes (Activities that is) that need direct package name references.
  4. Save the manifest.
  5. Then right click the package name inside the project.
  6. Select "Refactor".
  7. Select "Rename".
  8. Type the new package name.
  9. Select "update references".
  10. Press OK and you're done and watch out also what should be done to replace the new name.
  11. Don'tforget to also update the layout XML files with the new package name. You might have a custom View. Look for them.
  1. 去编辑清单。
  2. 删除清单中的所有旧包名称。
  3. 在清单内的每个位置放置新的包名而不是旧的包名。您可能有需要直接包名称引用的类(即活动)。
  4. 保存清单。
  5. 然后右键单击项目内的包名称。
  6. 选择“重构”。
  7. 选择“重命名”。
  8. 键入新的包名称。
  9. 选择“更新引用”。
  10. 按 OK,你就完成了,还要注意应该做什么来替换新名称。
  11. 不要忘记用新的包名更新布局 XML 文件。您可能有自定义视图。寻找他们。

回答by Kogile

 Right click on your project name and select Android Tools , then click 
 Rename Application Package, then change your project name click OK and 
 click Finish.

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Selva

Just right click on the project name and select Android toolsand click Rename Application Package nameand rename it. It's only for Android projects.

只需右键单击项目名称并选择Android 工具,然后单击重命名应用程序包名称并重命名。它仅适用于 Android 项目。

回答by anon

To change the app name when in Android, go to res/values/strings.xmland change app_nameto what you want.

要在 Android 中更改应用程序名称,请转到res/values/strings.xml并更改app_name为您想要的名称。

回答by arlen

Goto the Srcfolder of your Android project, and open the Java file.

转到Android 项目的Src文件夹,然后打开 Java 文件。

Change the package OldName.android.widgetto newName.android.widget.

将包更改OldName.android.widgetnewName.android.widget.

It gives you an error like this

它给你一个这样的错误

The declared package "newName.android.widget" does not match the expected package "OLDName.android.widget.

声明的包“newName.android.widget”与预期的包“OLDName.android.widget”不匹配。

To fix this problem, select Move filename.Java to Newname.android.widgetand delete the old package folder.

要解决此问题,请选择Move filename.Java to Newname.android.widget并删除旧的包文件夹。

Next step: Go to AndroidManifest.xmland change package="OldName.android.widget"to package="newName.android.widget".

下一步:转到AndroidManifest.xml并更改 package="OldName.android.widget"package="newName.android.widget"

回答by Praveen

Press F2and then rename it (or right click on the project and select Rename). It changes wherever that package is declared in your project. It's automatically renamed all over.

F2然后重命名(或右键单击项目并选择Rename)。它在项目中声明该包的任何地方都会发生变化。它会自动重命名。

回答by Christine

None of this worked for me until I combined the two answers mentioned in this post.

在我将本文中提到的两个答案结合起来之前,这些都不适合我。

Step 1: Right click on the project -> Select Android Tools -> Rename application Package. (This will change all the files in the gen folder and in AndroidManifest but will not change the package name in the src folder so I followed Step 2)

第一步:在项目上右键->选择Android工具->重命名应用程序包。(这将更改 gen 文件夹和 AndroidManifest 中的所有文件,但不会更改 src 文件夹中的包名称,因此我遵循了第 2 步)

Step 2: Inside src folder Right Click your package name -> Refactor -> Rename -> Enter the new name that you entered in Step 1.

步骤 2:在 src 文件夹内右键单击您的包名称 -> 重构 -> 重命名 -> 输入您在步骤 1 中输入的新名称。

Just to make sure Check AndroidManifest if there is still the old package name and replace with the new one (in my case inside the "uses-permission" tag).

只是为了确保检查 AndroidManifest 是否仍然存在旧包名并替换为新包名(在我的情况下在“uses-permission”标签内)。

Then close the Eclipse and Reopen it. Uninstall the app from your device and install it again and everything should be working fine.

然后关闭 Eclipse 并重新打开它。从您的设备上卸载该应用程序并重新安装,一切都应该正常工作。

Hope this helps and saves your time.

希望这有助于并节省您的时间。