在 Eclipse 中进行 Android 开发:R.java 未重新生成

发布于 2024-08-30 18:23:56 字数 233 浏览 7 评论 0原文

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

我使用的是 Windows 7。

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

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?

I'm using Windows 7.

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

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

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

发布评论

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

评论(30

别念他 2024-09-06 18:23:56

我发现这种情况发生在我身上,布局损坏,一切都崩溃了。放松,这就像你第一次学习 C 编程时犯的老错误一样,你忘记了一个分号,就会产生一百个错误。许多人惊慌失措,按下所有按钮,使事情变得更糟。

解决方案

  • 确保 R. 链接到的任何内容都没有损坏。 修复 XML 文件中的所有错误。如果 ADK 中的任何内容损坏,R 将不会重新生成。
  • 如果您不知何故碰到了某些东西并在 Activity 中创建了 import android.R,请将其删除
  • 运行项目 ->干净。这将删除并重新生成 R 和 BuildConfig。
  • 确保项目->自动构建被勾选。如果没有,请通过 Menu -> 手动构建它项目->构建项目
  • 等待几秒钟,错误就会消失。
  • 如果它不起作用,请删除 /gen/ 文件夹中的所有内容
  • 如果它仍然不起作用,请尝试右键单击您的项目 -> Android 工具 ->修复项目属性。
  • 检查您的 *.properties 文件(位于应用程序文件夹的根文件夹中)并确保其中的链接没有损坏。
  • 右键单击您的项目>属性>安卓。查看页面右侧的“项目构建目标”和“库”部分。您的构建目标应与 AndroidManifest.xml 中的目标匹配。因此,如果在 AndroidManifest 中将其设置为目标 17,请确保目标名称为 Android 4.2。如果您的库的引用下方有一个 X,请删除并重新添加该库,直到出现绿色勾号。如果您移动了一些文件和文件夹,则可能会发生这种情况。

如果 R 无法重新生成该怎么办

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

  • 检查 XML 文件中的错误,主要是在 /res/ 文件夹中,
  • 常见位置是 /layout//values/,特别是如果您最近更改了其中之一
  • 检查 AndroidManifest.xml。我发现我经常更改字符串并忘记更改 AndroidManifest.xml 中的字符串名称。
  • 检查是否已安装 Android SDK Build-tools窗口-> Android SDK管理器->工具-> Android SDK Build-tools
  • 确保在更新 Android SDK Tools 时,同时更新 Android SDK Platform-toolsAndroid ADK Build-工具。如果它们不匹配,构建将默默失败。
  • 如果找不到问题,右键单击 /gen/ ->从本地历史恢复... ->勾选 R.java ->单击“恢复”。即使不能解决问题,它也会清除多余的错误,使问题更容易找到。

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.

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, R will not regenerate.
  • If you somehow hit something and created import android.R in your activity, remove it.
  • Run Project -> Clean. This will delete and regenerate R and BuildConfig.
  • Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
  • Wait a 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 *.properties files (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.

What to do if R doesn't regenerate

This usually happens when you have a broken XML file.

  • 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-tools is 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-tools and 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.
不语却知心 2024-09-06 18:23:56

此网站建议:

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

...和...

在 Eclipse 中,Project 菜单下有一个自动构建的选项。
这将帮助您在每次修改时构建 R.java 文件
制成。 “清理...”选项也位于“项目”下。

此网站建议了另一种解决方案。

This site suggests:

if you run a clean on the project it should regenerate all the generated Java files, namely 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.

This site suggests another solution.

巷雨优美回忆 2024-09-06 18:23:56

我有同样的问题。当我检查时,我发现布局下的 XML 资源的名称没有正确的命名约定。它有一些大写字母。所以我将其重命名为所有字母小写,神奇的事情发生了。

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.

放手` 2024-09-06 18:23:56

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

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.

一紙繁鸢 2024-09-06 18:23:56

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

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

注意:不必修复因缺少 R 文件而出现的错误!只需修复 XML 文件和其他项目错误并使用 clean!

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

If for some reason that dosn't work: Make sure your 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.

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!

執念 2024-09-06 18:23:56

快速修复:

manifest 中的包名称需要与 /src 文件夹中的包名称相同,/gen 文件夹中的包将被自动复制。

详细观察:

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

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

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

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

Quick fix:

The package name in the manifest needs to be the same as the one in the /src folder, the /gen folder package will be automatically reproduced.

Detailed observation:

Observe the name of package in the /gen folder. In my case it was different than the one in the /src folder.

The package referenced in the manifest was that of the /gen folder.

I attempted to add a package with the name of the /src folder to the /gen folder 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 /src folder. This package with the name that did not correspond to the /src folder was recreated as soon as I removed it.

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 /src folder, this prompted to rename package = "oldPackage" with the package = "srcFolderPackage".

探春 2024-09-06 18:23:56

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

然而,AndroidManifest.xml 引用了这个值,因此:

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

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.

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

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

过去的过去 2024-09-06 18:23:56

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

  1. 更新 Android SDK 工具
  2. 更新 Android SDK 平台工具
  3. 更新 Android SDK 构建工具
  4. 将构建工具的路径添加到路径变量中。 (路径到----您的目录路径\android-sdk\build-tools)

这将解决问题。

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

  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)

This will solve the issue.

末が日狂欢 2024-09-06 18:23:56

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

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

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

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

牵你手 2024-09-06 18:23:56

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

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

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

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.

深居我梦 2024-09-06 18:23:56

如果您的操作系统是Ubuntu,我可以提供一些建议:

  1. 安装或升级ia32-lib:

    sudo apt-get 升级 ia32-libs
    
  2. 检查aapt文件夹是否有正确的权限:

    cd ANDROID/adt-bundle-linux-x86_64-20130522/sdk/build-tools/android-4.2.2
    chmod 777 apt
    
  3. 启动 Eclipse:

    sudo eclipse
    
  4. 运行项目 ->在 Eclipse 中清理

If your OS is Ubuntu, I can provide some suggestion:

  1. Install or upgrade ia32-lib:

    sudo apt-get upgrade ia32-libs
    
  2. Check if you have the right permission on the aapt folder:

    cd ANDROID/adt-bundle-linux-x86_64-20130522/sdk/build-tools/android-4.2.2
    chmod 777 aapt
    
  3. Start Eclipse:

    sudo eclipse
    
  4. Run Project -> Clean in Eclipse

翻了热茶 2024-09-06 18:23:56

您的 XML 文件 100% 有错误,但 XML 验证不会向您显示该错误。这就是为什么您需要首先检查 XML 文件的原因!

You 100% have an error in an XML-file, but the XML verification does not show you the error. This is the reason why you need to check your XML files first!

又怨 2024-09-06 18:23:56

查看 Eclipse 中的“问题”选项卡总是有帮助的。就我而言,我收到一条“android 无法解析目标 'android-8'” 错误消息,导致 R.java 无法生成。因此,我将导入的 target 更正为我在 default.properties 文件中使用的目标,然后我通过 Projects->Clean 执行了清理,瞧! R.java是自动生成的!希望有帮助!

It is ALWAYS helpful to take a look at the Problems Tab in Eclipse. In my case, I was getting a "android unable to resolve target 'android-8'" error message that kept the R.java from being generated. So, I corrected the imported target to the one I was using in the default.properties file, then I performed a clean via Projects->Clean and voila! R.java is automatically generated! Hope it helps!

悸初 2024-09-06 18:23:56

清理项目,关闭 Eclipse,重新打开它并启动我的项目终于使我的 R.java 重新出现......希望它有帮助

Cleaning project, closing Eclipse, re-opening it and launching my project finally made my R.java to reappear... Hope it helps

長街聽風 2024-09-06 18:23:56

确保您没有导入

android.R;

Make sure you are not importing

android.R;

南…巷孤猫 2024-09-06 18:23:56

如果 res 文件夹中存在任何错误,则永远不会生成 R.java。例如,drawable子文件夹中有两个同名文件,一个是icon.png,另一个是icon.html。

您可以在 Eclipse 控制台日志窗口中看到一些错误,显示“资源条目图标已定义。”。删除 icon.html 后,您可以清理或仅删除 gen 文件夹。你会发现R.java被创建了。

R.java will never be generated if there are any errors in the res folder. For example, in the drawable subfolder there are two files which have the same name, one is icon.png and the other is icon.html.

You can see some error in the Eclipse console log window which is saying "Resource entry icon is already defined.". After deleting icon.html, you can clean or just delete the gen folder. You will find that R.java is created.

离线来电— 2024-09-06 18:23:56

我发现 res 文件夹中包含大写字母任何文件都会产生此错误。这发生在我身上,因为我添加了一个 PNG 文件,但我忘记了。

I've found that any file that has capital letters in the res folder will create this error. This happened to me with a PNG file I added and forgot about.

落花随流水 2024-09-06 18:23:56

在阅读了许多帖子和 YouTube 视频后,我发现我们每个人都因不同的原因而缺少 R.java

以下是我在 Eclipse 中修复此问题的方法:

  • 手动gen 文件夹中创建 R.java 并保存。
  • 之后,转到项目并单击“清理”,

将显示以下消息,并且您的文件将自动重写:

R.java被手动修改!恢复到生成的版本!

After reading through many posts and YouTube videos, I found that each of us have R.java missing for different reasons.

Here's how I fixed this in Eclipse:

  • Create R.java in gen folder manually and save.
  • After that go to Project and click "Clean"

The following message will display and your file will automatically be rewritten:

R.java was modified manually! Reverting to generated version!

述情 2024-09-06 18:23:56

对我来说,我已经链接了 v7 appcompat 两次。无论如何,在 Eclipse 中,右键单击 Package Explorer 或 Navigator 中的项目名称,转到 Properties、Android,然后取消选中 Library 部分中的任何重复项。您可能需要构建 -->之后清洁。

For me, I had linked v7 appcompat twice. Anyhow, in Eclipse, right click the project name in Package Explorer or Navigator, go to Properties, Android, and uncheck any duplicates in the Library section. You may need to Build --> Clean afterwards.

旧瑾黎汐 2024-09-06 18:23:56

显然没有一个最终的答案,但这里还有另一个我在这里没有看到的答案:

在(尝试)团队同步后,我已经让 R.java 消失了,并且存在冲突解决文件在项目中(即 thingy.xml.minethingy.xml.r35 等)。删除它们/解决冲突重新生成R

There's obviously no one final answer to this, but here's another one I don't see here already:

I've had R.java disappear after (attempting) a Team Synchronize, and there were conflict resolution files in the project (i.e., thingy.xml.mine,thingy.xml.r35, etc.). Deleting them/resolving the conflicts regenerated R.

高冷爸爸 2024-09-06 18:23:56

我遇到了这个问题。我不小心删除了它

xmlns:tools="http://schemas.android.com/tools"

,这开始导致我的 XML 文件和 Java 文件中的整个项目出现构建错误。当我重新输入删除的内容后,它又起作用了:)

I had this problem. Accidentally I deleted this

xmlns:tools="http://schemas.android.com/tools"

which started causing build errors all over the project in my XML files as well as my Java files. As soon as I retyped what I deleted, it worked again :)

花想c 2024-09-06 18:23:56

好的,很明显,导致此问题的原因可能有很多。如果您使用的是 64 位 Linux 计算机,并且您只是第一次设置 ADT,则可能会遇到 R 未自动生成的问题。检查控制台选项卡,您可能会看到类似以下内容的错误:

 'No such file or directory' while attempting to get adb version from '/home/patrick/code/android-sdks/platform-tools/adb'

如果是这种情况,您需要安装 ia32-libs,使用类似:

 sudo apt-get install ia32-libs

请参阅此处了解详细信息:​​Android adb 未找到

OK so it's clear that there can be a lot of causes for this problem. If you're on a 64 bit linux machine and you are just setting up the ADT for the first time, you may get this problem where R is not automatically generating. Check the console tab and you may see an error similar to:

 'No such file or directory' while attempting to get adb version from '/home/patrick/code/android-sdks/platform-tools/adb'

If that's the case you need to install ia32-libs, using something like:

 sudo apt-get install ia32-libs

See here for details: Android adb not found

∞梦里开花 2024-09-06 18:23:56

我已经遇到过这个问题好几次了。我发现,如果我没有通过应用程序的名称导入包 R,例如,如果我的应用程序的包名称为 example.test,那么我发现我必须导入 example.test.R 才能访问任何资源。

如果未导入此资源,则返回的资源是默认资源,不包含我自己的资源。

话虽如此,如果您发现只获得默认资源列表,那么只需检查以确保您导入的是 application_package_name.R 而不是 android.R。

I've came across this problem a few times. I found that if I didn't import the package R through my application's name, for example, if my application had the package name example.test then I found that I had to import example.test.R in order to access any of the resources.

If this wasn't imported then the resources that where getting returned were the default resources with none of my own included.

With that said if you find that you are only getting a list of default resources then just check to make sure that you're importing application_package_name.R and not android.R.

绿萝 2024-09-06 18:23:56

我的问题是 appcompat 与我的项目不在同一目录中。

我在使用 Ant 编译我的应用程序时发现了这一点。

My problem was that appcompat was not in the same directory as my project.

I found this out when compiling my app with Ant.

萌化 2024-09-06 18:23:56

还要确保您拥有最新版本的ADT插件,并打开Android SDK管理器将所有SDK工具更新到最新版本。还要检查 Eclipse 本身的更新。不匹配的版本给我带来了问题。

Also make sure that you have the latest version of the ADT plugin, and open the Android SDK manager to update all SDK tools to the latest version. Check for updates in Eclipse itself as well. Non-matching versions caused the problem for me.

可是我不能没有你 2024-09-06 18:23:56

我的问题出在菜单文件内。
如果菜单文件中引用了 strings.xml 中不存在的字符串,编译器似乎不会警告您。检查菜单 XML 文件中的item是否引用了 strings.xml 中不存在的任何字符串。通常在android:title中。

My problem was inside a menu file.
The compiler doesn't seem to warn you if strings which do not exist inside strings.xml are referenced in menu files. Check if your items in your menu XML files reference any strings which do not exist in your strings.xml. Usually in android:title.

挽你眉间 2024-09-06 18:23:56

对我来说,这个错误是当我尝试复制现有的 res/menu 而不是使用 wizard 创建一个时引起的。

For me, this error was caused when I tried to duplicate an existing res/menu instead of creating one with the wizard.

捂风挽笑 2024-09-06 18:23:56

如果您使用 Maven,所有这些答案都不起作用。我的解决方案是添加

<genDirectory>${project.basedir}/gen</genDirectory>

到 android-maven-plugin 的配置部分。

All of these answers could not work if you use Maven. The solution for me was to add

<genDirectory>${project.basedir}/gen</genDirectory>

to the configuration section of android-maven-plugin.

你在看孤独的风景 2024-09-06 18:23:56

我找到了一个解决方案,为什么 R.class 在再次制作后不是由 Eclipse 制作的 - 2 clean、build 等。

问题出在 strings.xml 中:

<string name="hello">Hello World, HelloAutoComplete!</string>

<string name="app_name">HelloAutoComplete</string>

这些是在您创建项目时默认由 Eclipse 创建的。

当然,您正在根据自己的要求更改 strings.xml 。有时您会从代码中清除 string.xmls 这两行:

它在 AndroidManifest.xml 文件中产生问题:

<application android:icon="@drawable/icon"  android:label="@string/app_name">
<activity android:name=".HelloAutoComplete" android:label="@string/app_name">

因此它无法与 strings.xml 通信。

I found a solution why R.class is not made by Eclipse after making it again - 2 clean, build, etc.

The problem is here in strings.xml:

<string name="hello">Hello World, HelloAutoComplete!</string>

<string name="app_name">HelloAutoComplete</string>

These are by default created by Eclipse when you create projects.

Definitely you are changing the strings.xml for your own requirement. Sometimes you clear the string.xmls these two lines from your code:

It is making a problem in the AndroidManifest.xml file:

<application android:icon="@drawable/icon"  android:label="@string/app_name">
<activity android:name=".HelloAutoComplete" android:label="@string/app_name">

So it can't communicate with strings.xml.

献世佛 2024-09-06 18:23:56

我更改了布局 XML 文件名,后来发现 XML 文件(在本例中为小部件提供程序)仍然引用不存在的旧布局 XML,这阻止了 R 类的自动生成/更正。

I changed my layout XML file name and found out later that of the XML file (widget provider in this case) still refers to the old layout XML which doesn't exist, and that prevented the auto generation/correction of R class.

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