Android studio“MethodHandle.invoke 和 MethodHandle.invokeExact 仅支持从 Android O (--min-api 26) 开始”构建 AP

Posted

技术标签:

【中文标题】Android studio“MethodHandle.invoke 和 MethodHandle.invokeExact 仅支持从 Android O (--min-api 26) 开始”构建 APK 时出错【英文标题】:Android studio "MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)" error when building APK 【发布时间】:2021-03-27 02:16:21 【问题描述】:

我正在开发一个带有许多导入的外部.jarandroid 应用程序(在我很新的 Android Studio 中),模拟器中的开发进展顺利,但是当我开始构建 APK 时,出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:desugarDebugFileDependencies'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Error while dexing.


Error: C:\Users\Daniel Alves Rosel\.gradle\caches\transforms-2\files-2.1\514725b031c9b95dac9cf3e9a193720c\jetified-jetty-util-9.4.31.v20200723.jar:org/eclipse/jetty/util/ModuleLocation.class, java.net.URI org.eclipse.jetty.util.ModuleLocation.getModuleLocation(java.lang.Class), MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Stack trace:
com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
    at com.android.tools.r8.errors.a.a(:7)
    at com.android.tools.r8.ir.conversion.O.b(:58)
    at com.android.tools.r8.ir.conversion.O.a(:104)
    at com.android.tools.r8.ir.conversion.O.a(:53)
    at com.android.tools.r8.graph.C.b(:43)
    at com.android.tools.r8.ir.conversion.O.b(:35)
    at com.android.tools.r8.utils.U0.a(:10)
    at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:125)
    at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:69)
    at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:78)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutorService.execute(MoreExecutors.java:322)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:66)
    at com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:36)
    at com.android.tools.r8.utils.U0.a(:3)
    at com.android.tools.r8.ir.conversion.O.a(:10)
    at com.android.tools.r8.D8.d(:29)
    at com.android.tools.r8.D8.b(:1)
    at com.android.tools.r8.utils.W.a(:30)
    at com.android.tools.r8.D8.run(:11)
    at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:116)
    at com.android.builder.dexing.DexArchiveBuilder.convert$default(DexArchiveBuilder.kt:46)
    at com.android.build.gradle.internal.tasks.DexFileDependenciesTask$DexFileDependenciesWorkerAction.run(DexFileDependenciesTask.kt:153)
    at com.android.build.gradle.internal.tasks.Workers$ActionFacade.run(Workers.kt:242)
    at org.gradle.workers.internal.AdapterWorkAction.execute(AdapterWorkAction.java:50)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:50)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:59)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:98)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:59)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
    at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:53)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:200)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.lang.Thread.run(Thread.java:748)
    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)
        ... 39 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)
    at com.android.tools.r8.ir.conversion.N.a(:633)
    at com.android.tools.r8.ir.conversion.N.a(:674)
    at com.android.tools.r8.r.n.A.a(:236)
    at com.android.tools.r8.ir.conversion.o.a(:153)
    at com.android.tools.r8.ir.conversion.N.a(:133)
    at com.android.tools.r8.graph.l.a(:93)
    at com.android.tools.r8.graph.l.a(:71)
    at com.android.tools.r8.graph.l.buildIR(:1)
    at com.android.tools.r8.graph.C0.buildIR(:1)
    at com.android.tools.r8.graph.T.a(:152)
    at com.android.tools.r8.ir.conversion.O.a(:412)
    at com.android.tools.r8.ir.conversion.O.b(:53)
    ... 52 more

我尝试将以下内容添加到我的 build.gradle;没有任何成功。

    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    

我尝试将我系统上运行的 Java 版本从 15 => 8 更改,因为我看到一个帖子建议版本 java -version 应该等于 sourceCompatibility JavaVersion.VERSION_1_8

当我运行 gradlew :app:dependencies 时(建议在 [https://***.com/questions/63612606/com-android-tools-r8-errors-a-methodhandle-invoke-and-methodhandle-invokeexact] 中找到jar 导致问题),我得到:

* What went wrong:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

添加:

allprojects 
    configurations.all 
        resolutionStrategy.force 'org.objenesis:objenesis:2.6'
    

在 build.gradle 结束时也没有做任何事情。

这是我的 build.gradle:

apply plugin: 'com.android.application'


buildscript 
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath "com.android.tools.build:gradle:4.0.1"

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


android 
    packagingOptions 
        exclude 'META-INF/DEPENDENCIES'
    
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig 
        applicationId "com.borg.borg2"
        minSdkVersion 23
        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.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation files('C:\\Users\\Daniel Alves Rosel\\Documents\\Invoy\\Projects\\BORG\\nodes\\node-api-tunnel-v9.jar')
    implementation fileTree(dir: 'C:\\Users\\Daniel Alves Rosel\\Documents\\Invoy\\Projects\\BORG\\dependencies\\dependencies_package', include: ['*.aar', '*.jar'], exclude: [])
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'


allprojects 
    configurations.all 
        resolutionStrategy.force 'org.objenesis:objenesis:2.6'
    

【问题讨论】:

【参考方案1】:

您使用的其中一个库正在使用仅在 API 级别 26 的 Android 上受支持的 Java 功能。由于这些功能没有脱糖/反向移植,如果 minSdkVersion 低于 26,则编译失败。允许这些语言在 issue 174733673 中跟踪较低 API 级别的功能。

这在从 Android Studio 进行测试时起作用的原因是,Android Studio 将指示 AGP 使用与用于测试的设备匹配的 API 级别进行构建,以获得最佳调试体验。当最终项目构建完成时,build.gradle 中设置的 API 级别将生效,并导致此编译错误。

除了删除有问题的库(似乎是jetty-util-9.4.31.v20200723.jar)之外,您还可以尝试另一件事,那就是按照Shrink, obfuscate, and optimize your app 中的描述缩小您的应用程序。这假设有问题的代码在运行时实际上是死代码,并通过收缩删除。

【讨论】:

以上是关于Android studio“MethodHandle.invoke 和 MethodHandle.invokeExact 仅支持从 Android O (--min-api 26) 开始”构建 AP的主要内容,如果未能解决你的问题,请参考以下文章

android studio怎么更新

android studio setting 在哪

如何解决android studio2.3导入android studio2.1的问题

Android studio上找不到setting?

android studio v4包冲突

如何用android studio 编成app