105

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.

TG-T
  • 2,174
  • 3
  • 19
  • 20
  • possible duplicate of [Android Studio cannot resolve symbol but code executes correctly](http://stackoverflow.com/questions/18991161/android-studio-cannot-resolve-symbol-but-code-executes-correctly) – danben May 26 '15 at 00:36
  • year 2018, Android Studio 3+: sometimes, Android Studio and Gradle don't agree. solution here: https://stackoverflow.com/questions/50313885/android-studio-cannot-resolve-symbols-from-aar/50327343#50327343 – user1506104 May 14 '18 at 17:31
  • I had the same issue after importing an exercise project into Android Studio. When imported Android Studio gave me warnings and suggested to update the sdk version, so I followed the advice. After that something was not compatible anymore, so Android Studio suggested to change another version of something else. After following all these advices I had the same "cannot find symbol" issues. I reversed the changed, imported the project again and didn't follow the hints of Android Studio this time. After that the errors were gone. Be careful with blindly following the suggestions of that program. – David May 14 '18 at 21:14
  • Related post - [Android Studio suddenly cannot resolve symbols](https://stackoverflow.com/q/21100688/465053) – RBT Aug 06 '18 at 00:04

28 Answers28

245

No idea if this will work or not but my only thought so far: right click the jar file in file tree within AS and select "Add as library..."

EDIT: You can do "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.

EDIT 2: This fix should work for all similar incidents and is not a twitter4j specific resolution.

doydoy
  • 4,021
  • 3
  • 20
  • 33
  • I'm not getting that as an option when I right-click the jar. I'm running 0.2.13. – TG-T Oct 22 '13 at 13:35
  • This probably means you have already done this. Try File>Project Structure and confirm you see the .jar file in the modules node. – doydoy Oct 22 '13 at 13:50
  • Ah, yeah, it's in there. – TG-T Oct 22 '13 at 13:56
  • You can do "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this. Thats my last idea unfortunately. – doydoy Oct 22 '13 at 15:13
  • 16
    Invalidating the caches and restarting does not work for me. I have also tried reinstalling android studio and reimporting the project. This is very annoying... – clocksmith Nov 21 '13 at 00:20
  • Try ensuring you have downloaded the up to date support tools through sdk manager. – doydoy Nov 21 '13 at 20:16
  • 3
    The "File" -> "Invalidate Caches..." just saved my day. I wasted so much time on this issue. Why was this answer not a top search result? Thank you so much! – Eric Cochran Jun 29 '14 at 00:34
  • Not working for me. What did work is commenting out the lines where you had trouble -> successfully compiling the whole project -> commenting in the lines. I think there are problems with the gradle task order. – Geki Mar 09 '16 at 21:29
  • 2
    tossing in my gratitude – Yevgeny Simkin Apr 06 '18 at 15:22
34

Try changing the order of dependencies in File > Project Structure > (select your project) > Dependencies.

Invalidate Caches didn't work for me, but moving my build from the bottom of the list to the top did.

Nauraushaun
  • 1,484
  • 12
  • 10
  • Changing the order of dependencies helps me, I tried all the solutions given in the answers, only yours help me, thanks. – wqycsu Aug 05 '15 at 02:24
  • Moving only one dependency up did the trick for me! (Android Studio 3.0.1). – Sébastien Nov 30 '17 at 13:02
  • Yeah it's not important what dependency you move, just change the order of anyone, this works for Android Studio 3.3 too. – karique Feb 10 '19 at 20:57
12

This is what worked for me.

In the Project panel, right click on the project name, and select Open Module Settings from the popup menu.

then change the Compile SDK Version to the minimum version available (the minimum sdk version you set in the project). wait for android studio to load everything.

It will give you some errors, ignore those.

Now go to your java file and android studio will suggest you import

import android.support.v4.app.FragmentActivity;

Import it, then go back to Open Module Settings and change the compile sdk version back to what it was before.

Wait for things to load and voila.

Alastair
  • 6,837
  • 4
  • 35
  • 29
Georgi Dimitrov
  • 121
  • 1
  • 3
  • it's brilliant work. in my case, Android Studio reported me all the Classes of SDK cannot resolved(included String, Integer, TextView etc.). before that, aStudio also told me she cannot find out the android-14 SDK which I used to compile my projects. I ignored that so the problems occurred. finally, I found this answer and follow, change the compile sdk version then worked. thanks to save me time. – VinceStyling Aug 14 '15 at 08:13
  • `Now go to your java file and android studio will suggest you import` Not in my case – NineCattoRules Feb 02 '17 at 16:13
9

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.

Beeing Jk
  • 3,796
  • 1
  • 18
  • 29
6

I also had this issue with my Android app depending on some of my own Android libraries (using Android Studio 3.0 and 3.1.1).

Whenever I updated a lib and go back to the app, triggering a Gradle Sync, Android Studio was not able to detect the code changes I made to the lib. Compilation worked fine, but Android Studio showed red error lines on some code using the lib.

After investigating, I found that it's because gradle keeps pointing to an old compiled version of my libs. If you go to yourProject/.idea/libraries/ you'll see a list of xml files that contains the link to the compiled version of your libs. These files starts with Gradle__artifacts_*.xml (where * is the name of your libs).

So in order for Android Studio to take the latest version of your libs, you need to delete these Gradle__artifacts_*.xml files, and Android Studio will regenerate them, pointing to the latest compiled version of your libs.

If you don't want to do that manually every time you click on "Gradle sync" (who would want to do that...), you can add this small gradle task in the build.gradle file of your app.

task deleteArtifacts {
    doFirst {
        File librariesFolderPath = file(getProjectDir().absolutePath + "/../.idea/libraries/")
        File[] files = librariesFolderPath.listFiles({ File file -> file.name.startsWith("Gradle__artifacts_") } as FileFilter)

        for (int i = 0; i < files.length; i++) {
            files[i].delete()
        }
    }
}

And in order for your app to always execute this task before doing a gradle sync, you just need to go to the Gradle window, then find the "deleteArtifacts" task under yourApp/Tasks/other/, right click on it and select "Execute Before Sync" (see below).

Gradle window to configure task execution

Now, every time you do a Gradle sync, Android Studio will be forced to use the latest version of your libs.

nah0y
  • 141
  • 2
  • 6
5

When i lived to this problem(red color codes but they work correctly) in my project;

As first, i made it that (File -> Indicate Cashes) --> (Invalidate and Restart).

As last, i resync my build.gradle file in my app. After problem was resolved.

oguzhan
  • 2,073
  • 1
  • 26
  • 23
5

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...

ZhangLei
  • 393
  • 1
  • 7
  • 17
2

This problem happened for me when my Glass project was not using the SDK installed in Android Studio's default location. I was using another location I had previously from ADT, since I was trying to avoid re-downloading everything. Once I pointed the project back to the SDK location in Android Studio's install location the problem went away.

DaveTrux
  • 476
  • 5
  • 11
  • Yeah I guess that is also a good shout: by default android studio will decide all SDK's are installed in the default android studio install directory. This will need to be amended if anything doesn't follow that pattern. – doydoy Jan 15 '15 at 22:08
2

I had this problem for a couple of days now and finally figured it out! All other solutions didn't work for me btw.

Solution: I had special characters in my project path!

Just make sure to have none of those and you should be fine or at least one of the other solutions should work for you.

Hope this helps someone!

grAPPfruit
  • 2,041
  • 3
  • 20
  • 29
  • Please check if you have a project path which has special characters like ! (exclamation mark). In a similar problem that I experienced http://stackoverflow.com/questions/32477067/android-studio-cannot-resolve-symbol-syntax-highlighting-errors-although-bui, this was the root cause. Many Java applications seem not to tolerate such special characters (e.g. doing a 'gradlew clean' from the terminal would fail and throw a RunTimeException.). None of the other solutions posted online had helped me. But, once I had removed the ! from the path and did a clean build, Android Studio magically worked. – Jim C Sep 17 '15 at 01:16
  • WOW AMAZING! Thanks :) – Josep Escobar Jan 23 '17 at 23:38
1

I had the very same problem recently with Android Studio 1.3. The only working solution was to remove the .gradle and .idea folders and re-import the project into Android Studio.

ticofab
  • 7,551
  • 13
  • 49
  • 90
1

For those, who tried the accepted answer and have no success,

I tried also the accepted answer and did not work for me. After that, I updated my project with the repository and synchronized the project and the could not resolve warnings are gone.

drJava
  • 697
  • 2
  • 9
  • 24
1

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

Community
  • 1
  • 1
TomCobo
  • 2,886
  • 3
  • 25
  • 43
1

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
serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

In my case, I had created a new file using the GUI and defined an interface within the file. I kept getting this symbol error because the file was not a "java" file. It had no extension on it at all.

After modifying the file extension to ".java", the system correctly finds the symbols.

0

In my case the jar file did not have a META-INF/MANIFEST.MF file. After adding one, it worked!

Mahe
  • 759
  • 1
  • 6
  • 21
0

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

Hack06
  • 963
  • 1
  • 12
  • 20
0

This worked for me-- (File -> Indicate Cashes) --> (Invalidate and Restart).

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
0

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 you can invalidate caches and restart. This fixed it.

0

Change compile to implementation in the build.gradle.

Raulp
  • 7,758
  • 20
  • 93
  • 155
0

Changing the language injection settings worked for me.

Place the cursor on any of the red underlined code and Alt + Enter

enter image description here

Now select Language Injection Settings and a window will open as shown below.

enter image description here

Uncheck the selected option and click Ok

enter image description here

Hope this helps somebody.

Tricky Bay
  • 796
  • 9
  • 20
0

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
joe
  • 619
  • 6
  • 10
0

I have tried this but nothing worked for me:

  • Invalidate Caches / Restart
  • Changing the order of dependencies
  • Sync project with Gradle Files
  • Clean -> Rebuild Project

In my case, just do:

  • Delete all files in .idea/libraries folder
  • Rebuild Project
Conan
  • 3,074
  • 1
  • 22
  • 24
0

For me I updated gradle-wrapper.properties into 4.4

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

I found easiest way

Identify the library which is causing syntax error in Studio.

For Example if AppCompatActivity is showing error then you will perform below operation on AppCompat Dependency.

  • Remove dependency which is showing syntax error & Sync.
  • Add again & Sync.

That's It, error gone!

Before

Before

After

After

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

None of the answers above works for me.

After a lots of attempts, I upgraded com.android.tools.build:gradle in Project/build.gradle from 2.3.0 to 3.0.1, and it works.

I guess the version of the com.android.tools.build:gradle should be match to the version of AndroidStudio, and my AS version is 3.2.1

Wooby
  • 216
  • 2
  • 10
0

I tried all of the posted solutions and none of them worked for me. (Though invalidate caches does normally. This time it made no difference). After trying everything posted here the "Error cannot resolve" were still present. And the project did build and run successfully, only the IDE seemed to be confused, not the actual app or device.

My Version of Android Studio is 2021.3.1: enter image description here

The solution that did finally work for me was updating the "Android Gradle Plugin Version" configuration inside of File -> Project Structure.

It was originally 3.2.1 when it wasn't working. I updated it to 3.5.3 because I have another project that was working fine and it was on that setting.

enter image description here

After making this change and then sync'ing gradle the symbols not resolved error goes away and the IDE once again knows what those classes are.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
-1

Closing all files and closing Android Studio works for me.

Sayali Shinde
  • 311
  • 2
  • 6
-2

You need to restart Android Studio.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
george mano
  • 5,948
  • 6
  • 33
  • 43