在 Eclipse 中为 Android 开发:R.java 不重新生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2757107/
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 07:14:34  来源:igfitidea点击:

Developing for Android in Eclipse: R.java not regenerating

androideclipser.java-file

提问by Aleksejs Popovs

I've found out that my R.java is never updated, so it doesn't contain information about my new resources, so I decided to delete it and thought that Eclipse would generate a new one. But that didn't happen, and I don't have R.java now. How can I regenerate one?

我发现我的 R.java 从未更新过,因此它不包含有关我的新资源的信息,因此我决定将其删除并认为 Eclipse 会生成一个新资源。但这并没有发生,我现在没有 R.java。我怎样才能再生一个?

I'm using Windows 7.

我正在使用 Windows 7。

From one of the comments: "Doing Project-> Cleanis what caused the problem for me. Cleaning deletes R.java...and for whatever reason the plugin is not regenerating the file."

来自其中一条评论:“做项目->清理是我遇到问题的原因。清理会删除 R.java ......无论出于何种原因,插件都没有重新生成文件。”

采纳答案by aioobe

This sitesuggests:

该网站建议:

if you run a clean on the project it should regenerate all the generated Java files, namely R.

如果您对项目运行清理,它应该重新生成所有生成的 Java 文件,即 R。

...and...

...和...

In Eclipse, under the Project menu, is an option build automatically. That would help you build the R.java file everytime modifications are made. The Clean... option is also there under Project.

在 Eclipse 中,在项目菜单下,是一个选项自动构建。这将帮助您在每次进行修改时构建 R.java 文件。清洁...选项也在项目下。

This sitesuggests another solution.

该站点提出了另一种解决方案。

回答by Muz

I found this happening to me with a broken layout and everything blows up. Relax, it's like that old mistake when you first learned programming C where you forget one semicolon and it generates a hundred errors. Many panic, press all the buttons, and makes things worse.

我发现这发生在我身上,布局损坏,一切都炸了。放轻松,这就像你第一次学习 C 语言时的那个老错误,你忘记了一个分号,它会产生一百个错误。许多人恐慌,按下所有按钮,让事情变得更糟。

Solution

解决方案

  • Make sure that anything the R.links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, Rwill not regenerate.
  • If you somehow hit something and created import android.Rin your activity, remove it.
  • Run Project -> Clean. This will delete and regenerate R and BuildConfig.
  • Make sure Project -> Build Automaticallyis ticked. If not, build it manually via Menu -> Project -> Build Project.
  • Waita few seconds for the errors to disappear.
  • If it doesn't work, delete everything inside the /gen/ folder
  • If it still doesn't work, try right-clicking your project -> Android Tools -> Fix Project Properties.
  • Check your *.propertiesfiles (in the root folder of your app folder) and make sure that the links in there are not broken.
  • Right-click your project > properties > Android. Look at the Project Build Target and Library sections on the right side of the page. Your Build Target should match the target in your AndroidManifest.xml. So if it's set to target 17 in AndroidManifest, make sure that the Target Name is Android 4.2. If your Library has an X under the reference, remove and re-add the library until there's a green tick. This might happen if you've moved a few files and folders around.
  • 确保R.链接到的任何内容都没有损坏。修复 XML 文件中的所有错误。如果 ADK 中的任何内容损坏,R则不会重新生成。
  • 如果您以某种方式击中并import android.R在您的活动中创建了某些内容,请将其删除
  • 运行项目 -> 清理。这将删除并重新生成 R 和 BuildConfig。
  • 确保Project -> Build Automatically被勾选。如果没有,请通过Menu -> Project -> Build Project.
  • 等待几秒钟让错误消失。
  • 如果它不起作用,请删除 /gen/ 文件夹中的所有内容
  • 如果它仍然不起作用,请尝试右键单击您的项目 -> Android 工具 -> 修复项目属性
  • 检查您的*.properties文件(在您的 app 文件夹的根文件夹中)并确保其中的链接没有损坏。
  • 右键单击您的项目 > 属性 > Android。查看页面右侧的 Project Build Target 和 Library 部分。您的构建目标应与 AndroidManifest.xml 中的目标相匹配。因此,如果在 AndroidManifest 中将其设置为目标 17,请确保目标名称为 Android 4.2。如果您的图书馆在参考下有 X,请删除并重新添加图书馆,直到出现绿色勾号。如果您移动了一些文件和文件夹,则可能会发生这种情况。

