14

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?

Daniel Jette
  • 913
  • 1
  • 9
  • 23
  • In other words, you want gradle to refuse building if there is such a warning in the project? Or you just want gradle to log this in log output? – azizbekian Jan 27 '17 at 16:40
  • First step would be for gradle to acknowledge this situation. My command-line builds don't warn about this. But, yes, I would ultimately like to refuse to build if you don't check nullable values. – Daniel Jette Jan 27 '17 at 18:31
  • So you kind of want to abort compilation if a lint error is encountered ? – Dibzmania Feb 01 '17 at 22:26
  • What I'd like to have happen is the following: 1) developer adds @Nullable to a method in the code 2) another developer uses that method without checking for null 3) a lint check catches that mistake (like it already does with the Android Studio inspection) 4) the build fails – Daniel Jette Feb 03 '17 at 16:18
  • Hi Daniel. Would you consider a `findbugs` or `PMD` solution? If so, maybe you could edit your question to note that and then include those tags? I'm not sure whether it is possible but it might be closer to what you are looking for than what I put in my answer. – David Rawson Feb 07 '17 at 19:05

2 Answers2

9

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, e.g. 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:

the probable bugs section of Inspections in Android Studio

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

red underline on calling get on the result of a method that is @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.

commit changes dialog in Android Studio

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

code analysis dialog box in 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.

Community
  • 1
  • 1
David Rawson
  • 20,912
  • 7
  • 88
  • 124
1

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

Dibzmania
  • 1,934
  • 1
  • 15
  • 32
  • Thanks @Dibzmania. But I still don't think there is any way to stop the build for a non-lint inspection, right? Please let me know if there is – David Rawson Feb 05 '17 at 02:43
  • 1
    If by non-lint inspection if you mean the Intellij IDE inspection, no there is no way fail build on inspection errors. For reference check this SO link -http://stackoverflow.com/questions/13291432/force-intellij-to-fail-the-compilation-on-nonnull-violations – Dibzmania Feb 05 '17 at 02:58
  • Thanks - I added the link to that question in my answer . I think that resolves the issue once and for all – David Rawson Feb 05 '17 at 03:25