QBoard » Artificial Intelligence & ML » AI and ML - R » Android Studio marks R in red with error message "cannot resolve symbol R", but build succeeds

Android Studio marks R in red with error message "cannot resolve symbol R", but build succeeds

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



      October 23, 2021 2:17 PM IST
    0
  • 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

      November 1, 2021 2:27 PM IST
    0
    1. Select the File > Project Structure menu option.
    2. Press OK on the message box (Project Structure changes don't update build.gradle).
    3. Select "Modules" on the "Project Settings" side pane.
    4. For each module (project), check that the "Excluded Folders" section does not contain the "build" folder. If they do, press the 'x' button next to the "build" folder.
    5. Press OK

    You should now see the "MyAppProject/MyApp/build/source" folder. This should contain the "r" folder (where the R.java file is generated). Here, you should either see "debug", "release" in blue (if you have a projectFlavour in your build.gradle, these will be under the "" folder). Within that debug/release folder should be your app's package name and then an R.java file that contains all your resource ids.

      October 27, 2021 1:45 PM IST
    0
  • 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.
      December 18, 2021 11:58 AM IST
    0
  • I had similar issue with 'com.android.tools.build:gradle:3.3.1', I tried all above solutions nothing worked. It was actually build:gradle 3.3.1 issue. I changed it to 3.2.1 then it worked.

    So it can be issue with build:gradle version.

      November 23, 2021 1:09 PM IST
    0