As @matiash pointed out there is no one-size-fits-all solution.
For me a good compromise was to disable NullPointerException
warning for all calls to findViewById()
and keep it for other method calls. That way I take responsibility for checking the resource ids but still get a benefit of getting warnings if I make other mistakes.
To achieve this I added _ -> !null
method contract with Android Studio quick fix menu.
The action generated a following file file at android/support/v7/app/annotations.xml
in my project root.
<root>
<item name='android.support.v7.app.AppCompatActivity android.view.View findViewById(int)'>
<annotation name='org.jetbrains.annotations.Contract'>
<val val=""_ -> !null"" />
</annotation>
</item>
</root>
Update:
Unfortunately it doesn't survive Android Studio restarts :-(
External annotations are really useful so I hope I'll figure out a way to make Android Studio load them after restart.