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:13

Just Click on Build -> Rebuild Project option in your Android Studio.

查看更多
余生无你
3楼-- · 2019-01-02 18:13

This error happens to me because there is an invalid input in my activity_main.xml file.

When you try to build or clean your project you will see error messages.

I resolved mine by reading those error messages, correct what is wrong in my xml file. then rebuild project.

查看更多
孤独寂梦人
4楼-- · 2019-01-02 18:15

I get bitten by this every now and then and there's another thing to try: match your compileSdk and buildSdk versions to the latest available.

I had changed my compileSdkVersion from 21 back to 19 because the Android Studio (1.1 canary channel) debugger was not tracing properly into prebuilt libraries. I found that it seemed to be tracing based on the buildToolsVersion (19.1.0), which I verified by clicking an error in the stack trace (and then choosing 19 instead of 21). Though I changed it directly in the build.gradle file, a graphical walkthrough is here: https://www.blogger.com/comment.g?blogID=4722727826918942842&postID=3875998513888772255 and I'll summarize:

  1. Android Studio IDE, select File | Project Structure.
  2. Choose your module, like "app"
  3. In Compile SDK field, choose the latest version
  4. Also choose the latest version in Build Tools

Confirm, and the project should automatically sync properly.

The lesson I'm getting from this is that they should both be the same.

查看更多
弹指情弦暗扣
5楼-- · 2019-01-02 18:16

If you are getting this error, then clean your project.

  1. Go to build on top of your android studio
  2. Choose the option clean project

The reason behind this is that by cleaning your previous .apk file that is present in your android studio project folder on your system is deleted.

查看更多
高级女魔头
6楼-- · 2019-01-02 18:17

Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!

I would also recommend checking all your gradle files to make sure those package names are correct. That shouldn't make everything red, but it will stop your gradle files from syncing properly.

查看更多
宁负流年不负卿
7楼-- · 2019-01-02 18:18

I ran into the same error, turns out that the manifest file had some errors. After fixing it, clicked Make Project again, it worked fine. Android studio sometimes do not give very helpful error messages.

查看更多
登录 后发表回答