You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle中引入moko-socketio库报错,求解决方案?

解决moko-socketio依赖添加错误的问题

问题描述

想要使用moko-socketio库,但按照文档指引添加依赖后出现错误。无论是使用常规库还是CocoaPods类型的库,都会出现相同错误。不清楚依赖添加位置是否正确,寻求解决方法。

原Shared build.gradle代码

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
        }
    }
    
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.sn.myapplication"
    compileSdk = 32
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

dependencies {
    commonMainApi("dev.icerock.moko:socket-io:0.3.0")
    commonMainApi("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1")
}

// HERE IS ERROR
cocoaPods {
    podsProject = file("../ios-app/Pods/Pods.xcodeproj") // here should be path to Pods xcode project

    pod("mokoSocketIo", onlyLink = true)
}

错误截图

错误截图

解决步骤

  1. 合并重复的cocoaPods配置块
    你在gradle脚本中定义了两个独立的cocoaPods块:一个在kotlin{}内部,另一个在脚本最外层。这会导致gradle配置冲突,必须删除外层的cocoaPods块,将其中的配置内容移到kotlin{}内部已有的cocoaPods块里。

  2. 修正podsProject路径
    原代码中路径写的是../ios-app/Pods/Pods.xcodeproj,但你的podfile路径是../iosApp/Podfile,文件夹名称大小写不匹配,需要改成和实际文件夹一致的路径:../iosApp/Pods/Pods.xcodeproj。

  3. 修正后的完整配置
    调整后的Shared build.gradle代码如下:

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        podsProject = file("../iosApp/Pods/Pods.xcodeproj") 
        framework {
            baseName = "shared"
        }
        pod("mokoSocketIo", onlyLink = true)
    }
    
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "com.sn.myapplication"
    compileSdk = 32
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

dependencies {
    commonMainApi("dev.icerock.moko:socket-io:0.3.0")
    commonMainApi("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1")
}
  1. 后续验证步骤
    • 执行./gradlew clean清理缓存
    • 重新同步gradle配置
    • 如果是iOS端,进入iosApp目录执行pod install更新依赖

内容的提问来源于stack exchange,提问作者Saeed Noshadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 00:45:54