热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidStudio说“无法解析符号”但项目编译-AndroidStudiosays“cannotresolvesymbol”butprojectcompiles

Imimportingtwitter4jinAndroidStudio,usingthefollowinginmybuild.gradle:我在AndroidStudio中导入

I'm importing twitter4j in AndroidStudio, using the following in my build.gradle:

我在AndroidStudio中导入twitter4j,在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?

项目编译得很好,我可以毫无问题地创建twitter对象。但是,在Android studio中,引用该库的任何内容都显示“无法解析符号”并以红色显示。要让Android Studio识别库,我需要做些什么?

I have already tried rebuilding the project, ./gradlew clean, and closing and re-opening my project.

我已经尝试重建项目,./ gradlew清理,关闭并重新打开我的项目。

19 个解决方案

#1


207  

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

不知道这是否有效但是到目前为止我唯一的想法:右键单击AS中文件树中的jar文件,然后选择“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.

编辑2:此修复程序应适用于所有类似的事件,而不是twitter4j特定的解决方案。

#2


26  

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.

Invalidate Caches对我不起作用,但将我的构建从列表底部移到顶部。

#3


9  

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.

然后将Compile SDK Version更改为可用的最低版本(您在项目中设置的最小sdk版本)。等待android studio加载所有内容。

It will give you some errors, ignore those.

它会给你一些错误,忽略它们。

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

现在转到你的java文件,android studio会建议你导入

import android.support.v4.app.FragmentActivity;

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.

导入它,然后返回打开模块设置并将编译sdk版本更改回之前的版本。

Wait for things to load and voila.

等待东西加载瞧。

#4


7  

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.

因为我的是由导入的库项目引起的,在build.gradle中键入内容并再次删除它并立即按下同步,错误消失了。

#5


3  

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.

最后,我在我的应用程序中重新同步我的build.gradle文件。问题解决后。

#6


3  

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

我的Android应用程序也遇到了这个问题,这取决于我自己的一些Android库(使用Android Studio 3.0和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.

每当我更新lib并返回应用程序,触发Gradle Sync时,Android Studio都无法检测到我对lib所做的代码更改。编译工作正常,但Android Studio在使用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).

在调查之后,我发现这是因为gradle一直指向我的libs的旧编译版本。如果你转到yourProject / .idea / libraries /,你会看到一个xml文件列表,其中包含libs编译版本的链接。这些文件以Gradle__artifacts _ * .xml(其中*是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.

因此,为了让Android Studio获取最新版本的库,您需要删除这些Gradle__artifacts _ *。xml文件,Android Studio将重新生成它们,指向您的库的最新编译版本。

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.

如果您不希望每次单击“Gradle sync”(谁想要这样做......)时手动执行此操作,您可以在应用程序的build.gradle文件中添加此小gradle任务。

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 

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同步之前始终执行此任务,您只需要转到Gradle窗口,然后在App / Tasks / other /下找到“deleteArtifacts”任务,右键单击它并选择“Execute”在同步之前“(见下文)。

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.

现在,每次进行Gradle同步时,Android Studio都将被迫使用最新版本的库。

#7


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.

当我的Glass项目没有使用安装在Android Studio默认位置的SDK时,我遇到了这个问题。我正在使用我之前从ADT获得的另一个位置,因为我试图避免重新下载所有内容。一旦我将项目指向Android Studio安装位置的SDK位置,问题就消失了。

#8


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!

希望这有助于某人!

#9


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.

最近Android Studio 1.3出现了同样的问题。唯一可行的解​​决方案是删除.gradle和.idea文件夹,然后将项目重新导入Android Studio。

#10


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.

我也尝试了接受的答案,并没有为我工作。之后,我使用存储库更新了我的项目并同步了项目,无法解决警告消失了。

#11


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

无效的缓存对我来说不起作用(这次)。对我来说,这足以改变gradle并再次同步。或者https://stackoverflow.com/a/29565362/2000162

#12


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.

在我的例子中,我使用GUI创建了一个新文件,并在文件中定义了一个接口。我一直收到此符号错误,因为该文件不是“java”文件。根本没有任何扩展。

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

将文件扩展名修改为“.java”后,系统会正确查找符号。

#13


0  

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

在我的例子中,jar文件没有META-INF / MANIFEST.MF文件。添加一个后,它工作了!

#14


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

对于我使用AndroidStudio 2.2.3的情况,解决方案是更改gradle包装器/分发以在Gradle设置中使用本地包装(尽管是“推荐”)。

#15


0  

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

这对我有用 - (文件 - >指示兑现) - >(无效和重启)。

#16


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.

最终解决方案是在IDE左侧打开项目窗口,在项目模式下,删除.gradle和.idea文件夹,然后您可以使缓存无效并重新启动。这解决了它。

#17


0  

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.

    删除build.gradle中的compile ***或implementation ***行。

  2. Clean and rebuild. Errors should be raised here.

    清洁和重建。这里应该提出错误。

  3. Add the line in step 1 back to build.gradle.

    将步骤1中的行添加回build.gradle。

  4. Clean and rebuild.

    清洁和重建。

Weird...

奇怪的...

#18


0  

If nothing else helped, you could do as Android Studio suddenly cannot resolve symbols recommends:

如果没有其他帮助,你可以做,因为Android Studio突然无法解析符号建议:

  • Exit Android Studio
  • 退出Android Studio
  • Back up your project
  • 备份您的项目
  • Delete all the .iml files and the .idea folder
  • 删除所有.iml文件和.idea文件夹
  • Relaunch Android Studio and reimport your project
  • 重新启动Android Studio并重新导入您的项目

#19


-2  

You need to restart Android Studio.

您需要重新启动Android Studio。


推荐阅读
author-avatar
mobiledu2502920897
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有