1

My project uses Array#sort that requires API level 24 (Android 7.0)

_tmp.sort((left, right) -> { ... };

But I have also minSdkVersion 21 (Android 5.0)

When I run that code on Android 6.0 Emulator, the following exception is thrown:

... Caused by: java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;)V in class Ljava/util/List; or its super classes (declaration of 'java.util.List' appears in /system/framework/core-libart.jar)

I have found that other users asked about this exception: java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;) exception in sorting arraylist android

Any good way to Android Studio warns that .sort is not workable on OSes earlier than Android 7.0? like APIs having @RequiresApi?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { ... }

kenjiuno
  • 341
  • 5
  • 11

2 Answers2

0

Android studios on my computers warns my about this out of the box, not to sure why yours doesn't. Maybe you made changes to your lint warning configuration? If not, you should still be able to enable that there.

meeeee
  • 2,929
  • 3
  • 22
  • 25
  • I knew why... I have set `compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 }`. And then warns are gone... – kenjiuno Oct 05 '17 at 05:43
0

Sort method is annotated as @since 1.8

You can change Android Studio errors and warnings levels in Settings->Editor->Inspections

Try change Java->Java language level migration aids->usages of API which isn't available at her configure (description of it: "This inspection finds all usages of methods that have @since tag in their documentation.")

Leśniakiewicz
  • 874
  • 1
  • 10
  • 21
  • 1
    Hmm. I want to use only **Lambda expressions** introduced at [Supported Java 8 Language Features and APIs](https://developer.android.com/guide/platform/j8-jack.html#supported-features). It is done by adding `compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 }` to build.gradle. However it also allowed us to call API level 24 APIs like **Array#sort** without any API level validation on Android Studio. – kenjiuno Oct 05 '17 at 05:49
  • @kenjiuno did you find a way to actually see warnings? – gscaparrotti Nov 28 '20 at 19:33