Android Studio inline compiler showing red errors, but compilation with gradle works fine

你说的曾经没有我的故事 提交于 2019-11-26 22:33:52

问题


I've set up my project in Android Studio to use the Square Wire library, as per this question.

Build -> Compile Project works fine.

2:03:10 PM Compilation completed successfully in 31 sec

However, if I open one of my generated Protocol Buffer files, then the Square package is not being recognized.

Another symptom is that methods inherited from the Square Message class are showing as "Cannot resolve method" compile errors in the editor, even though they build and run fine.

Do I need to do something more than just adding the dependency in build.gradle for my module in my project?


回答1:


The following steps helps

  1. Close your project, and in your project folder delete project/.idea
  2. Delete ~/.gradle folder.

Open the project again. Everything should be solved.




回答2:


Thanks to free3dom for getting me on the right track here.

Diagnosis

The Sync Project with Gradle Files option in Android Studio seems to keep the Project Structure libraries up to date.

However, in my case there were some errors:

Expanding the (not very obvious) link showed the detail. My wire-runtime library was showing in an incorrect location:

This seemed to be a hangover from my original configuration where I had imported the JAR into a libs folder.

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

I subsequently changed it to be imported directly from Maven Central (as per here), and it seems that the old setting was never overwritten with the new one.

The Fix

The fix is to delete the library dependency from the Project Structure screen and then Sync Project with Gradle Files again.

More Detail

You can also see what path is being used to the resource by opening the .idea/libraries/wire_runtime_1_2_0.xml directly.

My broken one was:

  <component name="libraryTable">
  <library name="wire-runtime-1.2.0">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/MY_MODULE/libs/wire-runtime-1.2.0.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />

After deleting and re-adding the library it changed the root url to:

      <root url="jar://$USER_HOME$/.gradle/caches/artifacts-26/filestore/com.squareup.wire/wire-runtime/1.2.0/jar/44e7acbd5eb6f29698181dc3d67a60acf7efee80/wire-runtime-1.2.0.jar!/" />

Update

Android Studio v0.4.4 apparently fixes other bugs which have the same symptoms, so if you have an older version you may wish to upgrade.




回答3:


Simple Solution

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 Android Studio and restart it. Also, it will start re-indexing the project.

It resolved my problem

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

Source here for youtube




回答4:


1) Click on the gradle icon (right side on the android studio)

2) Click on the refresh button and wait for the issue to be resolved :D




回答5:


I was still having the same problem with Android Studio v3.2.1 constantly, and sorted out the problem just removing these two sub-folders of the .idea folder (no need to delete the whole .idea folder) while a gradle sync didn't work:

  • ./idea/caches
  • ./idea/libraries



回答6:


For me deleting .gradle and .idea folder of project solved the problem.




回答7:


My hacky fix for this -

  1. Open your build.gradle file
  2. Cut out everything inside dependencies{}
  3. Paste in notepad for super safe temporary keeping
  4. Choose 'Sync Now' in the build.gradle file
  5. Wait for Sync to fail
  6. Paste your dependencies back in
  7. 'Sync Now' again

Tried a lot of solutions and this was the only fix for me.




回答8:


I had this problem after upgrading to Android Studio 3.1. I solved the issue by upgrading all the components I use. So I changed the following entries in my build.gradle (app):

  1. Upgrade SDK (from "compileSdkVersion 25" to "compileSdkVersion 26" or maybe higher when you read this note):

    //compileSdkVersion 25

    compileSdkVersion 26

    Comment: This change forced program to download/install new sdk during next sync.

  2. Upgrade appcompat-v7:

    //implementation 'com.android.support:appcompat-v7:25.4.0'

    implementation 'com.android.support:appcompat-v7:26.1.0'

  3. Upgrade firebase:

    //implementation 'com.google.firebase:firebase-ads:11.4.2'

    implementation 'com.google.firebase:firebase-core:12.0.1'

    implementation 'com.google.firebase:firebase-ads:12.0.1'

  4. In PROJECT build.gradle, upgrade google-services:

    //classpath 'com.google.gms:google-services:3.1.0'

    classpath 'com.google.gms:google-services:3.2.0'

  5. Upgrade gradle (from 4.4 to 4.6). From gradle-wrapper.properties: distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip

