2

I'm calling findViewById and Android Studio (version 3.1, stable) prompts me to put an assertion:

enter image description here

When I select it, it adds the following line above the selected line:

assert v != null;

Great. I run the app, and the View v is null. However, assertion doesn't work and it jumps to the line below it in debugger:

enter image description here

How is this even possible? It was Android Studio's own suggestion, and I didn't change a thing. Its own recommendation/autogenerated code is not working. Am I missing something obvious?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Java assertions [are disabled by default](https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html). Which VM options are you running with? – Mick Mnemonic Jul 10 '18 at 00:31
  • @MickMnemonic it's whatever Android Studio 3.1 ships with. I haven't changed it. So, if it's disabled, why does Android Studio highlight the code and recommend me to add it? It just adds great confusion. – Can Poyrazoğlu Jul 10 '18 at 00:32
  • 1
    Haven't worked with AS, so unfortunately I don't know why the linter suggests this. Personally, I never use `assert` (and [here is a good discussion about some reasons why](https://stackoverflow.com/questions/4537609/is-it-good-practice-to-use-assert-in-java)). Instead I prefer _explicit_ (not disableable) validators such as [Guava Preconditions](https://github.com/google/guava/wiki/PreconditionsExplained) (might not be available in Android). – Mick Mnemonic Jul 10 '18 at 00:37
  • It is surprising why AS is behaving like that. However, if you don't mind looking beyond assertions, there are two things you can do here. – Rajan Prasad Jul 10 '18 at 05:10
  • 1
    1. Instead of assert statement, use something like `if (v == null && BuildConfig.DEBUG) Log.e("your_tag", "your_message"); else { mapView = v.findViewById (R.id.MapView);}` – Rajan Prasad Jul 10 '18 at 05:17
  • 1
    2. If you know the parent activity of the MapView, you can just call findViewById on the layout root of the parent activity. In or after Creation of activity, (basically once onCreate is called), you can rest assured that the root layout of the parent activity will never be null. So you need not check for `if(root !=null) mapView = root.findViewById(R.id.MapView);` – Rajan Prasad Jul 10 '18 at 05:18
  • 1
    3. You can have a look at ButterKnife and it can map all your required views with Java Objects at compile time, without you having to use `findViewById` at all. It reduces boilerplate and runtime code. So if your project is decently sized, you can maybe look at Butterknife. – Rajan Prasad Jul 10 '18 at 05:18
  • 1
    @Raymond232 thank you, I'll look at ButterKnife, it sounds good. But my main point is that why AS recommends me to take some action that does absolutely nothing in default settings in the first place. – Can Poyrazoğlu Jul 10 '18 at 10:21
  • That is still surprising. Because I put an assert statement in my code and it threw me this warning, " Assertions are unreliable in Dalvik and unimplemented in ART. Use BuildConfig.DEBUG conditional checks instead. less... (Ctrl+F1) Assertions are not checked at runtime. There are ways to request that they be used by Dalvik (adb shell setprop debug.assert 1), but note that this is not implemented in ART (the newer runtime), and even in Dalvik the property is ignored in many places and can not be relied upon. – Rajan Prasad Jul 10 '18 at 11:03
  • Instead, perform conditional checking inside if (BuildConfig.DEBUG) { } blocks. That constant is a static final boolean which is true in debug builds and false in release builds, and the Java compiler completely removes all code inside the if-body from the app. For example, you can replace assert speed > 0 with if (BuildConfig.DEBUG && !(speed > 0)) { \ throw new AssertionError() }. (Note: This lint check does not flag assertions purely asserting nullness or non-nullness; these are typically more intended for tools usage than runtime checks.) – Rajan Prasad Jul 10 '18 at 11:03
  • 1
    More info: https://code.google.com/p/android/issues/detail?id=65183 ." So AS in fact clearly warns me against assertions. I am surprised it suggests you to use them. – Rajan Prasad Jul 10 '18 at 11:04

1 Answers1

3

Apparently, (as stated in the comments) assertions are disabled by default but encouraged by Android Studio.

In this manner, the "problem" can be classified as a misleading UX issue rather than a technical problem.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389