今天编译Android的时候,加入了一个新库,在编译的阶段报如下的错误:

����: ������ParametersAreNonnullByDefault
  �Ҳ���javax.annotation.ParametersAreNonnullByDefault�����ļ�
ע: ijЩ�����ļ�ʹ�ûѹ�ʱ�� API��
ע: �й���ϸ��Ϣ, ��ʹ�� -Xlint:deprecation ���±��롣
1 ������

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> 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. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 3s

其实发生在-Xlint:deprecation阶段。报错的信息很多,还有一些是乱码,不过我们主要看这一句:javax.annotation.ParametersAreNonnullByDefault

这个报错信息是说,我们在代码里面用上了annotation标签,但是又没有标签表进行编译,所以编译器无法知道该标签规则。

查找了下,Android里面引用annotation标签需要引入依赖库:

compileOnly 'com.google.code.findbugs:jsr305:3.0.2'

注意,只需要在编译阶段引入即可,因为只是标签依赖的错误,所以只会导致编译无法通过,不影响运行时。

参考文档:https://www.coder.work/article/5196990