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

Compose Multiplatform集成Room与SQLite同步失败求助

Compose Multiplatform添加Room与sqlite-bundled依赖同步失败问题

问题详情

在为Compose Multiplatform应用集成Room和sqlite-bundled依赖时,项目同步出现以下依赖解析错误:

:composeApp:wasmJsMain: Could not resolve androidx.room:room-runtime:2.7.0-beta01.
Required by:
    project :composeApp

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

:composeApp:wasmJsMain: Could not resolve androidx.sqlite:sqlite-bundled:2.5.0-beta01.
Required by:
    project :composeApp

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

:composeApp:wasmJsTest: Could not resolve androidx.room:room-runtime:2.7.0-beta01.
Required by:
    project :composeApp

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

:composeApp:wasmJsTest: Could not resolve androidx.sqlite:sqlite-bundled:2.5.0-beta01.
Required by:
    project :composeApp

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

使用的依赖版本:

sqlite = "2.5.0-beta01"
room = "2.7.0-beta01"

已配置的settings.gradle.kts:

pluginManagement {
    repositories {
        google {
            mavenContent {
                includeGroupAndSubgroups("androidx")
                includeGroupAndSubgroups("com.android")
                includeGroupAndSubgroups("com.google")
            }
        }
        mavenCentral()
        gradlePluginPortal()
    }
}

dependencyResolutionManagement {
    repositories {
        google {
            mavenContent {
                includeGroupAndSubgroups("androidx")
                includeGroupAndSubgroups("com.android")
                includeGroupAndSubgroups("com.google")
            }
        }
        mavenCentral()
    }
}

项目级build.gradle.kts片段:

plugins {
    ...
    alias(libs.plugins.ksp) apply false
    alias(libs.plugins.room) apply false
}

composeApp模块build.gradle.kts片段:

plugins {
    ...
    alias(libs.plugins.ksp)
    alias(libs.plugins.room)
}


kotlin {
    androidTarget {
        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_11)
        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
    }

    jvm("desktop")

    @OptIn(ExperimentalWasmDsl::class)
    wasmJs {
        ...
    }

    room {
        schemaDirectory("$projectDir/schemas")
    }

    sourceSets {
        val desktopMain by getting

        androidMain.dependencies {
            ...
        }
        commonMain.dependencies {
            ...
            implementation(libs.androidx.room.runtime)
            implementation(libs.sqlite.bundled)
        }
        desktopMain.dependencies {
            ...
        }

        nativeMain.dependencies {
            ...
        }

        wasmJsMain.dependencies {
            ...
        }

        dependencies {
            ksp(libs.androidx.room.compiler)
        }
    }
}

已尝试降级依赖版本,但问题未解决。


解决方案

1. 添加AndroidX快照仓库

Room 2.7.0-beta01的Wasm目标构件处于实验阶段,可能仅发布在AndroidX的快照仓库中。在settings.gradle.kts的dependencyResolutionManagement块中添加该仓库:

dependencyResolutionManagement {
    repositories {
        google {
            mavenContent {
                includeGroupAndSubgroups("androidx")
                includeGroupAndSubgroups("com.android")
                includeGroupAndSubgroups("com.google")
            }
        }
        mavenCentral()
        // 添加AndroidX快照仓库
        maven("https://androidx.dev/snapshots/builds/8755556/artifacts/repository")
    }
}

2. 为Wasm目标单独处理依赖

如果快照仓库无法解决问题,可暂时为Wasm目标排除Room和sqlite-bundled,改用Wasm兼容的SQLite库,待正式版本发布后再切换:

sourceSets {
    commonMain.dependencies {
        ...
        // 仅为非Wasm目标添加Room和sqlite-bundled
        implementation(libs.androidx.room.runtime) {
            exclude("org.jetbrains.kotlin", "kotlin-stdlib-wasm-js")
        }
        implementation(libs.sqlite.bundled) {
            exclude("org.jetbrains.kotlin", "kotlin-stdlib-wasm-js")
        }
    }

    wasmJsMain.dependencies {
        // 使用Wasm兼容的SQLite库替代
        implementation("org.sqlite:sqlite-wasm:3.45.1")
    }
}

3. 验证版本映射配置

检查libs.versions.toml中的依赖映射是否正确,确保没有拼写错误:

[versions]
room = "2.7.0-beta01"
sqlite = "2.5.0-beta01"

[libraries]
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
sqlite-bundled = { group = "androidx.sqlite", name = "sqlite-bundled", version.ref = "sqlite" }

4. 清理Gradle缓存

执行以下命令清理缓存并重新同步项目:

./gradlew clean build --refresh-dependencies

内容的提问来源于stack exchange,提问作者Abanoub Samir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 06:59:50