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

AndroidJava 8Kotlin

Android Problem Overview


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'
    }

Android Solutions


Solution 1 - 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
    }
...
}

Solution 2 - Android

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
  }
}

Solution 3 - Android

In the app-level Gradle file, you have to write this code:

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

They come from JavaVersion.java in Android.

> An enumeration of Java versions. > > Before 9: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html > > After 9: http://openjdk.java.net/jeps/223

Solution 4 - Android

Update your build.gradle(Module:app) add compileOptions block and add JavaVersion.VERSION_1_8

apply plugin: 'com.android.application'

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

Solution 5 - Android

You can resolve this issue by downgrading the Source Compatibility and *Target Compatibility Java version to 1.8 in the latest Android Studio version 3.4.1.

  1. Open the Module Settings (Project Structure) window by right clicking on the app folder or Command + Down Arrow on Mac

    Enter image description here

  2. Go to ModulesProperties

    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. That's it. It will solve your issue.

Also you can manually add in file build.gradle (Module: app):

android {
...

compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }

...
}

Solution 6 - Android

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.example.architecture"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.room:room-runtime:2.2.5'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    annotationProcessor 'androidx.room:room-compiler:2.2.5'
    def lifecycle_version = "2.2.0"
    def arch_version = "2.1.0"

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
    implementation "androidx.cardview:cardview:1.0.0"
}

Add the configuration in your app module's build.gradle

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

Solution 7 - Android

Use this code in your build.gradle file:

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

Solution 8 - Android

My project uses ButterKnife and Retro lambda. Setting JavaVersion.VERSION_1_8 will not work. It always blamed the ButterKnife static interface function until I found this Migrate from Retrolambda.

TL;DR

Just add JavaVersion.VERSION_1_8 and completely remove retrolambda from your project. It will build successfully.

Solution 9 - Android

Changing the minSdkVersion from 19 to 21 solved the issue for me.

defaultConfig {
    applicationId "com.example"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 23
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
}

Solution 10 - Android

This also happened to me but using Dynamic Features. I already had Java 8 compatibility enabled in the app module but I had to add this compatibility lines to the Dynamic Feature module and then it worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionj2emanueView Question on Stackoverflow
Solution 1 - Androidj2emanueView Answer on Stackoverflow
Solution 2 - AndroidAmirView Answer on Stackoverflow
Solution 3 - AndroidNamelessView Answer on Stackoverflow
Solution 4 - AndroidVasudev VyasView Answer on Stackoverflow
Solution 5 - AndroidRajesh DalsaniyaView Answer on Stackoverflow
Solution 6 - AndroidJose AntonioView Answer on Stackoverflow
Solution 7 - AndroidMahabub KarimView Answer on Stackoverflow
Solution 8 - AndroidleegorView Answer on Stackoverflow
Solution 9 - Androidricky royView Answer on Stackoverflow
Solution 10 - AndroidJoaquin IurchukView Answer on Stackoverflow