j2emanue
j2emanue

Reputation: 62519

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

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

Upvotes: 342

Views: 150778

Answers (10)

j2emanue
j2emanue

Reputation: 62519

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

Upvotes: 741

Jose Antonio
Jose Antonio

Reputation: 898

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

Upvotes: 12

ricky roy
ricky roy

Reputation: 176

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
}

Upvotes: 3

leegor
leegor

Reputation: 532

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.

Upvotes: 3

canerkaseler
canerkaseler

Reputation: 7468

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

Upvotes: 63

Rajesh Dalsaniya
Rajesh Dalsaniya

Reputation: 2127

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

...
}

Upvotes: 27

Mahabub Karim
Mahabub Karim

Reputation: 870

Use this code in your build.gradle file:

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

Upvotes: 7

Amirhosein Bayat
Amirhosein Bayat

Reputation: 1123

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

Upvotes: 88

Vasudev Vyas
Vasudev Vyas

Reputation: 742

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

Upvotes: 48

Joaquin Iurchuk
Joaquin Iurchuk

Reputation: 5617

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.

Upvotes: 1

Related Questions