Android Studio 3.1 Cannot Resolve Symbol (Themes, Widget, attr, etc.)

前端 未结 16 1943
梦如初夏
梦如初夏 2020-11-29 01:21

I upgraded Android Studio today to 3.1, and now Android Studio says it cannot resolve symbols for most of the resources (for example ThemeOverlay in style

相关标签:
16条回答
  • 2020-11-29 01:54

    For some reason, those attributes are not found anymore in the 26 libraries. For increasing those libraries you have to also increase your compileSdk to 27. It is probable you will also have to download the sdk 27

    Short version, following goes on the app `graddle``

    android {
        compileSdkVersion 27
        //...
    }
    
    dependencies {
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support:design:27.1.0'
        //...
    }
    

    Long version, check all following files:

    gradle-wrapper.properties

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
    

    build.gradle (Project)

    buildscript {
    
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.0'
    
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    And finally build.gradle (app)

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "cl.cutiko.testingupdate"
            minSdkVersion 21
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        implementation 'com.android.support:design:27.1.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    
    0 讨论(0)
  • 2020-11-29 02:02

    For me the same symptom/error was caused by dragging and dropping a new image into 'drawable-xxhdpi' folder with a name Android Studio did not like - for example with a number or capital in the name.

    No useful error message was given just the 'cannot resolve symbol R' message.

    Even after synching the app and cleaning and rebuilding project, the issue remained that 'R' was not recognised with no other error indicated.

    Closing and importing the project as suggested in other answers did not work, which makes sense given the root problem.

    However, even though it appears this has not worked for others in the past, judging by other questions and answers on SO, deleting the new image from the drawable folder did work.

    Similarly, and a better solution, obviously, renaming the image to remove any characters that Android does not like in resource names and then doing a clean and/or rebuild project also worked.

    0 讨论(0)
  • 2020-11-29 02:02

    Problem #1

    My problem was related with how I used an alpha/beta version of Android Plugin

    File > Project Structure > Project

    check your Android Plugin Version

    I was using something called 3.3.0-beta, and yet in another project 3.1.0-alpha

    changing it to 3.2.0 seems to clear up my issue, don't forget to change the one in build.gradle(Project) too

    Problem #2

    I had an error in one of my .xml files. I missed a @string resource. Please check all your xml files, and make sure they are all correct (opening an xml file should activate Android Studio linting, scroll through the file, it should complain in red if something is wrong)

    Problem #3

    In build.gradle I imported different versions of the android support libraries. e.g.

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:27.1.0'
    

    Please make sure they are the same version. And make sure that Android Studio doesn't complain anything inside your gradle dependencies. It might be that some of your dependencies also imported different android support library versions.

    Steps to take after #1, #2 and #3

    Clean your project Build > Clean Project

    Invalidate cache and restart File > Invalidate Caches / Restart

    After restart, wait for gradle build to finish, then voila!

    0 讨论(0)
  • 2020-11-29 02:03

    Example

    setContentView(R.layout.activity_login_ativity);

    1. Put the cursor in R
    2. Click Alt+Enter
    3. Choose import class R

    Work for me :)

    0 讨论(0)
  • 2020-11-29 02:04

    The support library is out of sync.

    This error happens because the support library gets out of sync with your project. To get it back in sync you can do the following steps:

    1. Open your app module's build.gradle file
    2. Comment out the implementation lines for the support library. For me it looks like this:

      //implementation 'com.android.support:appcompat-v7:27.1.1'
      //implementation 'com.android.support:recyclerview-v7:27.1.1'
      //implementation 'com.android.support.constraint:constraint-la
      
    3. Sync your project with gradle. You will have some errors now. Don't worry about that.

    4. Uncomment the implementation lines that you previously commented out.

      implementation 'com.android.support:appcompat-v7:27.1.1'
      implementation 'com.android.support:recyclerview-v7:27.1.1'
      implementation 'com.android.support.constraint:constraint-la
      
    5. Sync your project with gradle again.

    The "Cannot Resolve Symbol" error should be gone now. This has worked for me several times on different projects.

    Note

    • If your project has multiple modules, then you need to follow the directions above for all of the modules at once.
    0 讨论(0)
  • 2020-11-29 02:04

    Just importing project again did not work for me.

    My solution was

    1. Delete .idea folder inside your project.
    2. Then close project by File> Close Project
    3. Then import project by File> New > Import Project OR from welcome Screen Import Project (Gradle, Eclipse ADT, etc.).

    See screenshot for more info

    Or

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