1 现象描述
将AS由之前的dolphin版本升级到giraffe之后,接着打开以前的Android project,出现了"Gradle project sync failed…"的异常提示,在build面板中并没有出现project sync过程中报错的日志。
异常提示如下图所示:
<1> 通过invalidate cache -> restart ide未能解决。
<2> 多次Sync未能解决。
<3> 删掉编译期间生成的.idea、build目录之后,sync未能解决。
2 分析sync error 的日志
点击 上图中的 “show log in explorer”,打开对应的log文件,根据报错的time,在log文件由最底部开始查看日志。出现如下关键日志:
2024-03-17 22:33:23,834 [ 808163] INFO - #o.j.p.g.s.p.GradleProjectResolver - Gradle project resolve error
org.gradle.tooling.GradleConnectionException: Could not run phased build action using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-7.4-bin.zip'.
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:55)
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29)
at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:43)
at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:69)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.gradle.internal.jvm.JavaHomeException: The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location: C:\Program Files\Android\Android Studio\jre\bin\java.exe
at org.gradle.internal.jvm.Jvm.findExecutable(Jvm.java:183)
at org.gradle.internal.jvm.Jvm.getJavaExecutable(Jvm.java:208)
at org.gradle.internal.jvm.Jvm.forHome(Jvm.java:119)
at org.gradle.launcher.daemon.context.DaemonCompatibilitySpec.javaHomeMatches(DaemonCompatibilitySpec.java:64)
at org.gradle.launcher.daemon.context.DaemonCompatibilitySpec.whyUnsatisfied(DaemonCompatibilitySpec.java:40)
at org.gradle.launcher.daemon.context.DaemonCompatibilitySpec.isSatisfiedBy(DaemonCompatibilitySpec.java:35)
at org.gradle.launcher.daemon.context.DaemonCompatibilitySpec.isSatisfiedBy(DaemonCompatibilitySpec.java:25)
at org.gradle.launcher.daemon.client.DefaultDaemonConnector.getCompatibleDaemons(DefaultDaemonConnector.java:192)
at org.gradle.launcher.daemon.client.DefaultDaemonConnector.connectToIdleDaemon(DefaultDaemonConnector.java:157)
at org.gradle.launcher.daemon.client.DefaultDaemonConnector.connect(DefaultDaemonConnector.java:125)
at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:145)
at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:99)
at org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:44)
at org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:30)
at org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:60)
at org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:37)
at org.gradle.tooling.internal.provider.ProviderConnection.run(ProviderConnection.java:222)
at org.gradle.tooling.internal.provider.ProviderConnection.runPhasedAction(ProviderConnection.java:183)
at org.gradle.tooling.internal.provider.DefaultConnection.run(DefaultConnection.java:245)
at org.gradle.tooling.internal.consumer.connection.PhasedActionAwareConsumerConnection.run(PhasedActionAwareConsumerConnection.java:56)
at org.gradle.tooling.internal.consumer.connection.ParameterValidatingConsumerConnection.run(ParameterValidatingConsumerConnection.java:62)
at org.gradle.tooling.internal.consumer.DefaultPhasedBuildActionExecuter$1.run(DefaultPhasedBuildActionExecuter.java:78)
at org.gradle.tooling.internal.consumer.DefaultPhasedBuildActionExecuter$1.run(DefaultPhasedBuildActionExecuter.java:70)
at org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.run(LazyConsumerActionExecutor.java:143)
at org.gradle.tooling.internal.consumer.connection.CancellableConsumerActionExecutor.run(CancellableConsumerActionExecutor.java:45)
at org.gradle.tooling.internal.consumer.connection.ProgressLoggingConsumerActionExecutor.run(ProgressLoggingConsumerActionExecutor.java:61)
at org.gradle.tooling.internal.consumer.connection.RethrowingErrorsConsumerActionExecutor.run(RethrowingErrorsConsumerActionExecutor.java:38)
at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:67)
... 5 more
“Caused by: org.gradle.internal.jvm.JavaHomeException: The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location:”
这一处log是关键。
另外在期间,as提示了本地配置的JAVA_HOME jdk版本是11(as dolphin 使用jdk 11),而更新后的giraffe 版本使用的jdk是17。
同时也出现了如下的warn 日志:
Multiple Gradle daemons might be spawned because the Gradle JDK and JAVA_HOME locations are different.
Project 'VoiceUI' is using the following JDK location when running Gradle: 'C:/Program Files/Android/Android Studio/jbr'
The system environment variable JAVA_HOME is: 'C:\Program Files\Java\jdk-11'
If you dont need to use different paths (or if JAVA_HOME is undefined),
you can avoid spawning multiple daemons by setting JAVA_HOME and the JDK location to the same path.
3 解决方式
打开安装的AndroidStudio 目录,将jbr目录下的文件或文件夹全部copy到jre目录下。copy之前的jre目录缺少很多文件。
然后在AS中重新Sync,便能成功编译了。
4 个人理解
AS dolphin使用的是jdk 11,升级到giraffe后默认使用的是jdk 17,而安装成功的AndroidStudio 的jre目录缺少文件。
咱们可以在oracle官网上下载jdk 17的最新版本,安装之后,重新配置jdk环境变量JAVA_HOME为jdk-17。
5 参考文章
1The supplied javaHome seems to be invalid. I cannot find the java executable
2 【Android Studio】成功解决 “gradle project sync failed”