简体   繁体   English

Android Studio 更新后无法解析符号“NonNull”

[英]Cannot resolve symbol 'NonNull' after Android Studio update

I recently updated Android Studio.我最近更新了 Android Studio。 Afterwards it seems to no longer being able to resolve some support annotations, such as @NonNull or @Nullable .之后它似乎不再能够解析某些支持注释,例如@NonNull@Nullable It did work just fine before updating.在更新之前它确实工作得很好。 However, everything is still compiling and I am able to execute Code, but still it shows the symbol cannot be resolved message at my imports.但是,一切仍在编译中,我可以执行代码,但它仍然在我的导入时显示符号无法解析消息。

What I have already tried so far:到目前为止我已经尝试过的:

  1. Included the dependency in build.gradle (Module: Application):在 build.gradle 中包含依赖(模块:应用程序):

     compile 'com.android.support:support-annotations:27.0.0'
  2. I have invalidated Caches and restarted我已使缓存无效并重新启动
  3. Cleaning and rebuilding the project清理和重建项目
  4. (Edit) Synched Gradle (编辑)同步摇篮

I have no idea why this is not working and would really appreciate your help.我不知道为什么这不起作用,非常感谢您的帮助。

As of android api 29 ('targetSdkVersion' 29) com.android.support has been moved to 'androidx' library.从 android api 29 ('targetSdkVersion' 29) 开始,com.android.support 已移至 'androidx' 库。

In your build.gradle file(app level), dependencies block use...在您的build.gradle文件(应用程序级别)中,依赖项块使用...

dependencies{ 
    ...
    implementation 'androidx.annotation:annotation:1.1.0' 
}

And in your .java files use import as follows:在你的.java文件中使用 import 如下:

import androidx.annotation.NonNull;

Again build your project!再次构建您的项目!

Same thing happened to me.同样的事情发生在我身上。 And I was stuck for quite some time--what a shitty trick to play on someone for keeping Android Studio up-to-date.我被困了很长一段时间——为了让 Android Studio 保持最新状态而对某人玩真是太糟糕了。 And simply rebuilding the project was NOT even close to working.简单地重建项目甚至还没有接近工作。

Apparently some changes were made to gradle that prevents older programs from compiling properly.显然,对 gradle 进行了一些更改,以防止旧程序正确编译。

I had to create a new project and copy all my files into the new project.我必须创建一个新项目并将我的所有文件复制到新项目中。 Yeah, it bites big hairy green ones , but it was what I had to do to get it to work.是的,它会咬大毛绿色的,但这是我必须做的才能让它工作。

I hope someone comes up with a better answer than this!我希望有人想出比这更好的答案! (And I'd love to "talk" for a few minutes with the idiot that made this change! This is not the first time this has happened--nor the 2nd, the 3rd, 4th, etc etc.) (我很想和做出这个改变的白痴“谈谈”几分钟!这不是第一次发生这种情况——也不是第二次、第三次、第四次等等。)

!!! !!! NEW INFO !!!新信息!!!

Just started a new project and wham, this happened again.刚开始一个新项目,然后又发生了。 And it was using the master/design template.它正在使用主/设计模板。 Yup, exactly as Android Studio produced the code there are errors in ItemListActivity.java and other files because of the NonNull not importing correctly.是的,正如 Android Studio 生成的代码一样,由于 NonNull 未正确导入, ItemListActivity.java和其他文件中存在错误。

Sooooo, I went to the app build.gradle file and looked at all the dependency warnings. Sooooo,我去了应用程序 build.gradle 文件并查看了所有依赖项警告。 Why did the project generator use old dependencies?为什么项目生成器使用旧的依赖项? Hmmm.嗯。 So I updated all the dependencies to use the latest.所以我更新了所有依赖项以使用最新的。 "Sync Now." “立即同步。” Wow, it worked!哇,它奏效了!

Here are the lines I updated:以下是我更新的行:

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

Of course, you'll have to change the numbers to reflect the current versions.当然,您必须更改数字以反映当前版本。

Again, it boggles the imagination that straight-from-the-box templates have such glaring errors in them.再一次,令人难以置信的是,开箱即用的模板中存在如此明显的错误。 Someday I'm going to take a journey to Mountainview and tie some shoelaces together.总有一天我会去山景城旅行,把一些鞋带系在一起。

I faced similar issue recently with one library import and to fix this issue I had to make chances to import statement as below:我最近在导入一个库时遇到了类似的问题,为了解决这个问题,我不得不尝试导入如下语句:

Had to change following two imports.必须在两次导入后进行更改。

From:从:

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

To:到:

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Go to Build --> Rebuild Project.转到构建 --> 重建项目。 AS will refresh Gradle dependencies and your project too. AS 也会刷新 Gradle 依赖项和您的项目。

Click on the red bulb icon and the select add annotation to classpath.单击红色灯泡图标,然后选择向类路径添加注释。 It will resolve the issue.它将解决问题。

如果您使用的是 Android Studio,请使用Refactor -> Migrate to AndroidX选项并执行迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM