6
votes

I have a really annoying problem since several days, after I tried to restore a previously working Android Studio project from a backup, after getting a new computer.

I have the "cannot resolve symbol" syntax highlighting problem with the classes from Android libraries such as android.support.v7.app, android.support.v4.app, and Google Play Services, which I have included as dependencies. But, the project builds fine and I can run the App on phone with no problems.

I have verified whether these jar files exist under SDK installation - for e.g. "\sdk\extras\android\m2repository\com\android\support\appcompat-v7\23.0.1".

My problem is quite similar to the ones reported in below posts:

  1. Android Studio says "cannot resolve symbol" but project compiles
  2. Android Studio cannot resolve symbol but code executes correctly
  3. Android Studio suddenly cannot resolve symbols

I have tried all the solutions provided in various stackoverflow discussions (i.e. Sync Gradle Project, Invalidate Cache/Restart, Clean/Rebuild, Deleting gradle generated files (.gradle, build, .idea directories etc.), downgrade to lower API and upgrade etc. But, no method is helpful.

I use Android Studio 1.3.2 and API 23. All build tools, support library etc. are up to date (tried with both v22.+ and v23.+ of the libraries).

Please do help if you have any other suggestions!

Extract from my build.gradle:

apply plugin: 'com.android.application'
android {
        compileSdkVersion 22
        buildToolsVersion '23.0.1'

defaultConfig {
    applicationId "com.myapp.fun"
    minSdkVersion 22
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:22.+'
    compile 'com.android.support:appcompat-v7:22.+'
}

Screenshots of the problems can also be found below. I think Android Studio is not able to import the dependent libraries for syntax highlighting, although the compiler is able to include them and build successfully.

Screenshot: Syntax highlight and auto-complete issue

3
try to use gradle cleanGabriele Mariotti
'com.android.support:support-v4:22.+' is very bad idea, you should avoid using + in dependenciesDamian Kozlak
Yes I agree. Just before I took this screenshot, I had already tried with precise library versions but the problem persisted.Jim C
Just a small commentary to say that appcompat-v7 includes support-v4.xiaomi
Thanks @GabrieleMariotti for the suggestion. In fact, that helped me find the real root cause. I was not able to run ' gradlew clean ' from the terminal due to a run-time exception "Could not determine wrapper version. at org.gradle.wrapper.GradleWrapperMain.wrapperVersion". Eventually, I found out that it's because I had a '!' symbol in my project path, and it seems many Java programs fail due to that reason! :-) I just renamed the path and it works fine now :-)Jim C

3 Answers

3
votes

The reason is because I had a '!' symbol in my project path. Apparently, as I learnt now, many Java programs fail due to this reason! :-)

I just renamed the path and it works fine now :-)


P.S.

Thank you @gabriele-mariotti for the gradlew clean suggestion. In fact, that helped me find the real root cause. I was not able to run gradlew from the terminal due to a RuntimeException:

Could not determine wrapper version. at org.gradle.wrapper.GradleWrapperMain.wrapperVersion

But that helped me learn about the effect of '!' symbol on paths, and to the root cause of the problem. Thank you all!

1
votes

You can trying:

  • Open Module Settings, change the value of compile SDK version (you can choose the minimum version number)
  • Wait for Android Studio to finish refreshing
  • Make sure you have imported:
    import android.support.v4.app.FragmentActivity

  • Open Module Settings again and you should change back compile SDK version , the original version number before you changed it.

  • Wait for Android Studio to finish refreshing
-1
votes

I had this very same problem with two libraries in our project, com.koushikdutta.ion and uk.co.senab.photoview which are used thoughout our multi-app project but are declared as dependencies deep down a library project. The symptoms were like this:

  • I could perfectly compile and run the application
  • The IDE stubbornly refused to find the classes of these libraries and no navigation to their source code was possible, lots of errors (red bars) were associated with all classes that used them; class usages were highlighted in red, no code suggestions, you name it.

My solution is as follows: first of all, close Android Studio. Then :

cd <your-project-folder>
mv .idea/ .idea-old/ 
mv <projectname>.iml <projectname>.iml-old

re-open Android Studio but select the option "Open an existing Android Studio project". Wait for the project to be reparsed and enjoy perfect cleanness.