Gradle Android错误:MethodHandle.invoke和MethodHandle.invokeExact

jbr*_*own 5 java spring android gradle

我正在尝试在我的Android应用程序中使用SOAP服务。我正在尝试遵循本指南。当我尝试运行我的应用程序时,出现错误:

Error: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Run Code Online (Sandbox Code Playgroud)

我使用的是Android N(SDK 25-我无法像错误消息所示的那样升至26),这是由Android Studio创建的全新项目。

这是我尚未更改的项目build.gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Run Code Online (Sandbox Code Playgroud)

这是我的应用程序build.gradle文件:

apply plugin: 'com.android.application'

configurations {
    jaxb
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
    mavenCentral()
}

// tag::wsdl[]
task genJaxb {
    ext.sourcesDir = "${buildDir}/generatedjava/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = "https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01"

    outputs.dir classesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)
            mkdir(dir: classesDir)

            xjc(destdir: sourcesDir, schema: schema,
                    package: "hello.wsdl") {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }

            javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
                    debugLevel: "lines,vars,source",
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDir)
                include(name: "**/*.java")
                include(name: "*.java")
            }

            copy(todir: classesDir) {
                fileset(dir: sourcesDir, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
        }
    }
}
// end::wsdl[]

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.magicmirror"
        minSdkVersion 25
        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 JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/spring.tooling'
        exclude 'META-INF/spring.handlers'
        exclude 'META-INF/spring-configuration-metadata.json'
        exclude 'META-INF/additional-spring-configuration-metadata.json'
        exclude 'META-INF/spring.factories'
        exclude 'META-INF/spring.schemas'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation "org.springframework.boot:spring-boot-starter"
    implementation "org.springframework.ws:spring-ws-core"
    implementation(files(genJaxb.classesDir).builtBy(genJaxb))

    jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"
}

Run Code Online (Sandbox Code Playgroud)

从日志中,确切的JAR导致此错误org.springframework/spring-core/5.1.8.RELEASE.jarcompileOptions其他答案中提到了添加块作为修复程序,但尚未解决此问题。我怎样才能解决这个问题?

Mic*_*gan 0

我认为您只需要使用不同的库来进行 SOAP 调用。这是我使用过的,它支持早至 14 的 SDK 版本。

\n\n
private void GetSomeStuff(String myterm) throws UnsupportedEncodingException {\n\n\ntry {\n\n     String strStuff2 = "<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"+\n     "<soap:Envelope xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\\"http://www.w3.org/2001/XMLSchema\\" xmlns:soap=\\"http://schemas.xmlsoap.org/soap/envelope/\\">"+\n     "<soap:Header><wsse:Security xmlns:wsse=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\\">"+\n     "<wsse:UsernameToken><wsse:Username>my username</wsse:Username><wsse:Password Type=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\\">my password</wsse:Password></wsse:UsernameToken></wsse:Security></soap:Header>"+\n     "<soap:Body> <GetSomeStuff xmlns=\\"https://mycompany.com.MyService\\">"+\n     "<term>" + myterm + "/term></GetSomeStuff></soap:Body></soap:Envelope>";\n\n\n    StringBuilder stringBuilder = new StringBuilder();\n\n\n    HttpPost httppost = new HttpPost("https://mycompany.com/MyService.svc");        \n    StringEntity se;\n\n    se = new StringEntity(strStuff2,HTTP.UTF_8);\n\n\n    httppost.setHeader("SOAPAction", "https://mycompany.com.MyService/GetSomeStuff\xe2\x80\x9d);\n\n\n    se.setContentType("text/xml");  \n    httppost.setEntity(se);  \n\n\n    HttpClient httpclient = new DefaultHttpClient();      \n\n    HttpResponse theresponse = (HttpResponse) httpclient.execute(httppost);\n\n    StatusLine statusLine = theresponse.getStatusLine();\n    int statusCode = statusLine.getStatusCode();\n    if (statusCode == 200) {\n        HttpEntity entity = theresponse.getEntity();\n        InputStream content = entity.getContent();\n        BufferedReader reader = new BufferedReader(\n                new InputStreamReader(content));\n        String line;\n        while ((line = reader.readLine()) != null) {\n            stringBuilder.append(line);\n        }\n\n        String theXML = stringBuilder.toString();\n\n        int start = theXML.indexOf("<GetSomeStuffResult>") + 21;\n        int end = theXML.indexOf("</GetSomeStuffResult>") - 1;\n\n        // We didn\'t get the response we expected\n        if ((start < 0) || (end < 0) || (start==end)) {\n            Log.i(\xe2\x80\x9cMyApp\xe2\x80\x9d,\xe2\x80\x9dEmpty Response from GetSomeStuffResult");\n            return;\n        }\n\n        String myData = theXML.substring(start, end);\n\n        if (myData() > 0) {\n            try {\n                JSONObject jObject = new JSONObject(myData);\n\n                String deptDesc = jObject.getString("DepartmentDescription");\n                String areaCode = jObject.getString("area_code");\n                String phoneNumber = jObject.getString("phone_nbr");\n                String title = jObject.getString("title");\n\n                String fullPhoneNumber = "";\n\n                if (phoneNumber.length() == 7) {\n                    fullPhoneNumber = "(" + areaCode + ") " + phoneNumber.substring(0, 3) + "-" + phoneNumber.substring(3, 7);\n                } else {\n                     fullPhoneNumber = "(" + areaCode + ") " + phoneNumber;\n                }\n\n                SharedPreferences appPrefs =\n                        getSharedPreferences("appPreferences",MODE_PRIVATE);\n\n                SharedPreferences.Editor prefsEditor = appPrefs.edit();\n                prefsEditor.putString("Title",title);\n                prefsEditor.putString("DeptDescr",deptDesc);\n                prefsEditor.putString("PhoneNumber",fullPhoneNumber);\n\n                prefsEditor.commit();   \n\n            }\n            catch (JSONException e) {\n                e.printStackTrace();\n            }\n        }\n\n\n    } else {\n         Log.e(\xe2\x80\x9cMyApp\xe2\x80\x9d, "Failed to GetSomeStuff\xe2\x80\x9d);\n    }\n\n\n} catch (ClientProtocolException e) {\n    e.printStackTrace();\n} catch (IOException e) {\n    e.printStackTrace();\n} catch (Exception e) {\n    e.printStackTrace();\n}\n
Run Code Online (Sandbox Code Playgroud)\n