Background
I work on some large project on Android, that was originally written 100% in Java. As I've converted some files to Kotlin, I've found at least 2 cases that the documentation and the code declare that some parameters of some functions should be non-null, so I've let them stay this way on Kotlin too:
- https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener#onFling(android.view.MotionEvent,%20android.view.MotionEvent,%20float,%20float)
- https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent,%20android.view.MotionEvent,%20float,%20float)
Meaning they look as such :
boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY);
The problem
I've noticed via Crashlytics that these 2 cases actually cause some devices to crash because of they are actually getting null values, as Kotlin is strict about such a thing (getting null when the parameter is set as non null).
What I've tried
I've reported about this on the issue tracker (here and here), and I tried to find how to force Kotlin to allow the parameters to be nullable, despite the code behind that says they are not. Sadly, it will always cause the error of "'onFling' overrides nothing", and won't let me build it.
I can't find how to do it, so for now, I use Java. Meaning something like this:
public class SimpleOnGestureListenerCompat extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(@Nullable final MotionEvent e1, @Nullable final MotionEvent e2, final float velocityX, final float velocityY) {
return super.onFling(e1, e2, velocityX, velocityY);
}
}
I've also added Crashlytics to tell me if the second parameter can be null too, which is why I've set it to be nullable as well.
The questions
- Is this the only workaround I can use for now? Can't I use Kotlin somehow for this? Maybe use some annotation that removes the nullable check during runtime for new classes that I will make, that extend these problematic classes?
- Is this allowed only because Android Studio doesn't handle nullability so well/strict for Java, or is this expected to always be a possible workaround for Java? It didn't even show me a warning for using the wrong annotations