80

I am trying out Android Studio instead of Eclipse. I install it and then start a completely new project and follow the wizard. I add no code of my own.

Then, I right-click to create a new component, a new Fragment:

enter image description here

and choose a new fragment:

enter image description here

and as soon as I do, I see compile errors:

enter image description here

... so I start googling and find out that I need to install and reference support library 4, and that I do. When I check the build.gradle (whatever that is, new to me coming from Eclipse), I see:

enter image description here

but I change that to

enter image description here

because they said so here. After recompiling and all that, the error is still there. I then reference the .jar-file directly, like this:

enter image description here

and again do recompile etc, but that doesn't help either.

This behaviour seems very strange to me. What am I missing here? This is the SDK Manager view:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Ted
  • 19,727
  • 35
  • 96
  • 154
  • Worked for me : https://stackoverflow.com/a/21100896/3209132 – Pramod Garg Apr 23 '18 at 14:00
  • 3
    **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [How To Ask](https://stackoverflow.com/help/how-to-ask) – Rob Dec 01 '19 at 12:48

25 Answers25

131

DID NOT WORK:

I have already had the following dependency in my build.gradle

implementation 'com.android.support:support-v13:26.0.2'

I have tried all of the following,

  • Invalidate Caches/Restart
  • Sync project with gradle files
  • Clean project
  • Rebuild project
  • gradlew clean

But, none of them worked for me.

SOLUTION:

Finally, I solved it by deleting "/.idea/libraries", and then synced with gradle and built again.

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
Owen Chen
  • 1,700
  • 1
  • 11
  • 10
102

The symptom of this problem is usually that the build works fine from the command line (which means your build.gradle file is set up right) but you get syntax highlighting errors in the IDE. Follow This Steps To Solve The Problem: Click on Tools from the toolbar usually at the top part of your IDE, and then navigate to Android then navigate to Sync Project with Gradle Files button. We realize it's less than ideal that the IDE can't just take care of itself instead of forcing you to manually sync at the right time; we're tracking progress on this in https://code.google.com/p/android/issues/detail?id=63151

Wale
  • 1,644
  • 15
  • 31
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • 5
    ...and not to be confused with the "Synchronize" option, which didn't work for me twice. ;) – Anonsage Jun 07 '14 at 00:38
  • 3
    This does not solve the syntax highlighting problem in my case. I have also tried the Invalidate Cache/Restart option many have suggested and a few other approaches as well but in vain. I have put details in another thread: see http://stackoverflow.com/a/32475754/3804695 – Jim C Sep 09 '15 at 09:50
  • 2
    This is in the `Tools/Android` directory in the tool bar. – Aggressor Mar 23 '16 at 16:57
  • This worked for me. Make sure you do not have any actual compile errors present otherwise it will not work. I recently switched from Eclipse -- never had to deal with crap like this in Eclipse, Makes me wonder why we are being "encouraged" to make the switch. – dazed Aug 23 '16 at 12:42
  • 4
    Tools/Android does not exist... Did it change? – ADL May 08 '18 at 04:13
  • @ADL It's under the file menu now! – TomCB May 10 '18 at 06:35
  • 'Sync Project with Gradle Files' option is under File menu in Windows – whoami Aug 23 '18 at 08:34
  • "Sync Project with Gradle Files" is moved from Tools option in Android Studio 3.2.1 version. Click on File and you will see Sync Project with Gradle Files option. – Bijesh Jan 07 '19 at 07:02
  • 1
    this answer is obsolete, go look at 2nd one from "Owen Chen" – Henley n Mar 10 '19 at 19:55
53

Try this may will help you.Go to "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.

Suresh Parmar
  • 815
  • 7
  • 15
  • 1
    All the other suggestions were not helpful for me, only this one fixed the problem for me, seems IntelliJ sometimes goes out of sync with caches and resyncing the Gradle build does not solve it! – centic Aug 05 '17 at 21:18
27

replace this line of code

import android.support.v4.app.FragmentActivity;

with

import androidx.fragment.app.FragmentActivity;

and you are done.

Divyanshu Kumar
  • 279
  • 3
  • 2
12

"File" -> "Invalidate Caches..."

worked for me.

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
11

I got the same problem since I updated to latest version of Android Studio 0.3.7. So you can try with my stuffs.

Ensure you have updated to latest version Android Support Repository - 3 Android Support Library - 19

As your attachment picture above, you did it already. Then adding the following setting to your build.gradle

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
}

One more thing: Please make sure your Android SDK is targeting to right SDK folder

Lam Do
  • 520
  • 4
  • 12
7

enter image description hereI found a shortcut: File - Project Structure - Tab:Dependencies Click on the green + sign, select support-v4 (or any other you need), click OK.

now go to your gradle file and see that is been added

Pedro Gonzalez
  • 1,429
  • 2
  • 19
  • 30
7

I'm using buck and it seems like by removing the /.idea/libraries in your project folder and sync gradle again works for me.

James Wong
  • 4,529
  • 4
  • 48
  • 65
6

1> File -> invalidate caches 2> Build-> Rebuild

its work for me

user5259011
  • 61
  • 1
  • 1
5

I'm running the most current version of AndroidStudio to date (11/10/2015) -- v1.4 (build AI-141.2288178, built on September 28, 2015) and I built my project and everything worked fine. Then after a few hours of my computer being inactive I came back, edited some code that had nothing to do with the support libraries and started seeing :

