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

10

340

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'
    }
Deduct answered 27/3, 2018 at 12:16 Comment(2)
"as you can see i am targetting 27 which is already ahead of 24 that its complaining about ?" -- it is not complaining about your targetSdkVersion. "What exactly should i do to fix this ?" -- try compiling with Java 8 compatibility enabled. "if i change to 1.8 java wont i be missing alot of customers ?" -- not for the default interface methods that the error message is complaining about. "why was i not getting this error before i upgraded android studio. " -- perhaps an implicit dependency (e.g., data binding).Lymphatic
@Lymphatic it was exactly as you mentioned. changing to 1.8 resolved the issue but i'll have to check it on older devices to see what happens. the code that did it was compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }Deduct
D
740

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
    }
...
}
Deduct answered 28/3, 2018 at 3:37 Comment(7)
Please specify that it should be inside the android curly brace like so: android { // add here }Gabriellagabrielle
@Deduct I already added this one in build.gradle, but still get the same problem, only for release buildDemur
i'd have to see your gradle file both project and module gradle files. maybe post a question.Deduct
adding this will show unresloved reference into the project structure any idea about that?Bain
Sigh Why google cannot just put appropriate things in default project settings?..Lactam
i was concerned that this would make my app incompatible with older android devices, but that's not the case: developer.android.com/studio/write/java8-supportNectarous
It works for me. But curious about what introduce the issue. One suspicious point is that DexGuard uses Default interface methods.Comose
C
88

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
  }
}
Cracy answered 7/2, 2019 at 4:56 Comment(1)
Thankuu programmer.Nice JobLindeberg
M
63

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

Menswear answered 6/7, 2019 at 5:59 Comment(1)
thanks for the explanation and info about which file to update. App-level or Project level.Colwin
F
48

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
    }
}
Fomalhaut answered 13/3, 2019 at 10:13 Comment(0)
R
27

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

...
}
Rozellarozelle answered 18/6, 2019 at 5:10 Comment(1)
It just solved my problem. But please advise me how the hell did you know this is the solution?Aunt
H
12
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
    }
}
Howdoyoudo answered 15/7, 2020 at 16:29 Comment(0)
T
7

Use this code in your build.gradle file:

android {
    compileOptions {
        incremental true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Tillandsia answered 27/5, 2019 at 17:13 Comment(0)
I
3

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.

Inexpensive answered 24/12, 2019 at 16:4 Comment(0)
H
3

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
}
Hola answered 20/5, 2020 at 18:5 Comment(0)
A
1

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.

Antisepticize answered 19/7, 2019 at 14:2 Comment(3)
Could you provide an example how to do this?Multiracial
@OlegYablokov I meant compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }. See them here: https://mcmap.net/q/98065/-default-interface-methods-are-only-supported-starting-with-android-7-0-nougatAntisepticize
It would be better to update your answer. Comments may be removed at any time. But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.Ardor

© 2022 - 2024 — McMap. All rights reserved.