Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default interface methods are only supported starting with Android 7.0 (Nougat)

I upgraded to Android Studio 3.1 and I'm getting the following error:

Default interface methods are only supported starting with Android N (--min-api 24): void android.arch.lifecycle.DefaultLifecycleObserver.onCreate(android.arch.lifecycle.LifecycleOwner)

Message{kind=ERROR, text=Default interface methods are only supported starting with Android N (--min-api 24): void android.arch.lifecycle.DefaultLifecycleObserver.onCreate(android.arch.lifecycle.LifecycleOwner), sources=[Unknown source file], tool name=Optional.of(D8)}

Error

Here is my Gradle configuration:

compileSdkVersion 27 //buildToolsVersion '27.0.3' defaultConfig {     minSdkVersion 16     targetSdkVersion 27      multiDexEnabled true      //...    } 

As you can see, I am targeting 27 which is already ahead of 24 that it's complaining about. What exactly should I do to fix this? If I change to 1.8 Java, won't I be missing a lot of customers? Why was I not getting this error before I upgraded Android Studio?

I do not know if this is about the LifecycleObserver class I recently put in. It was in Kotlin and now I changed it to Java, but I still get the same error after cleaning the project:

public class LifeCycleAwareObserver implements LifecycleObserver {      @OnLifecycleEvent(Lifecycle.Event.ON_STOP)     public void  onAppBackgrounded() {         AnalyticsUtils.trackStartSession(true);     }      @OnLifecycleEvent(Lifecycle.Event.ON_START)     public void onAppForegrounded() {         AnalyticsUtils.trackStartSession(false);     } } 

How can I trace where the error is coming from so I can fix it?

Here are my version dependencies:

project.ext {          firebase_version = '12.0.0'          supportlib_version = '27.0.2'          room_version = '1.0.0'          espresso_version = '3.0.1'          archLifecycleVersion = '1.1.1'     } 
like image 713
j2emanue Avatar asked Mar 27 '18 12:03

j2emanue


People also ask

Can interface methods be default?

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.

Does Android support Java 8?

Android Gradle plugin 3.0. 0 and later support all Java 7 language features and a subset of Java 8 language features that vary by platform version. When building your app using Android Gradle plugin 4.0. 0 and higher, you can use a number of Java 8 language APIs without requiring a minimum API level for your app.

Does Android support Java?

Current versions of Android use the latest Java language and its libraries (but not full graphical user interface (GUI) frameworks), not the Apache Harmony Java implementation, that older versions used. Java 8 source code that works in latest version of Android, can be made to work in older versions of Android.


2 Answers

As CommonsWare mentioned, for reference add this inside the android {...} closure in the build.gradle for your app module (app level) to resolve the issue:

android { ...   compileOptions {         sourceCompatibility JavaVersion.VERSION_1_8         targetCompatibility JavaVersion.VERSION_1_8     } ... } 
like image 199
j2emanue Avatar answered Oct 12 '22 02:10

j2emanue


You should use Java 8 to solve this. Based on the Android documentation, you can do this by clicking menu FileProject Structure.

And change Source Compatibility and Target Compatibility.

Enter image description here

And you can also configure it directly in the app-level build.gradle file:

android {   ...   // Configure only for each module that uses Java 8   // language features (either in its source code or   // through dependencies).   compileOptions {     sourceCompatibility JavaVersion.VERSION_1_8     targetCompatibility JavaVersion.VERSION_1_8   } } 
like image 25
Amir Avatar answered Oct 12 '22 02:10

Amir