【Compose multiplatform教程06】用IDEA编译Compose Multiplatform常见问题

当我们从Kotlin Multiplatform Wizard | JetBrains 下载ComposeMultiplatform项目时

会遇到无法正常编译/运行的情况,一般网页和桌面是可以正常编译的,

我这里着重解决如下问题

1:Gradle版本不兼容或者Gradle连接超时

2:JDK版本不兼容

3:Gradle依赖库连接超时-替换成国内镜像

4:Android环境编译

5:Android 运行到手机/模拟器

6:IOS平台(后续补充)

1:Gradle版本不兼容或者Gradle连接超时

The project is using an incompatible version (AGP 8.5.2) of the Android Gradle plugin. Latest supported version is AGP 8.2.0

See Android Studio & AGP compatibility options.

Experimental\] Enable sync with future AGP version. > > > 该项目正在使用不兼容版本(Android Gradle 插件 8.5.2 版)的 Android Gradle 插件。所支持的最新版本是 Android Gradle 插件 8.2.0 版。 > > 查看 Android Studio 与 Android Gradle 插件的兼容性选项。 > > \[实验性\] 启用与未来 Android Gradle 插件版本的同步功能。 下载最新版本[IntelliJ IDEAIntelliJ IDEA 毫无疑问是软件开发者的首选 IDE。 It makes Java and Kotlin development a more productive and enjoyable experience.![icon-default.png?t=O83A](https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png)https://www.jetbrains.com.cn/idea/promo/?utm_source=baidu\&utm_medium=cpc\&utm_campaign=cn-bai-br-intellij-ex-pc\&utm_content=intellij-core\&utm_term=idea](https://www.jetbrains.com.cn/idea/promo/?utm_source=baidu&utm_medium=cpc&utm_campaign=cn-bai-br-intellij-ex-pc&utm_content=intellij-core&utm_term=idea "IntelliJ IDEA") 找到gradle/wrapper/gradle-wrapper.properties 文件将修改distributionUrl为合适的版本,我们用由于国外很多gradle依赖链接需要翻墙,我们可以用ai帮我们找合适的地址: [(Gradle国内镜像)南京大学镜像站 下载目录![icon-default.png?t=O83A](https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png)https://mirrors.nju.edu.cn/gradle/](https://mirrors.nju.edu.cn/gradle/ "(Gradle国内镜像)南京大学镜像站 下载目录")我们可以找个相对合适的版本8.9 直接替换链接,如图 右键复制地址 ![](https://i-blog.csdnimg.cn/direct/4dc6cc34991242408ca47aae09cd0427.png) > ``` > distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip > 修改为https://mirrors.nju.edu.cn/gradle/gradle-8.9-bin.ziphttps://mirrors.nju.edu.cn/gradle/gradle-8.9-bin.zip > ``` > > 然后点击小象load图标 load Gradle Changes 同步 快捷键是(ctrl+shift+o) ### 2:JDK版本不兼容 > Your build is currently configured to use Java 21.0.2 and Gradle 8.5. > > Possible solution: > > - Use Java 17 as Gradle JVM: Open Gradle settings > > - Upgrade Gradle wrapper to 8.9 version and re-import the project > 您的构建当前配置为使用 Java 21.0.2 版本和 Gradle 8.5 版本。 > > 可能的解决方案: > > * 将 Java 17 用作 Gradle 的 Java 虚拟机(JVM):打开 Gradle 设置。 > * 将 Gradle 包装器升级到 8.5 版本,然后重新导入项目。 打开工程结构窗口将SDK修改17 File-\>Project Structure-\>Project-\>SDK: 选择对应版本17 没有的话选择Edit 选择Add SDK选择本地JDK版本,为什么用官方JDK,因为有时候编译打包会莫名其妙的报错,用官网就没问题(血泪的教训) [JDK17下载(强烈推荐用官网的JDK)![icon-default.png?t=O83A](https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png)https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html "JDK17下载(强烈推荐用官网的JDK)")![](https://i-blog.csdnimg.cn/direct/e560fe96a9ad4dd487d86caead683f47.png) 打开Settings输入Gradle JVM找到Build-\>BuildTools-\>Gradle-\>Gradle JVM将版本改成17 ![](https://i-blog.csdnimg.cn/direct/f1db8c3bf103493ea9c5d8e529979bc0.png) 下载JDK1[Java Archive Downloads![icon-default.png?t=O83A](https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png)https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html "Java Archive Downloads")下载对应的系统的版本例如 找到项目根目录local.properties文件添加本地配置属性 > > ``` > org.gradle.java.home=C\:\\Program Files\\Java\\jdk-17 > ``` 3:该项目正在使用不兼容版本(安卓 Gradle 插件 8.5.2 版)的安卓 Gradle 插件。 > The project is using an incompatible version (AGP 8.5.2) of the Android Gradle plugin. Latest supported version is AGP 8.2.0 > > See Android Studio \& AGP compatibility options. > > \[Experimental\] Enable sync with future AGP version. > 该项目正在使用不兼容版本(安卓 Gradle 插件 8.5.2 版)的安卓 Gradle 插件。所支持的最新版本是安卓 Gradle 插件 8.2.0 版。 > > 查看安卓工作室与安卓 Gradle 插件的兼容性选项。 > > \[实验性\] 启用与未来安卓 Gradle 插件版本的同步功能。 解决办法1: \[实验性的\] 启用与未来 Android Gradle 插件(AGP)版本的同步。我们可以打开Settings勾选 Enable sync with future AGP version (Experimental) 然后再点击小象load图标 load Gradle Changes 同步 快捷键是(ctrl+shift+o) ![](https://i-blog.csdnimg.cn/direct/c5d7a6bc2ded475681921a8c41f9e478.png) 解决办法2:下载最新的IDEA版本 [IntelliJ IDEA -- 领先的 Java 和 Kotlin IDE![icon-default.png?t=O83A](https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png)https://www.jetbrains.com.cn/idea/promo/?utm_source=baidu\&utm_medium=cpc\&utm_campaign=cn-bai-br-intellij-ex-pc\&utm_content=intellij-core\&utm_term=idea](https://www.jetbrains.com.cn/idea/promo/?utm_source=baidu&utm_medium=cpc&utm_campaign=cn-bai-br-intellij-ex-pc&utm_content=intellij-core&utm_term=idea "IntelliJ IDEA – 领先的 Java 和 Kotlin IDE") ### 3:Gradle依赖库连接超时-替换成国内镜像 * 在 Windows 系统中,hosts 文件位于 C:\\Windows\\System32\\Drivers\\etc 文件夹中,以记事本方式打开后,在文件底部添加类似 "20.205.243.166 [github.com](https://github.com/ "github.com")" 的内容,保存并关闭文件12. * **更改 DNS 服务器**:可以尝试更改 DNS 服务器地址为公共 DNS 服务,如谷歌的 8.8.8.8 和 8.8.4.4 或Cloudflare的 1.1.1.1 和 1.0.0.1。在计算机的控制面板中找到网络和 Internet 设置,点击正在使用的网络连接,进入其属性设置,在 IPv4 设置中选择 "使用以下 DNS 服务器地址",输入相应的 DNS 服务器地址并保存设置. ```Groovy pluginManagement { repositories { // 阿里云镜像 maven("https://maven.aliyun.com/release/") // 华为云镜像 maven("https://mirrors.huaweicloud.com/repository/maven/") // 腾讯云镜像 maven("https://mirrors.cloud.tencent.com/nexus/repository/maven-public/") maven("https://jitpack.io") maven("https://maven.aliyun.com/repository/gradle-plugin") maven("https://maven.aliyun.com/repository/google") maven("https://maven.aliyun.com/repository/central") maven("https://maven.aliyun.com/repository/public") //淘宝镜像源 maven("https://registry.npmmirror.com") //腾讯云镜像源 maven("https://mirrors.cloud.tencent.com/npm/") //官方 CNPM 镜像 maven("https://r.cnpmjs.org") google { mavenContent { includeGroupAndSubgroups("androidx") includeGroupAndSubgroups("com.android") includeGroupAndSubgroups("com.google") } } mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositories { // 阿里云镜像 maven("https://maven.aliyun.com/release/") // 华为云镜像 maven("https://mirrors.huaweicloud.com/repository/maven/") // 腾讯云镜像 maven("https://mirrors.cloud.tencent.com/nexus/repository/maven-public/") maven("https://jitpack.io") maven("https://maven.aliyun.com/repository/gradle-plugin") maven("https://maven.aliyun.com/repository/google") maven("https://maven.aliyun.com/repository/central") maven("https://maven.aliyun.com/repository/public") //淘宝镜像源 maven("https://registry.npmmirror.com") //腾讯云镜像源 maven("https://mirrors.cloud.tencent.com/npm/") //官方 CNPM 镜像 maven("https://r.cnpmjs.org") google { mavenContent { includeGroupAndSubgroups("androidx") includeGroupAndSubgroups("com.android") includeGroupAndSubgroups("com.google") } } mavenCentral() } } ``` 不管是运行Gradle的task哪个任务(kotlin browser-\>wasmJsBrowserRun)还是运行(other-\>wasmJsRun),提示的错误,都可以用以上国内镜像源代替: > Could not resolve com.yarnpkg:yarn:1.22.17. > > Required by: > > project : > > \> Could not resolve com.yarnpkg:yarn:1.22.17. > > \> Could not get resource 'https://github.com/yarnpkg/yarn/releases/download/v1.22.17/yarn-v1.22.17.tar.gz'. > > \> Could not HEAD 'https://github.com/yarnpkg/yarn/releases/download/v1.22.17/yarn-v1.22.17.tar.gz'. > > \> Got socket exception during request. It might be caused by SSL misconfiguration > > \> Connect to github.com:443 \[github.com/20.205.243.166\] failed: Connection timed out: no further information > Connect to [https://github.com:443](https://github.com/ "https://github.com:443") \[[github.com/20.205.243.166](https://github.com/20.205.243.166 "github.com/20.205.243.166")\] failed: Connection timed out: no further information. If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle ### 4:Android环境编译 Could not find compile target android-34 for modules :composeApp, :shared 点击composeApp\\src\\androidMain\\AndroidManifest.xml 会在右上角显示Install Android plugin 点击后依照提示Next就会自动下载安装,然后重启IDE就可以使用了 ![](https://i-blog.csdnimg.cn/direct/4811f1c2631d424590a585c2c78b413d.png) > SDK location not found. Define a valid SDK location with an ANDROID_HOME environment variable or by setting the sdk.dir path in your project's local properties file at 'D:\\pro\\ComposeMultiplatform\\local.properties'. > ``` 找到项目根目录local.properties文件添加本地配置属性,输入你的Android SDK本地路径 ``` > org.gradle.java.home=C\\:\\\\Program Files\\\\Java\\\\jdk-17 > > sdk.dir=D\\:\\\\Program Files\\\\Android\\\\sdk > Cannot locate tasks that match ':composeApp:compileJava' as task 'compileJava' is ambiguous in project ':composeApp'. Candidates are: 'compileDebugAndroidTestJavaWithJavac', 'compileDebugJavaWithJavac', 'compileDebugUnitTestJavaWithJavac', 'compileReleaseJavaWithJavac', 'compileReleaseUnitTestJavaWithJavac'. > > Clear Project 清除IDEA缓存 重启IDEA 打开settings配置Android SDK Manager ![](https://i-blog.csdnimg.cn/direct/dd985a452f4e41d3a0ef2367a98be55a.png) ### 5:Android 运行到手机/模拟器 打开Gradle task列表点击android-\>sourceSets和signingReport ![](https://i-blog.csdnimg.cn/direct/7fc90910ba0640188278ecfa20f95dbb.png) 点击Idea的Run 运行栏打开EditConfigurations ![](https://i-blog.csdnimg.cn/direct/9b818ea01d2f4faeb9b412ef86363e81.png) Run/Debug configurations选择android ,如下图所示,如果你的本地安装了android adb 现在你就可以在自己android手机上运行compose ![](https://i-blog.csdnimg.cn/direct/b60ea3aac1ba4311a553b20c63e19635.png) ### 6:IOS环境编译 (后续补充) 打开iosApp.xcodeproj 的projec.pb.xpbxproj 在右边会显示Install Multi-OS Engine plugin 点击安装以后 然后重启idea ![](https://i-blog.csdnimg.cn/direct/ac0f433b9c1440788381ed105857712f.png) 在Run/Debug configuratios就可以看到 Multi-OS Engine plugin的配置不过博主没有ios和注册ios相关开发账号 (后续补充) ![](https://i-blog.csdnimg.cn/direct/020eb5a4c2ff4321ba6f631b6e9cc756.png)

相关推荐
天天扭码几秒前
前端必看 | 用EditInPlace实现B站个性签名设计
前端·javascript·dom
隔壁小查14 分钟前
【后端开发】Spring配置文件
java·spring·microsoft
怪味&先森20 分钟前
关于IDEA中使用ctrl跳转源码出现???的解决方案
java·ide·intellij-idea
江沉晚呤时22 分钟前
CAP 定理与 BASE 定理在 .NET Core 中的应用
java·服务器·开发语言·前端·.netcore
魔云连洲23 分钟前
用Webpack 基础配置快速搭建项目开发环境
前端·webpack·node.js
自在如风。29 分钟前
Java 设计模式:组合模式详解
java·设计模式·组合模式
PuddingSama32 分钟前
Android 视图转换工具 Matrix
android·前端·面试
未定义.22133 分钟前
Java设计模式实战:装饰模式在星巴克咖啡系统中的应用
java·开发语言·设计模式·软件工程
创码小奇客36 分钟前
MongoDB 增删改查:从青铜到王者的全攻略
java·mongodb·trae
OpenTiny社区40 分钟前
TinyPro 1.2.0 正式发布:增加综合搜索,解决数据筛选难题,后端单测覆盖率再提升!
前端·vue.js·github