Android Studio 无法解析符号但代码正确执行 [英] Android Studio cannot resolve symbol but code executes correctly

查看:21
本文介绍了Android Studio 无法解析符号但代码正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一个库 Jar 文件,并将其导入到我的 Android 项目中.我能够调用函数并使用此 Jar 文件中存在的类,但 Android Studio 一直告诉我它无法解析我正在使用的类的符号".请看下面的截图:

I have recently created a library Jar file that I imported in my Android project. I am able to call functions and use classes that are present in this Jar file, but Android Studio keeps telling me that it cannot "resolve symbol" of the class I am using. Please see the screenshot below:

点击此处查看全尺寸图片

代码构建并成功执行.函数 isThisThingWorking() 只返回 true,而这正是 boolean blah 设置的内容.

The code builds and executes successfully. The function isThisThingWorking() simply returns true, and that is just what boolean blah gets set to.

我尝试按下 Sync Project with Gradle Files 按钮并使用 Android Studio 的 File 中的 Invalidate Caches/Restart... 选项菜单,但这些都没有解决问题.我该怎么做才能让 Android Studio IDE 不显示 Cannot resolve symbol 'xxxSDK' 错误?

I have tried pressing the Sync Project with Gradle Files button and using the Invalidate Caches / Restart... option from Android Studio's File menu, but none of this solved the issue. What can I do to make the Android Studio IDE not display the Cannot resolve symbol 'xxxSDK' error?

推荐答案

对于大多数人来说,Senthil 的回答 会更合适.我将此作为已接受的答案,因为它解决了我的具体问题.

For most folks, Senthil's answer will be more appropriate. I am leaving this one as the accepted answer because it solved my specific problem.

我发现了问题 - 我的 SDK.jar 没有正确生成.它包含 .java 文件而不是 .class 文件.这解释了为什么 IDE 无法找到 SDK 类.Jar 中的包结构仍然正确,这就是为什么包名本身不是红色的原因.代码运行正常,因为编译器知道编译 .java 文件.

I found the issue - my SDK.jar was not generated correctly. It included .java files instead of .class files. This explains why the IDE was not able to find the SDK class. The package structure was still correct in the Jar, which is why the package name itself is not a red color. The code worked correctly, because the compiler knew to compile the .java files.

为了解决这个问题,我修改了我的 SDK 项目的 build.gradle 以包含 .class 文件,而不是 .java 文件, 在创建 Jar 时.包含这个新 Jar 而不是旧 Jar 解决了 IDE 问题.

To solve the issue, I modified my build.gradle of my SDK project to include .class files, instead of .java files, when creating the Jar. Including this new Jar instead of the old Jar fixed the IDE issue.

这篇关于Android Studio 无法解析符号但代码正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