android studio error:默认接口方法仅支持从Android N开始(--min-api 24)

j2e*_*nue 193 android java-8 kotlin

我升级到Android工作室3.1,我得到以下错误:

    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)}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是我的gradle配置:

      compileSdkVersion 27
//buildToolsVersion '27.0.3'
defaultConfig {
    minSdkVersion 16
    targetSdkVersion 27
     multiDexEnabled true
     //...
   }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,我的目标27已经领先于它抱怨的24个?我该怎么做才能解决这个问题?如果我改为1.8 java不会让我失去很多客户?为什么我在升级android studio之前没有收到此错误.

我不知道这是关于我最近投入的LifecycleObserver类,它是在kotlin中,现在我将其更改为java但在清理项目后仍然得到相同的错误:

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);
}
Run Code Online (Sandbox Code Playgroud)

}

如何追踪错误的来源,以便我可以修复它?

这是我的版本依赖项:

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'
    }
Run Code Online (Sandbox Code Playgroud)

j2e*_*nue 456

正如CommonsWare所提到的,作为参考android {...},在build.gradle 的闭包内添加这个,以便你的app模块解决问题:

android {
...
  compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
...
}
Run Code Online (Sandbox Code Playgroud)

  • 请指定它应该在android大括号内,如下所示:android {// add here} (13认同)
  • @ j2emanue我已经在build.gradle中添加了这个,但仍然遇到同样的问题,仅用于发布版本 (3认同)
  • *叹息*为什么谷歌不能把适当的东西放在默认项目设置中?.. (2认同)

Ami*_*mir 53

您应该使用Java8来解决这个问题,基于谷歌,您可以通过(单击文件>项目结构)来执行此操作.并更改源兼容性和目标兼容性.

在此输入图像描述

您也可以直接在相应的build.gradle文件中配置它:

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
  }
}
Run Code Online (Sandbox Code Playgroud)


can*_*ler 23

在应用程序级gradle中,您必须编写以下代码:

android {
...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Run Code Online (Sandbox Code Playgroud)

它们来自Android中的JavaVersion.java

Java版本的枚举。

9之前:http : //www.oracle.com/technetwork/java/javase/versioning-naming-139433.html

9点之后:http//openjdk.java.net/jeps/223

@canerkaseler


Vas*_*yas 20

更新您的build.gradle(Module:app)添加compileOptions块并添加JavaVersion.VERSION_1_8

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "ApplicationId"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*iya 14

您可以通过在最新的Android Studio版本3.4.1中将源兼容性和目标兼容性Java版本降级为1.8来解决此问题。

  1. 右键单击应用程序文件夹或在Mac上单击Command +向下箭头,以打开模块设置(项目结构)Winodw 在此处输入图片说明

  2. 转到模块->属性 在此处输入图片说明

  3. 将源兼容性和目标兼容性版本更改为1.8 在此处输入图片说明

  4. 单击“应用”或“确定”。它将解决您的问题。

您也可以手动添加build.gradle(模块:app)

android {
...

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

...
}
Run Code Online (Sandbox Code Playgroud)


Ant*_*nio 10

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"
}
Run Code Online (Sandbox Code Playgroud)

在您的应用程序模块中添加配置build.gradle

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*rim 5

在build.gradle中使用此代码

android {
    compileOptions {
        incremental true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Run Code Online (Sandbox Code Playgroud)