Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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?

like image 441
Alexei Avatar asked Oct 22 '17 11:10

Alexei


People also ask

Can we have default implementation in the interface?

Interfaces can have default methods with implementation in Java 8 on later. Interfaces can have static methods as well, similar to static methods in classes. Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.

Is it mandatory to override default method of interface?

If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface. In short, you can access the default methods of an interface using the objects of the implementing classes.

What is default interface method?

Default methods enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.

How many default methods are allowed in interface?

Multiple Defaults With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.


2 Answers

Depending on what your minSdk version is you may need to add the following to your app's or module's build.gradle file:

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

See https://developer.android.com/studio/write/java8-support.html#supported_features for more details

like image 171
dboof Avatar answered Oct 19 '22 21:10

dboof


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)
like image 30
nhaarman Avatar answered Oct 19 '22 20:10

nhaarman