Android IntelliJ IDEA - 无法构建任何东西,总是得到“包 R 不存在”

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

IntelliJ IDEA - Can't build anything, always get "package R does not exist"

androidintellij-idea

提问by Mick Byrne

I'm trying to use IntelliJ IDEA to work on an Android app with a colleague that swears by it, but I'm unable to build any of the Android projects he sends me because whenever I try a build I get an error saying package R does not exist.

我正在尝试使用 IntelliJ IDEA 与一位发誓的同事一起开发 Android 应用程序,但我无法构建他发送给我的任何 Android 项目,因为每当我尝试构建时,我都会收到一条错误消息package R does not exist

I have been trying out everything I can think of. Examples and other things to note are:

我一直在尝试我能想到的一切。示例和其他需要注意的事项是:

  • Making sure I have a version of the local.propertiesfile pointing to the root of my Android SDKs folder.

  • Fiddling with various settings in the project structure dialog (I won't list them all).

  • The project I'm working on has two library projects with also use the Rclass and they work fine.

  • The intellisense within the IDE recognises the Rclass and comes up with the stuff I expect.

  • 确保我有一个local.properties指向我的 Android SDKs 文件夹根目录的文件版本。

  • 摆弄项目结构对话框中的各种设置(我不会全部列出)。

  • 我正在处理的项目有两个库项目,它们也使用R该类,并且它们工作正常。

  • IDE 中的智能感知识别R类并提出我期望的内容。

Any help would be greatly appreciated. I feel I'm missing something basic.

任何帮助将不胜感激。我觉得我缺少一些基本的东西。

采纳答案by xbakesx

I am brandnew to IntelliJ so I apologize if this doesn't work for you. I was experiencing the same problem and the solution was to add an AndroidFacetto your project.

的品牌新的IntelliJ所以我道歉,如果这并不为你工作。我遇到了同样的问题,解决方案是AndroidFacet在您的项目中添加一个。

(I am on a Mac, so directions here may be slightly off, and there are probably better ways to find this window, if so let me know!)

(我在 Mac 上,所以这里的方向可能会稍微偏离,并且可能有更好的方法来找到这个窗口,如果有的话请告诉我!)

  1. Right click your project and go down to Open Module Settings(seems F4 also works)
  2. Select Facetsin the far left column
  3. Click the + button
  4. Add an AndroidFacetto your project, and VOILA!
  5. You may need to import your Rs now, which could be a huge pain... so hopefully someone can chime in with an easy way to auto-import
  1. 右键单击您的项目并转到Open Module Settings(似乎 F4 也有效)
  2. Facets在最左边的列中选择
  3. 单击 + 按钮
  4. 将一个添加AndroidFacet到您的项目中,瞧!
  5. R现在可能需要导入你的s,这可能是一个巨大的痛苦......所以希望有人可以用一种简单的方法来自动导入

Hope this helps someone!

希望这可以帮助某人!

回答by Bulwinkel

Another possible solution to those listed here is to check that the package name in your AndroidManifest.xml matches that of your actual package:

此处列出的另一种可能的解决方案是检查 AndroidManifest.xml 中的包名称是否与实际包的名称匹配:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yourpackagename" >

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yourpackagename" >

My package was building fine until I refactored the package name, everything refactored except for the package name in manifest file (see above) which caused the error "Package R does not exist".

我的包构建良好,直到我重构包名称,除了清单文件中的包名称(见上文)导致错误“包 R 不存在”外,所有内容都被重构。

回答by richardwiden

If you run into this with a package that has been building before you need to "Rebuild". Build->Rebuild Project . Deleting gen does not do it since IDEA saves the actual generated R somewhere else.

如果您在需要“重建”之前使用已构建的包遇到此问题。构建->重建项目。删除 gen 不会这样做,因为 IDEA 将实际生成的 R 保存在其他地方。

回答by mkai

In my case, I added some classes from another project and I had to add the import to the R package from my actual project:

就我而言,我从另一个项目中添加了一些类,并且必须从我的实际项目中将导入添加到 R 包中:

import com.youcompanyname.yourprojectname.R;

回答by Jason

I've started playing around with IntelliJ since I've had issues with the new Android Studio and wanted to try something that was a little more stable (I've never really used either before). I ended up getting the dreaded “package R does not exist” error. In my experience, this is usually something messed up in an XML file. For me, the problem was actually with the AndroidManifest.xml file for an Android Library Module that I had created for ActionBarSherlock. For whatever reason, when it created the AndroidManifest.xml file it didn't bother to use the AndroidManifest.xml file that came with ABS and it put the following in:

我开始尝试使用 IntelliJ,因为我在使用新的 Android Studio 时遇到了问题,并想尝试一些更稳定的东西(我以前从未真正使用过)。我最终得到了可怕的“R 包不存在”错误。根据我的经验,这通常是 XML 文件中的一些东西。对我来说,问题实际上出在我为 ActionBarSherlock 创建的 Android 库模块的 AndroidManifest.xml 文件中。无论出于何种原因,当它创建 AndroidManifest.xml 文件时,它并没有费心使用 ABS 附带的 AndroidManifest.xml 文件,并将以下内容放入:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.ActionBarSherlockLibrary_4_3_1"
          android:versionCode="1"
          android:versionName="1.0">

    <application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">
        <activity
            android:name="ACTIVITY_ENTRY_NAME"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

When I created this Module I chose File > New Module... I then Selected Library Module under the Android section and for the Content Root I browsed to the location of the files for ABS (Other fields auto filled in correctly and I didn't change them). I then clicked finish and it prompted me if I wanted to keep the local file or the memory file for project.properties. I chose to keep the local file and it didn't prompt me for any others after that.

当我创建这个模块时,我选择了文件 > 新模块...然后我选择了 Android 部分下的库模块,对于内容根,我浏览到 ABS 文件的位置(其他字段自动填写正确,我没有改变它们)。然后我单击完成,它提示我是否要保留 project.properties 的本地文件或内存文件。我选择保留本地文件,之后它没有提示我输入任何其他文件。

I have no idea why it messed up the AndroidManifest.xml file (maybe what I did above was wrong...???), but to fix it I just copied the original AndroidManifest.xml that I had downloaded with the library and replaced the messed up AndroidManifest.xml file. After that I did Build > Rebuild Project and it resolved the “package R does not exist” errors.

我不知道为什么它搞砸了 AndroidManifest.xml 文件(也许我上面做的错了......???),但为了修复它,我只是复制了我用库下载的原始 AndroidManifest.xml 并替换了它搞砸了的 AndroidManifest.xml 文件。之后,我执行了 Build > Rebuild Project 并解决了“R 包不存在”错误。

One other thought, @xbakesx mentions the Facets settings, and for any modules you are using as libraries, you should make sure that "Library module" is checked under Facets for that Module. I had some issues with that at first when I was trying to figure out how to configure libraries in IntelliJ/Android Studio (I've really only used Eclipse).

另一个想法是,@xbakesx 提到了 Facets 设置,对于您用作库的任何模块,您应该确保在该模块的 Facets 下选中“库模块”。起初,当我试图弄清楚如何在 IntelliJ/Android Studio 中配置库时,我遇到了一些问题(我实际上只使用了 Eclipse)。

回答by Youngjae

I just restart IntelliJ and it magically solved. Try it first.

我只是重新启动 IntelliJ,它神奇地解决了。先试试。

回答by smac2020

Had same issue - the issue was the Manifest file was did not have the default Activity setup properly.

有同样的问题 - 问题是清单文件没有正确设置默认活动。

回答by random6174

To all the hopeless people who have reached this far down the answers: I feel your pain. I have spent an hour doing nothing but trying all those things above as well as proposals from other sources. I have checked all my xml resources twice, cleaned caches, rebuilt a dozen times, even restarted the entire machine. Here is what actually worked for me:

对于所有已经走到这一步的绝望的人,我的答案是:我感受到了你的痛苦。除了尝试上述所有内容以及其他来源的建议之外,我花了一个小时什么都不做。我已经检查了我所有的 xml 资源两次,清理了缓存,重建了十几次,甚至重新启动了整个机器。以下是对我真正有用的内容:

Under 'Build Variants'(lower left corner), I selected a different Build Variant(in my case release instead of debug) for the target that had the missing R problem.Solved it.

在“构建变体”(左下角)下,为缺少 R 问题的目标选择了不同的构建变体(在我的情况下是发布而不是调试)解决了。

回答by codewing

I've refactored the packagenameand intellij didn't update it in the manifest.

重构了包名,intellij 没有在清单中更新它。

So if you've refactored the packagename then go to the manifest and update the attribute "package" in the root of the xml file save and rebuild it. If any errors occur then it's probably a wrong packagename in the import (at least this was my case) so just fix them

因此,如果您重构了 packagename,则转到清单并更新 xml 文件根目录中的属性“package”,保存并重建它。如果发生任何错误,那么导入中的包名可能是错误的(至少这是我的情况)所以只需修复它们

hope this helps anyone

希望这可以帮助任何人

回答by Ken Montagna

In my case I had to delete the intellj compiler cache. on my windows machine it was somelike this:

就我而言,我不得不删除 intellj 编译器缓存。在我的 Windows 机器上,它有点像这样:

%USERPROFILE%\.IdeaIC12\system\compile-server\<my project>