No warning about using Java 9 methods in Android projects

Viewed 213

I used java.time.Duration.toMinutesPart() method in my Android project targeting SDK 31 and from the IDE/compiler point of view everything was fine, on my emulator/device with Android 12 the app worked but then I've noticed crashes on our testing devices running lower Android versions

Fatal Exception: java.lang.NoSuchMethodError
No virtual method toMinutesPart()I in class Lj$/time/Duration; or its super classes (declaration of 'j$.time.Duration' appears in base.apk!classes2.dex)

I've checked the doc of the method and I've noticed that the method was added in Java 9 so the crash makes sense but what bothers me is that no part of the build pipeline (IDE, compiler, lint) warned me about using this method and now I'm scared how many methods I might be using that may crash in production. Is there any way to prevent this? I am using Kotlin and desugaring plugin

1 Answers

I've checked the doc of the method and I've noticed that the method was added in Java 9

Also as per Android documentation it's only available on API level 31 (Android S).

The possible causes:

  • The lint is accidentally suppressed from Android Studio: So, check that from Settings >> Editor >> Code Style >> Inspections

enter image description here

  • SDK of API 31 is not installed on Android Studio >> So make sure to install it from the SDK Manager:

enter image description here

  • The warning is suppressed from this code block using @SuppressLint("NewApi").

  • Any other unexpected Android Studio errors >> Try File >> Invalidate caches and restart.

Related