Android R.java 文件背后的魔法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2048415/
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
Magic behind R.java file
提问by LambergaR
Recently I have been having quite some problems with R.java file. Now I have decided to do a backup and delete the file to see what happens.
最近我遇到了一些 R.java 文件的问题。现在我决定做一个备份并删除文件,看看会发生什么。
Nothing happened, so I created an empty R.java file and hoped for the best. Now Eclipse seems to figure out that the file was tampered with and even issues a warning:
什么也没发生,所以我创建了一个空的 R.java 文件并希望一切顺利。现在 Eclipse 似乎发现文件被篡改,甚至发出警告:
R.java was modified manually! Reverting to generated version!
And that's all there is. I tried building it manually but got no results.
这就是全部。我尝试手动构建它但没有结果。
So, I have two questions:
所以,我有两个问题:
- what should I do to force Eclipse to regenerate the file
- what is happening here? How is the file created, where is the code that is generating the file?
- 我该怎么做才能强制 Eclipse 重新生成文件
- 这里发生了什么?文件是如何创建的,生成文件的代码在哪里?
I would appreciate any help. As usual the problem occurred just a few days before the deadline :)
我将不胜感激任何帮助。像往常一样,问题发生在截止日期前几天:)
采纳答案by LambergaR
I found the problem - I have created a 9patch image with the patch area defined only on the Y axis (I was trying to prevent resizing on the other one).
我发现了问题 - 我创建了一个 9patch 图像,其补丁区域仅在 Y 轴上定义(我试图防止在另一个上调整大小)。
The question remains: Why isn't there a warning about the error? Or if there is - where is it buried?
问题仍然存在:为什么没有关于错误的警告?或者如果有 - 它埋在哪里?
回答by Stan Mohler Jr.
I had a problem getting my Android app to compile, with seemingly inexplicable errors about failure to generate R.java.
我在编译我的 Android 应用程序时遇到了问题,出现了关于无法生成 R.java 的看似莫名其妙的错误。
The problem was caused by my previously killing the Android emulator during the build. Don't do that! A clue was the appearance of several left over *.out.xml files.
问题是由于我之前在构建过程中杀死了 Android 模拟器。不要那样做!一个线索是出现了几个遗留的 *.out.xml 文件。
The solution was to select the project, click Project->Clean, and then manually delete all the *.out.xml files. Only then could I click Run->Run and have a successful build.
解决办法是选中项目,点击Project->Clean,然后手动删除所有*.out.xml文件。只有这样我才能单击运行->运行并成功构建。
One more thing: I'm using Eclipse. I had to make sure that the project was selected in Package Explorer, and notjust some random source file. Easy to not notice!
还有一件事:我正在使用 Eclipse。我必须确保该项目在Package Explorer中选择,而不是只是一些随机的源文件。容易不注意!
回答by Tomas
R.java is autogenerated on build. It's content is based on the resource files (including layouts and preferences).
R.java 在构建时自动生成。它的内容基于资源文件(包括布局和首选项)。
When you deleted it, it was recreated, but if you create your own, you will get into trouble as the build system will not replace it.
当您删除它时,它会被重新创建,但是如果您创建自己的,则会遇到麻烦,因为构建系统不会替换它。
You can simply delete it again (manually or by choosing Project->Clean) and rebuild your program.
您可以简单地再次删除它(手动或通过选择 Project->Clean)并重建您的程序。
回答by Antonio
I had this problem several times, and most of the times it is caused by some mistake in the "res" files, check everything is OK in all your res files, mainly XML, and after you can built.
我多次遇到这个问题,大多数时候是由“res”文件中的一些错误引起的,检查所有 res 文件中的一切正常,主要是 XML,然后可以构建。
For example, two common causes of this problem can be:
例如,此问题的两个常见原因可能是:
Didn't properly spell the name of a property in a Style in a XML file.
Didn't properly close a string in the XML of a locale file.
未正确拼写 XML 文件中样式中的属性名称。
未正确关闭语言环境文件的 XML 中的字符串。
回答by ponkin
Sorry, but try to check the output in Eclipse. May be you have some errors that prevents Eclipse to finish build proccess(check your AndroidManifest.xml, is it correct?).I mean, that you should check all your resource files(string.xml, layouts, ...) and interfaces(*.aidl), may be there are some problems(inconsistent syntax, or something....) that prevent eclipse to generate R.java.
抱歉,请尝试检查 Eclipse 中的输出。可能您有一些错误阻止 Eclipse 完成构建过程(检查您的 AndroidManifest.xml,是否正确?)。我的意思是,您应该检查所有资源文件(string.xml、布局等)和接口(*.aidl),可能是有一些问题(语法不一致,或者什么的……)阻止 eclipse 生成 R.java。
回答by Juan
I had the same issue and it was that I had named a resource file (a .png image) using uppercase letters. (ImageButton001.png instead of imagebutton001.png).
我遇到了同样的问题,我用大写字母命名了一个资源文件(一个 .png 图像)。(ImageButton001.png 而不是 imagebutton001.png)。
Afther changing the name of the file my R.java came back.
更改文件名后,我的 R.java 又回来了。
Rgards, Juan
加兹,胡安
回答by Lawrence
If there's insufficient free memory the build tool will silently fail(Eclipse will not show any errors or report anything in the error log). Close your other open applications (e.g. the emulator and browser) then add a space to the application manifest file and save it.
如果可用内存不足,构建工具将无声地失败(Eclipse 不会在错误日志中显示任何错误或报告任何内容)。关闭其他打开的应用程序(例如模拟器和浏览器),然后在应用程序清单文件中添加一个空格并保存。
回答by Oliver
I had the same problem. I just went to:
我有同样的问题。我刚去:
Project -> Properties -> Android
and chose the right Emulator for my project, I had 3 different to choose from in that moment. I realized that i changed the Emulator in some given moment. So try to change the Emulator, that might help!
并为我的项目选择了正确的模拟器,那一刻我有 3 种不同的模拟器可供选择。我意识到我在某个特定时刻更改了模拟器。因此,尝试更改模拟器,这可能会有所帮助!
Good luck
祝你好运
回答by nwestwood
I had a similar problem, I had renamed a .wav file in the Raw folder with a capital letter. When I changed to all lower case, cleaned the project and it rebuilt R.javafine.
我有一个类似的问题,我在 Raw 文件夹中用大写字母重命名了一个 .wav 文件。当我更改为全部小写时,清理了项目并且它重建得R.java很好。
回答by zorglub76
As ponkin stated - you probably have one single problem that causes R not to re-generate itself, because of which you now have one million and one problem.
正如 ponkin 所说 - 您可能有一个问题导致 R 无法自行重新生成,因此您现在有一百万零一个问题。
Do not try to create R class yourself. Try to think of the last XML you've been messing with, and check it for errors
不要尝试自己创建 R 类。试着回想你最近弄乱的 XML,并检查它是否有错误
I've had the same problem here
我在这里遇到了同样的问题

