Android编译错误 - 无法找到java / util / function / Function

时间:2018-04-02 22:58:06

标签: java android android-gradle

将Gradle版本更新为3.1后,运行应用程序时会抛出以下错误:

  

无法找到java / util / function / Function的字节代码

     

java.io.IOException:找不到字节代码失败   java / util / function /函数在   com.android.build.gradle.internal.incremental.AsmUtils.lambda $静态$ 0(AsmUtils.java:89)     在   com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:307)

有人可以帮我解决这个问题吗?

完整堆栈错误:

  

org.gradle.api.tasks.TaskExecutionException:任务执行失败   ':应用程序:transformClassesWithInstantRunForMockDebug&#39 ;.在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)     在   org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)     在   org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)     在   org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)     在   org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)     在   org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)     在   org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)     在   org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)     在   org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)     在   org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)     在   org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)     在   org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)     在   org.gradle.execution.taskgraph.DefaultTaskGraphExecuter $ EventFiringTaskWorker $ 1.run(DefaultTaskGraphExecuter.java:248)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor $ RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor $ RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)     在   org.gradle.execution.taskgraph.DefaultTaskGraphExecuter $ EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)     在   org.gradle.execution.taskgraph.DefaultTaskGraphExecuter $ EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor $ TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor $ TaskExecutorWorker.access 200 $(DefaultTaskPlanExecutor.java:79)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor $ TaskExecutorWorker $ 1.execute(DefaultTaskPlanExecutor.java:104)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor $ TaskExecutorWorker $ 1.execute(DefaultTaskPlanExecutor.java:98)     在   org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)     在   org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)     在   org.gradle.execution.taskgraph.DefaultTaskPlanExecutor $ TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)     在   org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)     在   org.gradle.internal.concurrent.ManagedExecutorImpl $ 1.run(ManagedExecutorImpl.java:46)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:624)     在   org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)     在java.lang.Thread.run(Thread.java:748)引起:   了java.lang.RuntimeException:   com.android.build.api.transform.TransformException:   java.lang.RuntimeException:java.lang.RuntimeException:   java.io.IOException:无法找到的字节代码   java / util / function /函数在   com.android.builder.profile.Recorder $ Block.handleException(Recorder.java:55)     在   com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)     在   com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)at   org.gradle.api.internal.project.taskfactory.IncrementalTask​​Action.doExecute(IncrementalTask​​Action.java:46)     在   org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)     在   org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter $ 1.run(ExecuteActionsTaskExecuter.java:121)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor $ RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor $ RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)     在   org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)     在   org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)     ... 32更多引起:   com.android.build.api.transform.TransformException:   java.lang.RuntimeException:java.lang.RuntimeException:   java.io.IOException:无法找到的字节代码   java / util / function /函数在   com.android.build.gradle.internal.transforms.InstantRunTransform.doTransform(InstantRunTransform.java:312)     在   com.android.build.gradle.internal.transforms.InstantRunTransform.transform(InstantRunTransform.java:178)     在   com.android.build.gradle.internal.pipeline.TransformTask $ 2.call(TransformTask.java:221)     在   com.android.build.gradle.internal.pipeline.TransformTask $ 2.call(TransformTask.java:217)     在   com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)     ... 48更多引起:java.lang.RuntimeException:   java.lang.RuntimeException:java.io.IOException:找不到字节   java / util / function / Function的代码   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在   java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:593)     在   java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:677)     在java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:720)at at   com.android.ide.common.internal.WaitableExecutor.waitForTasksWithQuickFail(WaitableExecutor.java:146)     在   com.android.build.gradle.internal.transforms.InstantRunTransform.doTransform(InstantRunTransform.java:307)     ... 52更多引起:java.lang.RuntimeException:   java.io.IOException:无法找到的字节代码   java / util / function /函数在   java.util.concurrent.ForkJoinTask $ AdaptedCallable.exec(ForkJoinTask.java:1431)     at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)     在   java.util.concurrent.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool.java:1056)     在   java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)     在   java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)   引起:java.io.IOException:无法找到的字节代码   java / util / function /函数在   com.android.build.gradle.internal.incremental.AsmUtils.lambda $静态$ 0(AsmUtils.java:89)     在   com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:307)     在   com.android.build.gradle.internal.incremental.AsmUtils.readInterfaceHierarchy(AsmUtils.java:216)     在   com.android.build.gradle.internal.incremental.AsmUtils.readInterfaces(AsmUtils.java:251)     在   com.android.build.gradle.internal.incremental.AsmUtils.readInterfaceHierarchy(AsmUtils.java:220)     在   com.android.build.gradle.internal.incremental.AsmUtils.readInterfaces(AsmUtils.java:251)     在   com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:288)     在   com.android.build.gradle.internal.incremental.IncrementalVisitor.instrumentClass(IncrementalVisitor.java:342)     在   com.android.build.gradle.internal.transforms.InstantRunTransform.transformToClasses2Format(InstantRunTransform.java:406)     在   com.android.build.gradle.internal.transforms.InstantRunTransform.lambda $ doTransform $ 3(InstantRunTransform.java:268)     在   com.android.build.gradle.internal.transforms.InstantRunTransform.lambda为$ null $ 4(InstantRunTransform.java:297)     在   java.util.concurrent.ForkJoinTask $ AdaptedCallable.exec(ForkJoinTask.java:1424)     ......还有4个

7 个答案:

答案 0 :(得分:29)

似乎有几个原因导致了这个问题。

如果您使用Kotlin,请尝试将forEach{ key, value -> ... }更改为forEach{ (key, value) -> ... }

在我的情况下,我从不使用Kotlin。禁用“即时运行”后,此错误消失。

答案 1 :(得分:19)

我在这里找到了解决问题的方法:Why does Kotlin byte code reference java.util.function.BiConsumer?

我正在使用Kotlin,我发现了使用forEach {键,值-> ...}的行,并将其更改为forEach {(键,值)-> ...},这使Kotlin选择了JRE 6而不是8。

答案 2 :(得分:4)

这看起来像编译器问题。 您是否尝试重启Android工作室? 如果是,请尝试

Build-> Clean Project
Build-> Make Project

如果问题仍然存在,请尝试清除Android studio缓存

答案 3 :(得分:0)

我遇到了同样的问题,对我有用的是清理Android Studio首选项:

删除.AndroidStudioX.X

删除.gradle

重启Android Studio

然后我遇到另一个问题,解决方案是禁用Instant Run。

希望它有助于某人

答案 4 :(得分:0)

当某些gradle插件与minsdk不匹配时会发生。我使用的是Sceneform sdk,但我的minsdk是22,但需要24。当我将minsdk从22更改为24时,它就起作用了。

答案 5 :(得分:0)

我有一个类似的问题,而且似乎突然开始了。禁用即时运行是唯一对我有用的修复程序。我知道这里提到了它,但是没有看到关于禁用它的任何说明,因此您可以.... 在Android Studio中,依次选择“文件”,“设置”,“执行,部署,即时运行”,将“对即时交换代码启用即时运行/部署时更改资源”设置为未选中。点击“应用”,确定,“调试”应该再次起作用。感谢那些提到它的人!

答案 6 :(得分:0)

尝试清理和重建/制作您的项目。如果问题仍然存在,请在您的Android Studio中disable Instant Run,然后再次运行您的应用,这应该可以解决问题。