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)}
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' }
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.
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.
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.
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 } ... }
You should use Java 8 to solve this. Based on the Android documentation, you can do this by clicking menu File → Project Structure.
And change Source Compatibility and Target Compatibility.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With