What to do if R doesn't regenerate

如果 R 不再生怎么办

This usually happens when you have a broken XML file.

这通常发生在您的 XML 文件损坏时。

  • Check errors inside your XML files, mainly within the /res/folder
  • Common places are /layout/and /values/, especially if you've changed one of them recently
  • Check AndroidManifest.xml. I find that often I change a string and forget to change the string name from AndroidManifest.xml.
  • Check that Android SDK Build-toolsis installed. Window -> Android SDK Manager -> Tools -> Android SDK Build-tools
  • Make sure when you update the Android SDK Tools, you also update the Android SDK Platform-toolsand Android ADK Build-tools. Build fails silently if they don't match.
  • If you can't find the issue, right click /gen/ -> Restore from local history... -> tick R.java -> click Restore. Even if it doesn't solve the problem, it will clear out the extra errors to make the problem easier to find.
  • 检查 XML 文件中的错误,主要是在/res/文件夹中
  • 常见的地方是/layout/and /values/,特别是如果您最近更改了其中之一
  • 检查AndroidManifest.xml。我发现我经常更改字符串而忘记将字符串名称从AndroidManifest.xml.
  • 检查Android SDK Build-tools是否已安装。Window -> Android SDK Manager -> Tools -> Android SDK Build-tools
  • 确保在更新 时Android SDK Tools同时更新Android SDK Platform-toolsAndroid ADK Build-tools。如果它们不匹配,构建将无声地失败。
  • 如果找不到问题,请右键单击 /gen/ -> 从本地历史记录恢复... -> 勾选 R.java -> 单击“恢复”。即使它没有解决问题,它也会清除额外的错误,使问题更容易找到。

回答by Amit Kumar

I had the same issue. When I checked it out I found that the name of the XML resource under layout was not having the correct naming convention. It had some capital letters. So I renamed it to make all letters lowercase and the magic worked.

我遇到过同样的问题。当我检查它时,我发现布局下的 XML 资源的名称没有正确的命名约定。它有一些大写字母。所以我重命名它使所有字母小写并且魔法起作用了。

回答by Pawel

One reason the R.class can go missing suddenly is when there are errors in you XML files. For instance, when you add an XML file with uppercase letters in the name like myCoolLayout.xml which is not allowed. Or when you have references that don't point to existing files, etc.

R.class 突然丢失的原因之一是 XML 文件中存在错误。例如,当您添加名称中带有大写字母的 XML 文件时,例如 myCoolLayout.xml,这是不允许的。或者当您有不指向现有文件等的引用时。

回答by masi

In Eclipse, simply use Project --> clean to clean the project. The R.java is going to be automaticly (re)-created.

在 Eclipse 中,只需使用 Project --> clean 来清理项目。R.java 将自动(重新)创建。

If for some reason that dosn't work:Make sureyour layout.xml files don't contains errors. Eclipse seems to be a bit buggy here: sometimes it doesn't mark the errors within the XML nor the package explorer. In such a case: Take a look at the "console" or "problems" view after using "clean". All errors should be displayed there. Fix them and redo a clean.

如果由于某种原因不起作用:确保您的 layout.xml 文件不包含错误。Eclipse 在这里似乎有点问题:有时它不会标记 XML 或包浏览器中的错误。在这种情况下:在使用“clean”后查看“console”或“problems”视图。所有错误都应该显示在那里。修复它们并重做清洁。

NOTE: It is NOT neccessary to fix the errors you get because of a missing R file!Just fix the XML files and other project errors and use clean!

注意:没有必要修复由于缺少 R 文件而导致的错误!只需修复 XML 文件和其他项目错误并使用 clean!

回答by androidWorkerTheEighth

