我升级到Android Studio 3.1,我得到以下错误:

默认的接口方法只支持从Android N(——min-api 24)开始:void Android .arch.lifecycle. defaultlifecycleobserver . oncreate (Android .arch. lifecycleowner) 消息{kind=ERROR, text=默认接口方法只支持从Android N开始(——min-api 24): void Android .arch. lifeccycle . defaultlifecycle观察者. oncreate (Android .arch. lifeccycle . lifecycleowner), sources=[未知源文件],工具名=Optional.of(D8)}

这是我的Gradle配置:

compileSdkVersion 27
//buildToolsVersion '27.0.3'
defaultConfig {
    minSdkVersion 16
    targetSdkVersion 27
     multiDexEnabled true
     //...
   }

如你所见,我的目标是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);
    }
}

我如何跟踪错误来自哪里,以便修复它?

以下是我的版本依赖项:

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

当前回答

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

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

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

其他回答

在应用级Gradle文件中,你必须编写以下代码:

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

它们来自Android中的JavaVersion.java。

Java版本的枚举。 9点前:http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html 9点后:http://openjdk.java.net/jeps/223

更新build.gradle(Module:app),添加compileOptions块,并添加javaverse。VERSION_1_8

apply plugin: 'com.android.application'

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

您应该使用Java 8来解决这个问题。根据Android文档,您可以通过单击菜单文件→项目结构来完成此操作。

并更改源兼容性和目标兼容性。

你也可以在应用级构建中直接配置它。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
  }
}

我的项目使用ButterKnife和Retro lambda。设置JavaVersion。VERSION_1_8将不能工作。它总是责怪ButterKnife静态接口函数,直到我发现这个Migrate from Retrolambda。

博士TL;

只需添加javaverse。VERSION_1_8并从项目中完全删除retrolambda。它将成功构建。

将minSdkVersion从19改为21为我解决了这个问题。

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