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

Android项目控制台应用/单元测试调用limitedParallelism遇NoSuchMethodError

解决kotlinx.coroutines.CoroutineDispatcher.limitedParallelism方法找不到的问题

问题原因

limitedParallelism()是Kotlin Coroutines 1.6.0版本才新增的API,出现NoSuchMethodError的核心原因是:

  • 项目依赖的kotlinx-coroutines-core版本低于1.6.0,本身就没有这个方法
  • 项目中存在多个不同版本的kotlinx-coroutines依赖,运行时加载了旧版本的库,导致找不到该方法

解决方案

1. 统一升级Coroutines依赖版本

确保所有kotlinx-coroutines相关依赖(包括核心库和测试库)都升级到1.6.0或更高版本,且版本一致:

Groovy版build.gradle:

dependencies {
    // 核心库
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
    // 测试库(如果用JUnit测试)
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
}

Kotlin DSL版build.gradle.kts:

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
}

2. 排查并解决依赖冲突

如果升级后仍报错,可能是间接依赖引入了旧版本的Coroutines库,需要强制统一版本:

Groovy版build.gradle:

configurations.all {
    resolutionStrategy.force 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
    resolutionStrategy.force 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
}

Kotlin DSL版build.gradle.kts:

configurations.all {
    resolutionStrategy.force("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    resolutionStrategy.force("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
}

可以通过执行./gradlew dependencies命令查看所有依赖树,确认是否存在版本不一致的情况。

3. 清理构建缓存并重建项目

执行以下命令清理旧的构建缓存,确保新依赖生效:

./gradlew clean
./gradlew build

补充说明

不管是控制台应用还是JUnit测试,它们的运行时类路径都依赖kotlinx-coroutines库,只要库版本不支持limitedParallelism()方法,就会触发相同的错误,和是否注入作用域、是否是生产环境无关。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 19:54:54