After these changes, clean project, invalidate cache/restart, remove .idea/libraries content and resync (not sure about the order but ALL these activities solved the issue for me).




回答9:


Deleting libraries folder in .idea solved the issue for me. It is located in project's root/.idea/libraries.

I'm using Android Studio 3.1.4




回答10:


Just do this on your Android studio:

File -> Invalidate caches/Restart -> Invalidate and Restart

This worked for me. No need to clear ./gradle or ./idea folder




回答11:


Below steps when the Project is succesfully compiled but with unresolved classes (All classes usage will be in red color in all files)

Step 1. Delete .gradle file

Step 2. Delete .idea file

Step 3. Open new Android studio Window with some other android project on your system and close your Project with error

Step 3 . Now Open/Import the Project once again from the studio toolbar Android Studio ->File -> Open

Note: Now when you import the project the new .gradle and new .idea folders will be autogenerated with resolved dependencies by the compiler.




回答12:


If all above answers don't work for you. Just try update your dependencies to latest version. It worked for me.




回答13:


I tried most of the fixes above and what actually worked was deleting all the .AndroidStudio config folders and then restarting Android Studio.

  • Location: C:\Users\your.username



回答14:


I found easy way

  • Open app level build.gradle
  • Remove dependency which is showing error & Sync.
  • Again add dependency & Sync.

Error Gone!

Before

After




回答15:


Deleting the .idea/caches and .idea/libraries should sort out all the errors.




回答16:


It seems that everyone uses a different approach for getting around these issues. I'll suggest another one that I feel is a bit less invasive than the others. When I introduce changes from an aar library that I include, I will open up the "settings.gradle" file for the project.

Then cut out the main project module from the list of projects:

include ':mainproj',

and then sync your project. Then paste it back in and re-sync your project once more. I believe that this is the same as some of the manual deletion steps, but this lets you do so from the IDE a bit quicker and less painfully.




回答17:


These solutions didn't help me with the same problem in Android Studio 0.5.8.

I described my answer to the same question (I guess): https://stackoverflow.com/a/23891829/534698 I just deleted ~/.AndroidStudioPreview directory (in Ubuntu). I missed all my Android Studio settings but it works.




回答18:


In my case, for some reason, Android Plugin Version wasn't set. I did try clearing cache and removing .gradle and .idea as well, but it didn't work out and after finally wandering for hours, I found this.




回答19:


I had a similar issue before, invalidating cache and restarting didn't resolve it. I just did a Gradle Sync and everything worked fine.




回答20:


Solved the issue by cleaning project Build->Clean Project




回答21:


For me, I refer a jar in a module library. It cannot resolve the symbol in the jar. So I find the solution for my situation. Use the provided way.

provided fileTree(include: ['xxx.jar'], dir: '../moduleX/libs')



回答22:


All the answers above didn't work for me. My problem was that i had 3 modules, with 2 of them with the same package name.

It was ok for android studio <= 3.2.1.

For android studio > 3.2.1, ressouce was red.

I solved it like this mention here (i just made differents packages name for each modules ) : Any android Studio version > 3.2.1 show red resource but still compile




回答23:


I got the same issue cleared using the following steps:

  1. update the library version in gradle to any random number, eg: lib_1.0.0 -> lib_1.0.0.0

  2. gradle sync

  3. sync shows error

  4. revert the version change lib_1.0.0.0 -> lib_1.0.0

  5. gradle sync

Open the java file with imports to see the error cleared.



来源:https://stackoverflow.com/questions/20226912/android-studio-inline-compiler-showing-red-errors-but-compilation-with-gradle-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!