Method reference requires API level 22

Viewed 527

Based on Use Java 8 language features method references are compatible with any minSdkVersion, then why Method reference requires API level 22?

Currently, I'm using Android Studio 3.2.1 with com.android.tools.build:gradle:3.2.1 and JDK 1.8, in build-gradle I have:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Example

public class SimpleBundleEntry<V> extends BundleEntry<V> {

    public SimpleBundleEntry(String key, V value, 
                            BundleWriter<V> writer, BundleReader<V> reader) 
    {/*init*/}

    // ...

    public interface BundleReader<V> {
        V readValue(Bundle bundle, String key);
    }

    public interface BundleWriter<V> {
        void writeValue(Bundle bundle, String key, V value);
    }
}

Problematic code

public static BundleEntry<Boolean> ofBoolean(String key, Boolean value) {
    return new SimpleBundleEntry<>(key, value,
            Bundle::putBoolean, // <------------------- PROBLEM HERE
            (bundle, k) -> bundle.getBoolean(k));
}
2 Answers

I don't know what method you are calling but there are two different things here.

Android is built on java, which means you have a java version installed on your phone to run the OS.

As java gets updates, some methods are added to the framework and so there are methods that exist in 1.8 that don't on 1.7

But Android is also a platform that get updates, at this time we are the 28th version (https://developer.android.com/studio/releases/platforms).

Each one of this version has a new set of methods that dont exist on the previous one.

So if you are calling a method which has been introduced in Android 22 and you are running your application on an API 16 phone, this will lead to a crash.

To prevent this, you'll have to surround the code that requires Android API 22 by

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
   // call the method that needs API 22 at least
} else {
   // do something backward compatible
}

Ref : Lambda expressions

Java 8 supported language features

The Android support of Java 8 language is not complete at the time of writing the post. However, all the main features are supported and some of them (like lambda expressions) are also backported to API level 23 and lower.

Here’s a list of the supported features:

  • Lambda expressions (also for API <= 23)

  • Method references (also for API <= 23)

  • Type annotations (also for API <= 23)

  • Default and static interface methods

  • Repeatable annotations

Other than the listed, it’s worth mentioning that Stream API is also supported as well as some other features (for a full list, please refer to the official documentation).

Surround the code that Requires API 22 or letter

//if(Build.VERSION.SDK_INT >= 22)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
  //code...
} else {
   //code.....
}
Related