cannot resolve symbol 'fragmentactivity' and I was seeing red highlighted items related to fragmentactivity in the AndroidStudio editor.

The solution was to simply do a

Build...Rebuild Project...

I only mention this so others might see it and know it is happening out there.

raddevus
  • 8,142
  • 7
  • 66
  • 87
5

2021 here -- if, like me, you sadly have to just get some new code working with legacy junk that hasn't been touched in five+ years and still uses the support libs AND can't be changed before the deadline, you might try taking a look in gradle.properties for:

android.useAndroidX=true
android.enableJetifier=true

and commenting them if present. Android Studio 4.1.2 inserts them in new projects by default and they seem to get in the way of using support libs. Not surprising given the names, but the fact that they get auto-added to new projects is a little obnoxious.

CCJ
  • 1,619
  • 26
  • 41
4

Android studio has option to manage dependencies. Follow path.

  1. Click on File, then select Project Structure
  2. Choose Modules "app"
  3. Click "Dependencies" tab
  4. Click on the + sign, choose Library Dependencies
  5. Select support-v4 or other libraries as needed and click OK

FYI check link stackoverflow.com/a/33414287/1280397

Ranjitsingh Chandel
  • 1,479
  • 6
  • 19
  • 33
3

For me : Build->Clean Project solved this question

Michael Yang
  • 1,403
  • 2
  • 18
  • 27
2

File -> "Invalidate caches/Restart" This worked for me.

1

Hrrm... I dont know how many times this has happend so far: I test, try, google, test again and mess around for hours, and when I finally give up, I go to my trusted Stackoverflow and write a detailed and clear question.

When I post the question, switch over to the IDE and boom - error gone.

I can't say why its gone, because I change absolutely nothing in the code except for that I already tried as stated above. But all of a sudden, the compile error is gone!

In the build.gradle, it now says:

dependencies {
    compile "com.android.support:appcompat-v7:18.0.+"
}

which initially did not work, the compile errors did not go away. it took like 30 min before the IDE got it, it seems... hmm...

=== EDIT === When I view the build.gradle again, it has now changed and looks like this:

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile "com.android.support:appcompat-v7:18.0.+"
}

Im not really sure what the appcompat is right now.

Ted
  • 19,727
  • 35
  • 96
  • 154
1

To downplay the "magic" of this issue a little bit. You need an Internet connection after you make those changes. If for some reason required libraries cannot by downloaded, instead of giving an appropriate message (like "Failed to fetch libraries") you will simply get the same build error. Was struggling with this for an hour before realizing that my company's VPN blocked the repo.

Dennis K
  • 1,828
  • 1
  • 16
  • 27
1

For everyone who comes to this thread and builds with maven: add support-v4 dependency and build to download the repo.

Then add the library manuelly to your project:

Project Structure -> Libraries -> Add

And move to you're repo for the correct jar file: C:\Users\XXXXXXX\.m2\repository\com\google\android\support-v4\r7\support-v4-r7.jar

kaiser
  • 940
  • 1
  • 10
  • 25
1

Do sth. modification (just to gradle sync) over app level build.gradle and sync. Again, redo what you changed in build.gradle and sync. It should fix your problem.

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
1

I was facing the same issue. As it turns out in my build.gradle file there was this :

configurations {
    all*.exclude group: 'com.android.support'
}

Removing that fixed my issue. So guys, if doing all that and still your issue is not fixed, look for any exclude keywords in your gradle file.

Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
1

I encountered this issue and tried everything including File > Invalidate Caches but nothing worked. The reason this issue was happening for me was because I had external projects that were using a different AppCompat version to my main gradle file.

After I updated all gradle files to be the same version the compile error went away.

1

Closing and Re-Importing project worked for me.

Ankush Joshi
  • 416
  • 3
  • 9
1

For me the cause of problem was broken class path:

Library Gradle: com.android.support:support-v4:28.0.0@aar has broken classes path:   
/Users/YOUR_USER/.gradle/caches/transforms-1/files-1.1/support-v4-28.0.0.aar/0f378acce70d3d38db494e7ae5aa6008/res

So only removing transforms-1 folder and further Gradle Sync helped.

Flovettee
  • 895
  • 1
  • 8
  • 9
0

I found that if you have comments in your build.gradle it may break when you try to add a new support library. So make sure you check your build.gradle and see if it looks alright manually.

superarts.org
  • 7,009
  • 1
  • 58
  • 44
0

I'm currently using Android Studio Arctic Fox | 2020.3.1 Patch 3. I've tried the most upvoted answers here but sadly none worked for me. I looked down my ".idea/libraries" folder and saw no gradle file for "Fragments".

Here's how I fixed it:

I go to File > Project Structure > Dependencies > . There's a small "+" there to Add Dependency (or do Alt+Insert) > Library Dependency. Type on the search box of Step 1 the following: 'androidx.fragment' then hit Search. A list will show below a list of libraries and go click the first one, then Done. The window will disappear then click Apply. It will fetch Gradle Build Models then will automatically rebuild/update to your project.

0

My problem was with Glide. Pasting this in build.gradle and execute "Sync project with gradle files" works perfectly:

dependencies {

  implementation 'com.github.bumptech.glide:glide:4.12.0'
  // Glide v4 uses this new annotation processor -- see https://bumptech.github.io/glide/doc/generatedapi.html
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

}

Hope this help someone.