Home » Android » Is it possible to use default interface implementation in Android < API 24?

Is it possible to use default interface implementation in Android < API 24?

Posted by: admin November 1, 2017 Leave a comment

Questions:

Android Studio 2.3.3, Java 8

I create Android app for Android 4.0+

In my app/build.gradle:

...
minSdkVersion 15
targetSdkVersion 26

I want to use default interface implementation (from Java 8).
So I create the tnext class:

public interface DefaultCallback {

    public default void onResponse(Call<T> var1, Response<T> var2) {

    }
}

but I get compile error:

Default method required API level 24 (current min is 15)

So the question is:

Can I use deafult interface implementation on Android < API 24?

Answers:

No.

Default methods require API level 24.

From https://developer.android.com/guide/platform/j8-jack.html#supported-features:

Android does not support all Java 8 language features. However, the
following features are available when developing apps targeting
Android 7.0 (API level 24):

  • Default and static interface methods
  • Lambda expressions (also
    available on API level 23 and lower)
  • Repeatable annotations
  • Method References (also available on API level 23 and lower)
  • Type Annotations
    (also available on API level 23 and lower)