Android Studio添加了@ androidx.annotation和@ android.support.annotation问题

Khe*_*raj 20 android android-studio

我将Android Studio更新到版本3.2.0.

当我接受建议列表中的任何更改或alt +输入时.

  • 问题是@androidx.annotation自动创建的.
  • 因此建议方法有两个注释@androidx.annotation.Nullableandroid.support.annotation.Nullable.
  • 我不想手动删除这个不需要的建议,所以我该怎么办android.support.annotation呢?
  • androidx.annotation包是不是我的应用程序添加.我不想添加androidx.annotation.

见例子.

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    @Override
    public void onCreate(@androidx.annotation.Nullable @Nullable Bundle savedInstanceState, @androidx.annotation.Nullable @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有一些IDE设置来删除自动生成androidx.annotation

更新

我可以通过迁移到androidx(来自官方文档)摆脱这个问题,但我无法将所有项目迁移到androidx当前.所以需要一个解决方案

K.A*_*nam 12

AndroidX是用于向后兼容支持的新扩展库。在将来的新功能中,将在AnddroidX中解决向后兼容性支持。如本博客所述:https://android-developers.googleblog.com/2018/05/hello-world-androidx.html

28.0.0的稳定版本将是打包为> android.support的最终功能版本。所有后续功能版本将仅以> androidx打包的工件形式提供。

https://developer.android.com/topic/libraries/support-library/revisions

修订的28.0.0生产(2018年9月21日)

这是Support Library 28.0.0的稳定版本,适合在生产中使用。这将是> android.support包装下的最后一个功能版本,建议开发人员迁移到> AndroidX。

将您的应用程序从android.support迁移到androidx打包的依赖项

请参阅此链接,https://developer.android.com/jetpack/androidx/migrate

如果您依赖的库引用了较早的支持库,则Android Studio将更新该库以引用androidx而不是通过依赖项转换。依赖关系转换是由Android Gradle插件3.2.0-alpha14自动应用的,它重写了JAR和AAR依赖关系(以及传递性依赖关系)的字节码和资源,以引用新的androidx打包的类和工件。我们还将提供独立的翻译工具JAR。

因此,在步骤1(依赖关系转换)中:在gradle.properties文件中,将android.useAndroidX标志设置为true,并将android.enableJetifier标志设置为true。

android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

第2步(源重构)中:使用Android Studio 3.2及更高版本,您可以通过从菜单栏中选择“重构”>“迁移到AndroidX”来快速迁移现有项目以使用AndroidX。

从菜单重构为Androidx

  • 为什么会出现错误:**无法找到满足版本约束的“androidx.annotation:annotation”版本** (3认同)
  • [迁移到AndroidX](https://developer.android.com/jetpack/androidx/migrate)在此页面中的“工件映射”下搜索**旧版本工件**,例如您的情况,**' com.android.support:support-annotations'**。然后替换为尊重** AndroidX构建工件**此处为您的应用gradle依赖项中的androidx.annotation:annotation:1.0.0 **实现'androidx.annotation:annotation:1.0.0'** (2认同)

Iti*_*mon 6

只需annotationProcessor 'androidx.annotation:annotation:1.1.0'在您的build.gradle(Module: app)文件中添加为依赖项即可。


Ben*_* P. 5

Android Studio 有一个首选项“从导入和完成中排除”,您可以使用它来抑制androidx包的建议:

在此处输入图片说明

例如,在此屏幕截图中,我已将java.time包添加到此列表中,因为我希望我的自动完成建议org.threeten.bp.LocalDate但从不java.time.LocalDate

您应该能够添加androidx.annotation到此列表中以解决您的问题。

  • 还是一样的兄弟。我排除了`androidx.annotation`,当我输入一些方法并从建议中覆盖它时,它同时添加了`androidx.annotation`和`android.support.annotation`。 (3认同)

Nic*_*oso 4

Android Studio 只能导入类路径中包含的文件。这包括 Androidx 库 - 值得庆幸的是,如果这些类不存在,Studio 不会建议它们(这就是为什么我们其他人在我们的项目中看不到这个问题)。

话虽如此,这里的含义是您已经更新了 gradle 文件以包含包含以下内容的包:androidx.annotation.NonNull

com.android.support:support-annotations根据迁移指南androidx.annotation:annotation:1.0.0,因此您可以在模块 Gradle 文件中查找该组。

最简单的修复方法是从 Gradle 中删除该包,然后清理并重建。

注意:如果您想坚持使用支持注释,那么最新和最好的版本是 28,如此处所述