Android Studio “cannot resolve symbol” but project

2020-02-17 08:37发布

The project compiles fine. However, Android studio 3.0.1 shows reference to one particular library in red and displays "cannot resolve symbol". Even single stepping into that library module doesn't work.

I already tried various options like 1) Invalidate cache/restart 2) deleted build directory 3) clean and rebuild but nothing seems to be working.

Interestingly, when Android Studio is rebuilding indexes after invalidate/restart, it momentarily shows that library references in while but then again in red.

10条回答
Rolldiameter
2楼-- · 2020-02-17 09:23

I tried Invalidate cache/restart or clean Project -> rebuild project. These didn't work for me.

The final solution was open Project window on the left side of IDE, under Project mode, delete .gradle and .idea folder, then sync, it works.

查看更多
forever°为你锁心
3楼-- · 2020-02-17 09:24

In my case changing the Gradle version worked.

In gradle-wrapper.properties

changing

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

to

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

and in build.gradle (project level)

changing

classpath 'com.android.tools.build:gradle:3.3.0'

to

classpath 'com.android.tools.build:gradle:3.2.1'
查看更多
家丑人穷心不美
4楼-- · 2020-02-17 09:25

This has happened to me a couple of times, there are a few ways you can resolve this problem;

  1. Sync project files with gradle in the File toolbar
  2. Use the Invalidate/Restart option in the File toolbar to invalidate the caches.
  3. Delete the .idea folder and restart Android Studio
查看更多
淡お忘
5楼-- · 2020-02-17 09:27

Following has resolved this for me.

  1. remove library from gradle cache (in case of linux, it's in ~/.gradle/caches).
  2. remove library dependency from the build.gradle and sync
  3. restart (maybe invalidate cache)
  4. add library dependency to build.gradle and sync
  5. clean and rebuild
查看更多
登录 后发表回答