尝试在Android中运行Spark:仅从Android O开始支持MethodHandle.invoke和MethodHandle.invokeExact

问题描述

我需要在android中生成一个webhook网址。最初,我尝试使用Spring Framework,但是实现起来似乎很困难,我只是想公开一个单独的webhook url,所以我寻找了一个轻量级的框架。我现在一直在尝试使用spark-java,但无法运行它。

错误

    Suppressed: java.util.concurrent.ExecutionException: com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:552)
        at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:513)
        at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:86)
        at com.android.tools.r8.utils.U0.a(:14)
        at com.android.tools.r8.utils.U0.a(:9)
        ... 114 more
    [CIRculaR REFERENCE:com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)]
Caused by: com.android.tools.r8.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)

这是我的build.gradle文件

plugins {
    id 'com.android.application'
//    id 'org.springframework.boot' version '2.3.3.RELEASE'
//    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
//apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsversion "30.0.2"

    defaultConfig {
        applicationId "com.fivefaces.tauri"
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation filetree(dir: "libs",include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'

    testCompileOnly 'org.projectlombok:lombok:1.18.12'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
    compile group: 'com.fasterxml.jackson.core',name: 'jackson-databind',version: '2.0.1'
    compile "com.sparkjava:spark-core:2.9.2"


}
  • 版本版本为6.6.1
  • Project Structure中,我已经检查了定义的Java SDK,并将其正确设置为1.8
  • 我正在使用的最低Android SDK是API 16

解决方法

我已经能够在Android上运行Spark服务器。这是我的build.gradle。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.mycom.appname"
        minSdkVersion 27
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs',include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'com.sparkjava:spark-core:2.9.2'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

我们俩都使用Spark 2.9.2。我对gradle不太熟悉,但是我注意到我们表示依赖的方式不同(编译“ com.sparkjava:spark-core:2.9.2”与实现'com.sparkjava:spark-core:2.9 .2')。