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

前端 未结 13 511
轻奢々
轻奢々 2020-12-16 09:32

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 whene

相关标签:
13条回答
  • 2020-12-16 10:09

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

    0 讨论(0)
  • 2020-12-16 10:09

    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

    0 讨论(0)
  • 2020-12-16 10:09

    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.

    0 讨论(0)
  • 2020-12-16 10:11

    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>
    
    0 讨论(0)
  • 2020-12-16 10:12

    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!

    0 讨论(0)
  • 2020-12-16 10:13

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

    0 讨论(0)
提交回复
热议问题