Quick fix:

快速解决:

The package name in the manifest needs to be the same as the one in the /srcfolder, the /genfolder package will be automatically reproduced.

manifest中的包名需要与/src文件夹中的包名一致,/gen文件夹包会自动复现。

Detailed observation:

详细观察:

Observe the name of package in the /genfolder. In my case it was different than the one in the /srcfolder.

观察/gen文件夹中包的名称。就我而言,它与/src文件夹中的不同。

The package referenced in the manifest was that of the /genfolder.

清单中引用的包是/gen文件夹的包。

I attempted to add a package with the name of the /srcfolder to the /genfolder too see what would've happened, but this did not solve the issue. I proceeded then to remove the package name that was not the same as the package name of the /srcfolder. This package with the name that did not correspond to the /srcfolder was recreated as soon as I removed it.

我也尝试将带有/src文件夹名称的包添加到/gen文件夹中,看看会发生什么,但这并没有解决问题。然后我继续删除与/src文件夹的包名称不同的包名称。这个名称与/src文件夹不对应的包被我删除后立即重新创建。

Since the package reference in the manifest corresponded to the one that was being senselessly recreated and that did not correspond with the package in the /srcfolder, this prompted to rename package = "oldPackage"with the package = "srcFolderPackage".

由于清单中的包引用对应于被无意义地重新创建的包,并且与/src文件夹中的包不对应,因此提示package = "oldPackage"使用package = "srcFolderPackage".

回答by Glaux

I had the same issue, and I finally found the problem: In the strings.xml, I did not keep the line <string name="app_name">program-name</string>which is present by default when creating a new Android project. I thought it was only used for the program HelloWord, which is the original program.

我遇到了同样的问题,我终于找到了问题:在strings.xml中,我没有保留<string name="app_name">program-name</string>创建新Android项目时默认存在的行。我以为它只用于程序HelloWord,它是原始程序。

However, the AndroidManifest.xml refers to this value, hence:

但是,AndroidManifest.xml 引用了这个值,因此:

=> A compile error;
=> the R.java isn't updated;
=> ID are not recognized...

=> 编译错误;
=> R.java 没有更新;
=> ID 无法识别...

回答by Mr.India

Android has added in SDK build tool ADT 22 for the building mechanism. You just need to do the following steps.

Android 在 SDK 构建工具 ADT 22 中添加了构建机制。您只需要执行以下步骤。

  1. Update Android SDK Tool
  2. Update Android SDK Platform Tool
  3. Update Android SDK Build Tool
  4. Add the path of your build tool to the path variable. (path up to---- YOUR DIRECTORY-PATH\android-sdk\build-tools)
  1. 更新 Android SDK 工具
  2. 更新 Android SDK 平台工具
  3. 更新 Android SDK 构建工具
  4. 将构建工具的路径添加到路径变量中。(路径到----你的目录路径\android-sdk\build-tools)

This will solve the issue.

这将解决问题。

回答by Shashank

If your R.java isn't getting generated, one of the solutions is to delete the layout file named "blabla.out.xml".

如果您的 R.java 没有生成,解决方案之一是删除名为“blabla.out.xml”的布局文件。

After deleting this file, try cleaning the project from menu Project-> Clean.

删除此文件后,尝试从菜单Project-> Clean 清理项目

回答by JnBrymn

As a generalization of Glaux's answer, if you have anyerrors in the resdirectory, then R.java may not generate - even if you clean and rebuild. Resolve those errors first.

作为 Glaux 答案的概括,如果目录中有任何错误res,则 R.java 可能不会生成 - 即使您清理并重建。首先解决这些错误。

As an example: when you add an image file of say, "myimage-2.jpg", the system will consider this an error, since file names are limited to alphanumeric values. Do a refresh on your 'res' directory after adding any files and watch the output in your console window for any file name warnings.

例如:当您添加一个图像文件“myimage-2.jpg”时,系统会认为这是一个错误,因为文件名仅限于字母数字值。添加任何文件后刷新“res”目录,并在控制台窗口中查看任何文件名警告的输出。