记录Compose第一个问题--Gradle插件和Java版本不匹配

杨甫
2023-12-01

在Android StudioArtic Fox创建项目后出现问题:

Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the User Manual chapter on the daemon at https://docs.gradle.org/7.0.2/userguide/gradle_daemon.html
Process command line:..\java-se-8u41-ri\bin\java.exe -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country=CN -Duser.language=zh -Duser.variant -cp ..\.gradle\wrapper\dists\gradle-7.0.2-bin\857tjihv64xamwrf0h14cai3r\gradle-7.0.2\lib\gradle-launcher-7.0.2.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 7.0.2
Please read the following process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap

-----------------------
Check the JVM arguments defined for the gradle process in:
 - gradle.properties in project root directory

这个问题是无法为2097152KB对象堆保留足够的空间,然后把gradle.properties下的 org.gradle.jvmargs=-Xmx2048m 改为1024m

接着出现了另外一个问题:

Build file '..\app\build.gradle' line: 2

An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

这个问题是Gradle插件要求Java版本是11,而我在电脑本地环境设置的Java版本是8,所以要改项目所使用Java的版本,这里我没有改环境变量的版本,而是修改在项目中修改Java版本。下面是步骤:依次点击 File -> Project Structure ->  SDK Location -> Gradle Settings -> Gradle JDK选择框 -> 选择Embedded JDK version 11 -> OK -> OK。

最后重新gradle一下,也可以把gradle.properties下的 org.gradle.jvmargs=-Xmx改回2048。

 类似资料: