IntelliJ IDEA - Can't build anything, always g

2019-01-23 22:25发布

问题:

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.

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.properties file 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 R class and they work fine.

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

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

回答1:

I am brand new 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 Android Facet to your project.

(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!)

  1. Right click your project and go down to Open Module Settings (seems F4 also works)
  2. Select Facets in the far left column
  3. Click the + button
  4. Add an Android Facet to 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

Hope this helps someone!



回答2:

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

<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".



回答3:

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.



回答4:

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:

import com.youcompanyname.yourprojectname.R;


回答5:

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:

<?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.

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.

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).



回答6:

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



回答7:

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



回答8:

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:

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.



回答9:

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

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

hope this helps anyone



回答10:

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

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


回答11:

Try create new project and select "Create project from existing sources"



回答12:

Adding my $0.02 just in case someone else has the issue I did. In my case I had generated the "Hello world" application using IntelliJ and forgot to change the package name from "com.example." I then used IntelliJ to refactor the package name. This caused the error to start happening.

So I did a global search for "com.example" and it was found in the AndroidManifest file as mentioned by others. However, changing this did not fix the problem. "com.example" was also found in "workspace.xml" under the ".idea" directory. I changed all the occurrences their, did a rebuild and then it started working again!

This seems like a bug in IntelliJ.



回答13:

In Android Studio, my problem was when i copy pasted a class to use as a template to make another similar class, it asks to automatically import a bunch of stuff, most of which was not going to be related to the new class, so i said cancel. with it it left out import com.your.packagename.R; so R wasnt actually being imported into the file i was trying to use it in.

can be fixed with an alt-enter when clicking on R (it should be highlighted in red)