Interface methods are only supported by default starting with Android N

I upgraded to Android Studio 3.1 and get 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)} 

enter image description here

here is my gradle config:

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

As you can see, I am targeting 27 who are already ahead of 24 who are complaining about. What exactly should I do to fix this? If I upgrade to Java 1.8, I won’t miss many clients? Why didn’t I get this error before updating Android Studio?

I don't know if this concerns the LifecycleObserver class that I recently inserted, it was in kotlin, and now I changed it to java, but still getting the same error after clearing 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 to track where the error comes from so that 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' } 
+169
source share
8 answers

As CommonsWare mentioned, for reference, add this to the android {...} closure in build.gradle for your application module to solve the problem:

 android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } ... } 
+416
source

You must use Java8 to solve this problem, based on Google you can do it (click File> Project Structure ). and change Source Compatibility and Target Compatibility.

enter image description here

And you can also configure it directly in the corresponding 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 } } 
+44
source

Update your build.gradle (Module: application), add the compileOptions block and add JavaVersion.VERSION_1_8

 apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "ApplicationId" minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 
+16
source

In the application level gradle, you should write the following code:

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

They come from JavaVersion.java in Android.

List of Java Versions.

Before 9: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html

After 9: http://openjdk.java.net/jeps/223

@canerkaseler

+13
source

You can solve this problem by downgrading Java Compatibility and Target Compatibility to version 1.8 in the latest version of Android Studio 3.4.1

  1. Open the module settings (project structure) of Winodw by right-clicking the application folder or Ctrl + Down Arrow on Mac enter image description here

  2. Go to modules β†’ Properties enter image description here

  3. Change Source Compatibility and Target Compatibility version to 1.8 enter image description here

  4. Click on Apply or OK. This will solve your problem.

You can also manually add to build.gradle (Module: application)

 android { ... compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' } ... } 
+10
source

Use this code in your build.gradle

 android { compileOptions { incremental true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 
+5
source

You must tell Gradle that you are using Java 8 in your source code.

Change the build.gradle file in the application directory or in your library; Not root build.gradle in your main project directory

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

This also happened to me, but using dynamic functions. I already had Java 8 compatibility enabled in the application module, but I had to add these compatibility lines to the Dynamic Feature module, and then it worked.

0
source

Source: https://habr.com/ru/post/1276113/


All Articles