0

I'm trying to use the List.of() method in one of my Android Studio projects but I'm running into this lint error when writing it:

Call requires API level 30(current min is 21):`java.util.List#of`

Note: I have already found a new solution that I haven't seen in other answers regarding this topic so I'm going to post it below to let others know.

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25

1 Answers1

0

I solved this error by adding the following annotation on top of the method that calls the List.of() method:

@RequiresApi(api = Build.VERSION_CODES.R)

This annotation specifies that the API level must be at least 30(which is what R represents). Check the Android Version Codes Page for the appropriate version code for your use case.

Here is the full method:

@RequiresApi(api = Build.VERSION_CODES.R)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
}
flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25
  • 1
    Note that while this gets rid of the compiler error, you will need more than this to get past the problem. Specifically, you need to use `List.of()` only on Android 11 and higher devices; the error indicates that your app is supporting devices older than that. – CommonsWare Nov 16 '21 at 17:42