热门标签 | 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。


推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文详细介绍了macOS系统的核心组件,包括如何管理其安全特性——系统完整性保护(SIP),并探讨了不同版本的更新亮点。对于使用macOS系统的用户来说,了解这些信息有助于更好地管理和优化系统性能。 ... [详细]
  • PHP 5.5.0rc1 发布:深入解析 Zend OPcache
    2013年5月9日,PHP官方发布了PHP 5.5.0rc1和PHP 5.4.15正式版,这两个版本均支持64位环境。本文将详细介绍Zend OPcache的功能及其在Windows环境下的配置与测试。 ... [详细]
  • NVIDIA Titan RTX深度评测
    NVIDIA的Titan RTX被誉为当前最强大的桌面显卡之一,其卓越的性能和高昂的价格吸引了众多专业人士和技术爱好者的关注。本文将详细介绍Titan RTX的技术规格、性能表现及应用场景。 ... [详细]
  • YB02 防水车载GPS追踪器
    YB02防水车载GPS追踪器由Yuebiz科技有限公司设计生产,适用于车辆防盗、车队管理和实时追踪等多种场合。 ... [详细]
  • 本文探讨了前端包管理器的核心功能,包括注册机制、文件存储、上传下载、以及依赖分析等关键特性,并介绍了几种流行的前端包管理工具。 ... [详细]
  • Qwik:一款创新的JavaScript框架,致力于提升网页应用的速度与响应性
    Qwik是一款新的JavaScript框架,旨在通过其独特的可恢复性机制,显著提高网页应用的加载速度和用户体验。 ... [详细]
  • mysql 分库分表策略_【数据库】分库分表策略
    关系型数据库本身比较容易成为系统瓶颈,单机存储容量、连接数、处理能力都有限。当单表的数据量达到1000W或100G以后,由于查询维度较多, ... [详细]
  • ArcBlock 发布 ABT 节点 1.0.31 版本更新
    2020年11月9日,ArcBlock 区块链基础平台发布了 ABT 节点开发平台的1.0.31版本更新,此次更新带来了多项功能增强与性能优化。 ... [详细]
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社区 版权所有