【问题标题】:importing NotNull or Nullable and Android Studio won't compile导入 NotNull 或 Nullable 并且 Android Studio 不会编译
【发布时间】:2013-05-17 10:54:29
【问题描述】:

当我向参数添加 @NotNull 或 @Nullable 注释时,Android Studio 会自动帮助我添加 /lib/annotations.jar 和导入

import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable;

但在此之后,项目将无法编译。如果我也删除注释但保留导入语句,项目仍然无法编译。 但是如果我删除 NotNull 和 Nullable 的导入语句,项目编译很好

Android Studio 给出一般错误:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

从 cmd 运行 gradlew compileDebug 会给出一点提示:

:Bugtester:compileDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

所以我检查了我的环境变量,它们设置为:

JAVA_HOME=C:\Program Files (x86)\Java\jre7
JDK_HOME=C:\Program Files\Java\jdk1.7.0_21\

有人对此有任何想法吗? (我是 java 和 android 编程新手)

【问题讨论】:

  • 我对 gradle 不熟悉,但是你应该在annotations.jar 上添加一个依赖到build.gradle。类似dependencies { compile files('full/path/to/annotations.jar') }
  • 谢谢,这解决了问题!我认为添加依赖项是 android studio 在我第一次编写 @NotNull 时自动执行的操作,它希望我添加一些内容。
  • 你可以在这里找到对这种行为的解释stackoverflow.com/questions/16622410/…
  • @vmironov 如果您将此添加为答案,我可以接受。

标签: java gradle android-studio


【解决方案1】:

只需为此目的导入 androidx.annotation.Nullable

【讨论】:

  • 谢谢!到今天为止,这是最简单的改变
【解决方案2】:

我猜,正确的方法是在你的 build.gradle 依赖项中使用来自 MavenCentral 存储库的原始 JetBrains 库(本示例中的最新可用版本):

dependencies {
    implementation 'com.intellij:annotations:+@jar'
    ...
}

【讨论】:

  • implementation 在较新的版本中使用,而不是 compile
  • 我使用实现 'org.jetbrains:annotations:16.0.2' - 对于 gradle-5.1.1,它对我有用。
  • 在这里完成新手 -> Android 4,即使在重新同步 gradle 和重建项目之后,这对我也不起作用。我仍然收到红色错误。
  • 从我的 build.gradle 文件中删除这个 Line implementation 'org.jetbrains:annotations:15.0' 解决了我的问题。
  • 它救了我的命 :)
【解决方案3】:

为 Maven 添加依赖:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>16.0.1</version>
</dependency> 

【讨论】:

    【解决方案4】:

    我正面临 gradle-5.1.1 - Android Studio 3.4 的问题以及类似的错误 - 编译失败;有关详细信息,请参阅编译器错误输出。任务 ':app:compileDebugJavaWithJavac' 执行失败。 等。在这种情况下,我使用

    implementation 'org.jetbrains:annotations:16.0.2'

    最重要的是错误将被清除。

    【讨论】:

    • 我希望这能奏效。但就像其他一切一样,我似乎无法摆脱这个错误。
    【解决方案5】:

    在 2015 年,您会使用 android.support.annotation 包中的注释。我只是不确定他们何时添加它们,就像在 docs 中那样,没有典型的句子“在 API 级别 X 中添加”,我不想阅读博客等。>]

    import android.support.annotation.NonNull;
    
    ...
    
    public void fooFighter(@NonNull Object honeyBunny){
       ...
    }
    

    【讨论】:

    • 2015 年的今天已经一去不复返了,在 2019 年的今天,使用 androidx.annotation:annotation:1.x.x :P
    • Smoooooooth! +1 ;)
    【解决方案6】:

    对于使用 Android 支持注释,如 - @Nullable、@NonNull 等。在您的项目中必须导入 android 支持注释库。只需将此行添加到 gradle 文件中的依赖项

    dependencies { compile 'com.android.support:support-annotations:+' }

    导入包到类。
    对于使用@Nullable 注解:

    import android.support.annotation.Nullable;
    

    对于@NonNull

    import android.support.annotation.NonNull;
    

    您可以在这里找到更多信息Android developers

    【讨论】:

      【解决方案7】:

      你也可以使用android自带的@NonNull&@Nullable

      • 将以下内容添加到 build.gradle

        dependencies {
            ...
            // For @Nullable/@NonNull
            compile 'com.android.support:support-annotations:+'
        }
        
      • 转到文件 / 设置项目设置检查并搜索“可为空”。

        Constant conditions & exceptions@NotNull/@Nullable问题中,点击Configure annotations并选择Android的annotations。

        您可能还想查看 Suggest @Nullable annotations…Constant conditions & exceptions 下,或者可能调整其他选项。

      【讨论】:

      • 您的回答解决了我的问题。只有一件事:显示“避免在版本号中使用 +”警告..​​.我已将我的版本设置为:compile 'com.android.support:support-annotations:23.1.1'
      • 应该在 Mac Android Studio -> 首选项上,但是我找不到项目设置或检查。我在 Android Studio 4 上。
      【解决方案8】:

      最好的办法是使用maven依赖

      1. 通过添加设置存储库路径

        repositories {
            maven {
                url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
            }
        }
        
      2. 添加依赖

        dependencies {
            compile 'org.jetbrains:annotations:7.0.2'
        }
        
      3. 利润!

      【讨论】:

        【解决方案9】:

        目前,Android API 或支持库中没有 NonNull/Nullable 注解。您也不能使用 IntelliJ,因为在使用 Gradle 构建时它们不在编译类路径中。

        但是,您可以轻松创建自己的。很简单:

        @Documented
        @Retention(RetentionPolicy.CLASS)
        @Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD})
        public @interface NonNull {
        }
        

        一旦关闭,您可以将 IntelliJ(或 Android Studio)配置为将其(以及匹配的 @Nullable)识别为用于 Null 检查的默认注释。

        为此,请进入 IntelliJ 首选项的 Inpections 下,然后在检查树中的 Probable Bugs 下找到 @NotNull/@Nullable problems 条目。

        选择项目,在右下角您将有一个“配置注释”按钮。这将允许您将自己的注释设置为 intelliJ 将用于此检查的注释。

        【讨论】:

        • 赞成,因为这个技巧不需要任何额外的第三方库
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        • 2015-01-06
        • 2014-09-11
        • 2022-01-01
        • 2020-10-07
        • 1970-01-01
        相关资源
        最近更新 更多