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

南楼画角 提交于 2019-11-26 12:23:35

问题


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 styles.xml or ?attr/actionBarSize). This doesn\'t seem to prevent me from building or running in an emulator so far, but these errors are making me nervous.

Has anyone else experienced this issue? How can I go about resolving it? I have tried syncing gradle and cleaning my project but it doesn\'t seem to help at all.

Any ideas?

I\'m using Android Studio 3.1 with Gradle version 4.4 and Gradle Plugin 3.1.0

EDIT: This issue affects updating to Android Studio 3.1.1, 3.1.2, 3.1.3, 3.1.4 as well as Gradle Plugin to 3.1.1, 3.1.2, 3.1.3, and 3.1.4. However, the answer below still works.


回答1:


Close the project and import it again. Worked for me today.




回答2:


Close and reopen project as existing Android Studio project




回答3:


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.



回答4:


After upgrading Android Studio, you can invalidate cache and restart.

File > Invalidate Caches / Restart…




回答5:


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'
}



回答6:


Closing a project and then opening as existing one does wonders!




回答7:


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




回答8:


You can try to close the project and exit Android Studio (only closing and reimporting it didn't do it for me). Launch Android Studio and open the project again. That worked for me!




回答9:


I closed and reopened project as existing Android Studio project (did not import it), and it worked! Thanks to Andrew Glukhoff 's comment.




回答10:


implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:26.1.0'

update your dependencies with the above in build.gradle(project)

and put the below code in build.gradle(Module:app)

classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'io.realm:realm-gradle-plugin:3.7.1'
classpath 'com.google.gms:google-services:3.1.0'



回答11:


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.




回答12:


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!




回答13:


I updated my gradle version to 4.10.1 and did "fix and reimport". There were no libraries folder under .idea before. And then it worked




回答14:


Similar error for me when I update to com.android.tools.build:gradle 3.3.1 and gradle-4.10.1

I have tried the following method but didn't work:

  1. Clean Project - Rebuild Project
  2. Invalidate Caches / Restart
  3. Check .xml error (I didn't find any errors about xml )
  4. Modify .iml file

I fix this error by modify com.android.tools.build:gradle 3.2.1 and Restart my mac , I didn't know which one can fix it, but it really work for me .




回答15:


How I solved it.:

I updated Android studio to version 3.2, and two steps solved this problem.

  1. Deleted from Android studio welcome page.

  2. Open project again.

Worked for me.




回答16:


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



来源:https://stackoverflow.com/questions/49523302/android-studio-3-1-cannot-resolve-symbol-themes-widget-attr-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!