importing NotNull or Nullable and Android Studio won't compile
当我将@NotNull或@Nullable批注添加到参数时,Android Studio会自动帮助我添加/lib/annotations.jar并导入
1 2 | import org.jetbrains.annotations.NotNull import org.jetbrains.annotations.Nullable; |
但是之后,该项目将无法编译。 如果我也删除了注释但保留了import语句,则该项目仍然无法编译。 但是,如果我删除了NotNull和Nullable的import语句,则项目编译正常!
Android Studio出现一般错误:
1 2 3 4 5 6 7 8 9 | 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运行
1 2 3 4 5 6 7 8 9 10 11 12 | :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 |
因此,我检查了环境变量,并将它们设置为:
1 2 | JAVA_HOME=C:\\Program Files (x86)\\Java\\jre7 JDK_HOME=C:\\Program Files\\Java\\jdk1.7.0_21\\ |
有人对此有任何想法吗? (我是Java和android编程的新手)
我想,正确的方法是在build.gradle依赖项中使用MavenCentral存储库中的原始JetBrains库(此示例中的最新可用版本):
1 2 3 4 | dependencies { implementation 'com.intellij:annotations:+@jar' ... } |
您还可以使用android自己的
-
将以下内容添加到build.gradle中:
1
2
3
4
5dependencies {
...
// For @Nullable/@NonNull
compile 'com.android.support:support-annotations:+'
} -
转到文件/设置→项目设置→检查,然后搜索"可为空"。
在"常量条件和例外"和" @ NotNull / @ Nullable问题"中,单击"配置注释",然后选择Android的注释。
您可能还想查看"常量条件和例外"下的"建议@Nullable批注",或者可能要调整其他选项。
对于使用Android支持注释,例如-@ Nullable,@ NonNull等。在您的项目中必须导入android支持注释库。只需将此行添加到gradle文件中的依赖项
compile 'com.android.support:support-annotations:+'
}
并导入包到类。
对于使用@Nullable注释:
1 | import android.support.annotation.Nullable; |
对于@NonNull
1 | import android.support.annotation.NonNull; |
您可以在这里找到更多信息Android开发人员
目前,Android API或支持库中没有NonNull / Nullable批注。您也不能使用IntelliJ,因为在使用Gradle进行编译时,它们不在编译类路径中。
但是,您可以轻松创建自己的。很简单:
1 2 3 4 5 | @Documented @Retention(RetentionPolicy.CLASS) @Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD}) public @interface NonNull { } |
一旦关闭,您可以将IntelliJ(或Android Studio)配置为将其(和匹配的
为此,请进入
选择该项,然后在右下角有一个"配置注释"按钮。这将允许您设置自己的注释,因为该注释将用于此检查。
在2015年,您将使用android.support.annotation包中的注释。我只是不确定他们何时在文档中添加了它们,因为文档中没有典型的句子" API级别X中添加",而且我不喜欢阅读博客等。>]
1 2 3 4 5 6 7 | import android.support.annotation.NonNull; ... public void fooFighter(@NonNull Object honeyBunny){ ... } |
我遇到了gradle-5.1.1的问题-Android Studio 3.4和类似的错误-编译失败;有关详细信息,请参见编译器错误输出。任务':app:compileDebugJavaWithJavac'的执行失败。等。在这种情况下,我使用
1 | implementation 'org.jetbrains:annotations:16.0.2' |
最重要的是,错误将被清除。
对于Maven添加依赖项:
1 2 3 4 5 | <dependency> <groupId>org.jetbrains</groupId> annotations</artifactId> <version>16.0.1</version> </dependency> |
最好的方法是使用Maven依赖项
通过添加来设置存储库路径
1 2 3 4 5 | repositories { maven { url"https://repository.jboss.org/nexus/content/repositories/thirdparty-releases" } } |
添加依赖
1 2 3 | dependencies { compile 'org.jetbrains:annotations:7.0.2' } |
利润!