我正在使用 eclipse,我创建了一个测试 android 项目,并且R.java
当前调用了包含在“gen”文件夹中的包com.something.test
(我以为我只是在测试,但在它上面构建了我的整个应用程序!)
这是在加载应用程序时引用的,手机有时会显示它,所以我需要重命名它。我通过单击重构尝试了此操作,但它再次使用旧名称重新生成了它!
我可以重命名它吗?
我正在使用 eclipse,我创建了一个测试 android 项目,并且R.java
当前调用了包含在“gen”文件夹中的包com.something.test
(我以为我只是在测试,但在它上面构建了我的整个应用程序!)
这是在加载应用程序时引用的,手机有时会显示它,所以我需要重命名它。我通过单击重构尝试了此操作,但它再次使用旧名称重新生成了它!
我可以重命名它吗?
右键单击您的项目,然后单击 Android 工具 -> 重命名应用程序名称:
或者,如果您想手动执行此操作,请转到清单文件,更改包名称,然后进行项目清理。
检查,顶级元素上AndroidManifest.xml
有一个package
属性。<manifest>
那是R.java
生成的地方,你应该小心重命名它。
对于那些试图手动更改它的人来说,链接 R.java 文件的语句是正确的。如果您只是在清单中更改它,您将在整个引用资源的 java 文件中收到一长串“R 无法解析”错误。要更正此问题,您需要在每个文件中添加 R 类的导入。
因此,如果您的包最初为:“com.mycomp.myapp.android”并将其更改为“com.mynewcomp.myapp.android”,则需要进入每个 java 类文件并添加:
import com.mynewcomp.myapp.android.R;
如果您在项目上运行最初建议的重命名命令,则会自动为您完成。
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:
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.
链接applicationId vs. package name解释了它们之间的区别以及为什么不应该更改它们。
它被命名为你的应用程序的根包。如果您将应用程序的根包更改为其他内容,则 R.java 现在将存在于该包中。
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"
...
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.