重命名生成 R.java 的包

发布于 2024-10-28 06:18:54 字数 205 浏览 0 评论 0原文

我正在使用 eclipse,并且创建了一个测试 android 项目,并且包含 R.java 的“gen”文件夹中的包当前称为 com.something.test(我认为我只是在测试,但在其上构建了我的整个应用程序!)

加载应用程序时会引用它,手机有时会显示它,所以我需要重命名它。我通过单击“重构”尝试了此操作,但它用旧名称再次重新生成了它!

我可以重命名吗?

I am using eclipse and I created a test android project and the package in the "gen" folder that contains R.java is currently called com.something.test (I thought I was just testing but build my whole app on it!)

This is referenced when loading the app and the phone sometimes displays it so I need to rename it. I tried this by clicking refactor but it regenerated it again with the old name!

Can I rename it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撩动你心 2024-11-04 06:18:54

右键单击您的项目,然后选择 Android Tools ->重命名应用程序名称:

在此处输入图像描述

或者,如果您想手动执行此操作,请转到清单文件,更改包名称,并清理项目。

Right click your project, then Android Tools -> Rename Application Name:

enter image description here

Or, if you want to do it manually, go to your manifest file, change the package name, and make a Project Clean.

公布 2024-11-04 06:18:54

检查 AndroidManifest.xml 顶级元素上有一个 package 属性。这是生成 R.java 的地方,重命名它时应该小心。

Check the AndroidManifest.xml, there's a package attribute on the <manifest> top-level element. That is where R.java is generated and you should be careful renaming it.

靑春怀旧 2024-11-04 06:18:54

我知道这是一个老问题,但我现在需要它,也许其他人也需要它。
我正在使用 Android Studio 2.1.1

为了使其工作,我必须更改:

  • package - 在 AndroidManifest.xml
  • applicationId - 在应用程序文件夹的 build.gradle

这样就不需要导入 R在每个 java 类上。

重要的是要记住,这不是通常的更改,也不应该每次都进行。

链接applicationId vs. 包名称 解释了它们之间的区别以及为什么不应更改它们。

I know it's an old question, but I needed it now and maybe someone else needs it too.
I'm using Android Studio 2.1.1

To make it work I had to change:

  • package - on AndroidManifest.xml
  • applicationId - on build.gradle of the app folder

This way there was no need to import R on every java class.

It is important to remember that this is not an usual change and should not be done every time.

The link applicationId vs. package name explains the difference between them and why they should not be changed.

清醇 2024-11-04 06:18:54

对于那些试图手动更改它的人来说,链接 R.java 文件的说法是正确的。如果您只是在清单中更改它,您将在引用资源的 java 文件中收到一长串“R 无法解析”错误。要纠正这个问题,您需要在每个文件中添加 R 类的导入。

因此,如果您的包最初为:“com.mycomp.myapp.android”并将其更改为“com.mynewcomp.myapp.android”,您将需要进入每个 java 类文件并添加:

import com. mynewcomp.myapp.android.R;

如果您在项目上运行最初建议的重命名命令,那么系统会自动为您完成此操作。

For those who attempt to manually change it, the statements that the R.java file is linked is correct. If you simply change it in the manifest, you will get a long sequence of "R cannot be resolved" errors throughout your java files that reference resources. To correct that you'll need to add an import for the R class in each of those files.

So if you had your package originally as: "com.mycomp.myapp.android" and changed it to "com.mynewcomp.myapp.android" you would need to go into each java class file and add:

import com.mynewcomp.myapp.android.R;

If you run the originally suggested rename command on the project this is done for you automatically.

心在旅行 2024-11-04 06:18:54

它被命名为应用程序的根包。如果您将应用程序的根包更改为其他包,R.java 现在将存在于该包中。

It gets named to the root package of your app. If you change your app's root package to something else, R.java will exist in that package now.

一曲琵琶半遮面シ 2024-11-04 06:18:54

另一件需要检查的事情是您是否有带有 xml 命名空间的自定义视图。我花了一分钟才意识到我必须更改 xmlns 属性。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myxmlnamespace="http://schemas.android.com/apk/res/com.mynew.packagename"
    ...

Another thing to check is if you have custom Views with xml namespaces. It took me a minute to realize I had to change the xmlns attribute.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myxmlnamespace="http://schemas.android.com/apk/res/com.mynew.packagename"
    ...
绿萝 2024-11-04 06:18:54

我使用右键->Refactor和Android Manifest.XML一一重构了我的包。我的 R 文件仍然基于旧的包名称。

关闭并打开项目解决了这个问题,但我需要对所有 java 文件进行手动更改。

I refactored my package one by one using Right Click->Refactor and Android Manifest.XML. My R file was still building on the old package name.

Closing and Opening the project fixed this issue, but I needed to go and do a manual change on all my java files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文