Android Studio says “cannot resolve symbol” but pr

2019-01-03 01:55发布

I'm importing twitter4j in AndroidStudio, using the following in my build.gradle:

dependencies {
  compile 'com.android.support:support-v4:18.0.+'
  compile files('libs/twitter4j-core-3.0.4.jar')
}

The project compiles fine, and I can create twitter objects without a problem. However, in Android studio, anything referencing that library shows "cannot resolve symbol" and displays in red. What do I need to do to get Android Studio to recognize the library?

I have already tried rebuilding the project, ./gradlew clean, and closing and re-opening my project.

25条回答
疯言疯语
2楼-- · 2019-01-03 02:35

If nothing else helped, you could do as Android Studio suddenly cannot resolve symbols recommends:

  • Exit Android Studio
  • Back up your project
  • Delete all the .iml files and the .idea folder
  • Relaunch Android Studio and reimport your project
查看更多
叛逆
3楼-- · 2019-01-03 02:36

It happened to me when I removed the whole files in .gradle/caches, then studio downloaded the dependences. The dependences not showed in the External Libraries but I can build App successfully. rebuild clean Invalidate and Restart have no effect. I solved the problem by the steps:

  • Close Android Studio completely
  • Remove .idea package in your project
  • Restart Android Studio
查看更多
神经病院院长
4楼-- · 2019-01-03 02:37

For mine was caused by the imported library project, type something in build.gradle and delete it again and press sync now, the error gone.

查看更多
虎瘦雄心在
5楼-- · 2019-01-03 02:37

Invalidate Caches / Restart didn't work for me this time.

Found a solution like this:

  1. Remove the compile *** or implementation *** line in build.gradle.

  2. Clean and rebuild. Errors should be raised here.

  3. Add the line in step 1 back to build.gradle.

  4. Clean and rebuild.

Weird...

查看更多
不美不萌又怎样
6楼-- · 2019-01-03 02:39

Invalidate Caches didn't work for me (this time). For me it was enough changing the gradle and syncing again. Or https://stackoverflow.com/a/29565362/2000162

查看更多
混吃等死
7楼-- · 2019-01-03 02:39

For my case working with AndroidStudio 2.2.3 the solution was to change the gradle wrapper/distribution to use local one in the Gradle Settings (despite of being "recommended"). enter image description here

查看更多
登录 后发表回答