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

Android Studio同步报错:无法加载KotlinJvmCompilerOptionsHelper类

问题:同步Gradle项目时无法加载类'org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptionsHelper'

环境信息

  • Android Studio:Hedgehog | 2023.1.1 Canary 16
  • Java:17.0.7
  • Gradle:8.3

问题描述

创建简单Android项目并添加依赖后,同步Gradle时触发错误:无法加载类'org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptionsHelper'。已尝试以下操作但均未解决问题:

  • 执行 ./gradlew cleanBuildCache
  • 降级Gradle和AGP版本
  • 清理Gradle缓存
  • 重启IDE
  • 升级Java版本

项目配置文件

libs.versions.toml

[versions]
agp = "8.1.0"
kotlin = "1.8.22"
ksp = "1.9.0-1.0.13"
hilt = "2.47"
core-ktx = "1.10.1"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.9.0"

[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
daggerHilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kotlinKsp = { id = "com.google.devtools.ksp", version.ref = "ksp" }

根目录build.gradle.kts

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlinKsp) apply false
alias(libs.plugins.daggerHilt) apply false
}
true // Needed to make the Suppress annotation work for the plugins block

app模块build.gradle.kts

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.kotlinKsp)
alias(libs.plugins.daggerHilt)
}

android {
namespace = "com.example"
compileSdk = 34

defaultConfig {
applicationId = "com.example"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}

dependencies {

implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)


//Material3
implementation "androidx.compose.material:material:$compose_version"
implementation("androidx.compose.material:material-icons-extended-android:1.5.0")

//Compose
implementation("androidx.compose.ui:ui:1.5.0")
implementation("androidx.compose.ui:ui-tooling-preview:")
debugImplementation("androidx.compose.ui:ui-tooling:1.5.0")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.5.0")
implementation("androidx.activity:activity-compose:1.7.2")

//lifecycler
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
implementation("androidx.navigation:navigation-compose:2.7.0")
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")

//Tests
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.5.0")

//Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")


    //Dagger - Hilt
implementation("androidx.hilt:hilt-android:2.47")
ksp("com.google.dagger:hilt-android-compiler:2.47")


//Room Database
implementation("androidx.room:room-ktx:2.5.2")
ksp("androidx.room:room-compiler:2.5.2")
//Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:2.5.2")

androidTestImplementation("androidx.room:room-testing:2.5.2")
}

settings.gradle.kts

pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}

rootProject.name = "Example"
include(":app")

gradle.properties

org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
kotlin.code.style=official
android.nonTransitiveRClass=true

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

解决方案

1. 对齐Kotlin与KSP版本

当前配置中Kotlin版本为1.8.22,但KSP版本是1.9.0-1.0.13,版本不匹配会引发兼容性问题。修改libs.versions.toml中的KSP版本为对应Kotlin 1.8.22的兼容版本:

ksp = "1.8.22-1.0.11"

2. 修复Compose依赖的版本问题

  • app模块中implementation("androidx.compose.ui:ui-tooling-preview:")缺失版本号,补充为与其他Compose依赖一致的1.5.0:
    implementation("androidx.compose.ui:ui-tooling-preview:1.5.0")
    
  • implementation "androidx.compose.material:material:$compose_version"中的$compose_version未定义,直接替换为具体版本:
    implementation "androidx.compose.material:material:1.5.0"
    

3. 彻底清理缓存后重新同步

执行以下命令清理项目和全局Gradle缓存:

./gradlew clean
rm -rf ~/.gradle/caches

重启Android Studio后重新同步项目。

4. 验证AGP与Gradle版本兼容性

AGP 8.1.0兼容Gradle 7.4+,但如果问题仍存在,可尝试将AGP版本升级到8.2.0或8.3.0,保持与Gradle 8.3的适配性。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 17:44:51