339

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'
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • 27
    "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](https://developer.android.com/studio/write/java8-support.html). "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). – CommonsWare Mar 27 '18 at 12:32
  • 2
    @CommonsWare 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 } – j2emanue Mar 27 '18 at 15:25

10 Answers10

738

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
    }
...
}
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • 14
    Please specify that it should be inside the android curly brace like so: android { // add here } – Idee Jun 07 '18 at 15:29
  • 4
    @j2emanue I already added this one in build.gradle, but still get the same problem, only for release build – ductran Sep 04 '18 at 14:28
  • i'd have to see your gradle file both project and module gradle files. maybe post a question. – j2emanue Sep 04 '18 at 16:56
  • adding this will show unresloved reference into the project structure any idea about that? – Urvish rana May 01 '19 at 09:27
  • 2
    *Sigh* Why google cannot just put appropriate things in default project settings?.. – Roman Truba Jun 18 '19 at 11:04
  • 1
    i was concerned that this would make my app incompatible with older android devices, but that's not the case: https://developer.android.com/studio/write/java8-support – Eric Jul 09 '19 at 15:17
  • It works for me. But curious about what introduce the issue. One suspicious point is that DexGuard uses Default interface methods. – Francis Bacon Jul 14 '20 at 06:55
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
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amir
  • 1,123
  • 8
  • 15
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
canerkaseler
  • 6,204
  • 45
  • 38
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
    }
}
eli
  • 8,571
  • 4
  • 30
  • 40
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
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'
    }

...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajesh Dalsaniya
  • 1,977
  • 1
  • 13
  • 12
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
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jose Antonio
  • 840
  • 14
  • 25
7

Use this code in your build.gradle file:

android {
    compileOptions {
        incremental true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahabub Karim
  • 810
  • 11
  • 17
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leegor
  • 472
  • 5
  • 15
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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ricky roy
  • 156
  • 1
  • 5
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.

Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • 1
    Could you provide an example how to do this? – Oleg Yablokov Jan 23 '21 at 15:04
  • @OlegYablokov I meant compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }. See them here: https://stackoverflow.com/a/56911656/2823516 – Joaquin Iurchuk Jan 27 '21 at 02:07
  • It would be better to [update your answer](https://stackoverflow.com/posts/57114138/edit). Comments may be removed at any time. But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today. – Peter Mortensen May 10 '21 at 14:20