12

I have a project in Android Studio which compiles with Gradle. The general file structure is

MyProject
  - MyProjectSource
  - Libraries
     - ActionBarSherlock

My project is built with Gradle and will compile without issue. However in the lines within my activity where I include a component from ActionBarSherlock, Android Studio marks it as an error.

I have noticed that Android Studio has also generated .iml files. So my question: Does Android Studio ignore the gradle build when assessing whether something is valid syntax or not?

Obviously this is not a project blocking problem but it does slow velocity not being able to take advantage of the full set of tools Android Studio has to offer.

Thanks in advance for any help.

laochiv
  • 2,433
  • 1
  • 15
  • 17
  • I am having the same problem. However, I am using ABS directly from Maven. Even the [ABS Gradle Sample application marks a syntax error, although it compiles. http://s17.postimg.org/40tt64min/Screen_Shot_2014_01_21_at_4_05_28_PM.png However, in another computer everything is working perfectly. I can not think of different conditions in each scenario. – kikoso Jan 21 '14 at 15:15
  • @kikoso Do you want to add ActionBarSherlock library in your app? Please answer. – Zohra Khan Jan 21 '14 at 15:49
  • Hello @ZohraKhan. ActionBarSherlock is added to my project using MavenCentral. – kikoso Jan 21 '14 at 15:51

9 Answers9

13

These are not errors actually, this a bug in Android studio( 0.4.2 ) for resolving the symbol and fixed for the next release( 0.4.3 ).

To get Rid of these until the release do the following

  1. Clean your project using Build > Clean Project
  2. Close your Android Studio
  3. Take Backup of your project
  4. Delete all .iml files inside your project
  5. Delete content of .idea directory
  6. Open Studio and wait until it completes the sync with gradle

Also verify if you are seeing any error in File > Project Structure . If there is any error saying "Dependency Not Added "click on that and using red bulb on right side Add them to dependency.

UPDATE

Android Studio 0.4.3 is available on Canary Channel Now.

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • You might not have to follow all the steps; I got it to work after Step 4. – Nolan Akash Jun 17 '17 at 03:47
  • Ohh great, I answered it long back could be studio changes a lot after that. Thanks for the comments it will help many and I will also update my answer soon. – Piyush Agarwal Jul 06 '17 at 20:38
  • Small Update: Oh well. This still happens (today for me in 3.1) and actually never stopped. Your fix still works, though, thank you. – ThemBones Apr 12 '18 at 10:30
4

UPDATE: @laochiv, I have found that cleaning manually the folder .idea/libraries and clicking afterward on "Sync Project with Gradle Files" solves the problem. Apparently this Sync does not remove elements, but keep the old ones and add the current ones. Probably a bug to report :)


I am having the same problem. However, I am using ABS directly from Maven.

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:18.0.+'
}

Even the ABS Gradle Sample application marks a syntax error, although it compiles.

Problem with ABS

However, in another computer everything is working perfectly. I can not think of different conditions in each scenario.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
kikoso
  • 673
  • 1
  • 6
  • 17
  • I had same issue even on version 1.0.2 which is stable version :( But thanks your solution "delete content of library folder and sync again" fixed my issue. – Hesam Jan 26 '15 at 03:21
2

I don't know how to add a library using MavenCentral. I follow these steps to include a library

  1. Add library in App_Name/App_Name/libs folder.
  2. In App_Name/build.gradle add this

dependencies {

compile ---

compile files('libs/fileName.jar') }

  1. ./gradlew clean
  2. Make sure your Android studio is updated.
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
2

"Does Android Studio ignore the gradle build when assessing whether something is valid syntax or not?" Yes. Android Studio imports the project configuration but runs its own compiler as you are editing and saving (you can even change it to the Eclipse ECJ compiler). The true authority on the build is Gradle, and AS is still in beta. You can try to run the gradle resync or close and reimport the project. I have had to manipulate AS like this many times, especially after making edits to build.gradle.

Edit: I have found it better to use ActionBarSherlock as a gradle dependency rather than a library directory.

compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'

You will need to use the SDK manager and install the local maven repository.

But it is possible to eventually get rid of these compile errors, as long as your build.gradle doesn't create any extra sourceSets that AS doesn't understand; try not to deviate too much from the android gradle plugin examples.

Also, I've been running the Canary updates of AS for the latest and greatest.

Eric Woodruff
  • 6,380
  • 3
  • 36
  • 33
2

Go through the link and it worked for me.

1.) Choose File option from menu on left top side of android studio. 2.) Select the option : "Invalidate Cache/ Restart.." It will open an dialog. 3.) Click on the first button with option : "Invalidate and Restart" 4.) It will close the studio and restart it. Start indexing of the project. It resolved my problem

Go to File -> Invalidate Cache/ Restart -> Invalidate and Restart

Source https://www.youtube.com/watch?v=FX_gCTpqhwM

Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
0

I had this problem in Android Studio 4.2, it might be fixed on 4.3

I have noticed that adding some dependencies did not attach the classes.jar to the dependency, so Gradle recognises everything but the IDE doesn't. To fix it:

  • Open Module Settings of your project
  • Select the dependency that has the error
  • Click Add (+) for that dependency (bottom of the pop up)
  • Navigate to your-project/build/exploded-bundles/your-dependency/classes.jar
  • The IDE should recognise the files now

Hope it helps

balysv
  • 176
  • 7
0

I had this today and gradle update/sync didn't solve. But updating to latest version of Android Studio 2.2 update 5 fixed (was on 2.1).

scottyab
  • 23,621
  • 16
  • 94
  • 105
0

Here i'm talking about android studio version v3.1.2 where this same issue happened.In later android studio version(above 3.0.1) this issue again rises. So, I solved this by going through few things and here is how

  1. Close android studio first

  2. Navigate to your Project Folder

  3. Delete .gradle and .idea folder (.gradle and .idea is hidden files. so make sure you tick Show Hidden Files in your File Explorer option to see these files

  4. Open your project in android studio now

    This Time another issue (unsupported module) will arise, but don't worry. I got you

  5. Goto File in android studio and hit Sync Project with Gradle Files

Tada..! you're done now!

Al Walid Ashik
  • 1,545
  • 20
  • 32
0

Simplest Solution

  • Comment/Remove the dependency which is showing syntax error & Sync Project.
  • Uncomment/Add again & Sync Project.

Yeah! error gone...

Before

Before

After

After

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