Android Studio marks R in red with error message “

2019-01-02 17:35发布

In every project I've tried to create in Android Studio, all usages of R are marked in red with the error message "cannot resolve symbol R", but the compilation succeeds and the application runs. This is really annoying, as it blocks auto-completion and shows huge red waved lines all over my code.

I'm running Android Studio 1.7.0 and creating the project with default settings. A screenshot is attached:

Android Studio Screenshot

This is my build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

This is how the Project Structure looks like:

enter image description here

Any idea how to fix this?

30条回答
残风、尘缘若梦
2楼-- · 2019-01-02 18:06

In most cases, there is some problems with your resources, i.e. layouts, styles, etc. so try to find and fix the problem in your resources then clean your project.

查看更多
君临天下
3楼-- · 2019-01-02 18:07

I resolved it by :

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.
查看更多
还给你的自由
4楼-- · 2019-01-02 18:07

Had the same problem with android studio, I found out that build/generated/source/r contained only release folder and not debug. So for some reason debug build did not generate usable R class.

I solved it by opening the project iml file and fixed this line

<option name="RES_FOLDERS_RELATIVE_PATH" value="" />

to

<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />

resynced with gradle, commented out lines using R, run debug to generate the R, uncommented lines using R and run the debug again, fixed.

查看更多
时光乱了年华
5楼-- · 2019-01-02 18:07

Recently I import my project into Android studio, a part of R are marked in red with the error meesage "cannot resolve symbol R",but the compilation succeeds and the application runs.

Develop evirment:

  • IDE: Android studio 3.1.2
  • System: Mac Os

Solution:

step1:

Find the configuration file for Android studio
My path:

/Applications/Android Studio.app/Contents/bin/idea.properties

step2:

Change 2500:

idea.max.intellisense.filesize=2500  

to 5000 or more:

idea.max.intellisense.filesize=5000  
查看更多
无色无味的生活
6楼-- · 2019-01-02 18:10

Here is my temporary solution until I find a better one:

  1. Using Everything, find where R.java is created. In my case it was C:\Program Files (x86)\Android\android-studio\system\compiler\<project-name>.cb969c52\.generated\aapt\<module-name>.6badd9a4\production\com\<project-name>\<module-name>

  2. In the Project view, click the module and press F4. Ignore the warning.

  3. Click "+ Add Content Root" and select the aforementioned folder. Make sure it's marked in blue (as a source).

After I did this, suddenly all the warnings are gone. The problem is that if you collaborate with other people, the folder name is different on each machine so be careful when synchronizing.

查看更多
长期被迫恋爱
7楼-- · 2019-01-02 18:10

This worked for me:

  1. Close and reopen project

  2. Press " Sync Project with Gradle file " (is next to AVD manager icon in top menu) repeat 1-2 if Gradle could not Sync for first time. (mine worked in second time).

Worked with Android Studio 1.2.2

查看更多
登录 后发表回答