简体   繁体   中英

How to enable Android lint check for the @Nullable annotation?

I've noticed that Android Studio will verify that @Nullable isn't being ignored in the code:

ex.

@Nullable MyObject getMyObject();
...

MyObject o = getMyObject();
o.method();

^ Method invocation 'method' may produce 'java.lang.NullPointerException'

This is enforced by the NullableProblems IntelliJ warning.

I would like to enforce this rule from gradle at build-time via lint rule. Does anyone happen to know if it's possible to enable something similar to that via gradle?

If the inspection in question was lint, there would be a way of configuring lintOptions inside your build.gradle to force builds to fail as in the answers to this question here

However, the inspection you are talking about is provided by the IDE itself in Android Studio. There is currently no way to stop builds for these inspections, as per this canonical answer from a IntelliJ developer Here is a link to the documentation for this static code analysis provided by IntelliJ. Within the analysis explained in the documentation, the specific name is "Constant conditions & exceptions":

This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations. Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, eg report possible NullPointerException errors.

I think the best you can do is change its severity so it appears as a warning (red underlined in the IDE). Do it like this, go to Preferences / Inspections / Probable Bugs and change the severity of "Constant conditions & exceptions" to warning:

Android Studio中Inspections的可能错误部分

Your project will still build if you ignore the warning, but it won't look nice:

调用时使用红色下划线获取@Nullable方法的结果

You'll have to set it up so that your team shares the same Android Studio code inspection settings. You can do that with a settings repository .

Please note also that you can enable the Commit Changes / Perform code analysis check box as well which will force a code analysis before the team commits.

在Android Studio中提交更改对话框

When you click commit, it performs the analysis first and finds the warning you have been talking about:

Android Studio中的代码分析对话框

Please also note that forcing a team to use inspections like this may not be the best option if NPEs are becoming problematic in the project. Instead, you can try options like discussing openly the problems that returning null generates and possible solutions. This excellent article on avoiding null in the Google Guava wiki is a great starting point.

If you want lint to throw an error when certain rules are broken in the source code, you can definitely configure the project in a way that the source compilation halts. I could have written the whole process but this link more or less summarizes the whole thing

Also go through the Android link doc

Note : Analyze option in Android Studio Menu mostly uses both Lint and built in Intellij Idea Code inspection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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