添加runtime-livedata依赖后PrimitiveSnapshotStateKt解析失败求助
问题
基于Jetpack Compose和Dagger Hilt的项目原本运行正常,添加implementation "androidx.compose.runtime:runtime-livedata:1.6.0-alpha03"依赖后,运行时抛出以下错误:
FATAL EXCEPTION: main Process: com.urahimli.asdf, PID: 3702 java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/compose/runtime/PrimitiveSnapshotStateKt;
模块build.gradle内容
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id "com.google.dagger.hilt.android" } android { namespace 'com.urahimli.asdfg' compileSdk 34 defaultConfig { applicationId "com.urahimli.asdfg" minSdk 26 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.3.2' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.10.1' implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0') implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' implementation 'androidx.activity:activity-compose:1.7.2' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' // Compose dependencies implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" implementation "androidx.navigation:navigation-compose:2.7.0" implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01" // Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.2' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.2' //Dagger - Hilt implementation 'com.google.dagger:hilt-android:2.46.1' kapt 'com.google.dagger:hilt-android-compiler:2.46.1' kapt "androidx.hilt:hilt-compiler:1.0.0" // Room implementation "androidx.room:room-runtime:2.5.2" kapt "androidx.room:room-compiler:2.5.2" // Kotlin Extensions and Coroutines support for Room implementation "androidx.room:room-ktx:2.5.2" //material 3 implementation("androidx.compose.material3:material3:1.1.1") implementation "androidx.compose.material:material:1.5.0" implementation("androidx.compose.material:material-icons-extended:1.5.0") // coil for images implementation("io.coil-kt:coil-compose:2.4.0") // system ui controller (status bar) implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0" implementation "com.google.accompanist:accompanist-swiperefresh:0.28.0" // date picker implementation "io.github.vanpra.compose-material-dialogs:datetime:0.8.1-rc" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' // implementation "androidx.compose.runtime:runtime-livedata:1.6.0-alpha03" } kapt { correctErrorTypes = true }
项目根build.gradle内容
plugins { id 'com.android.application' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false id("com.google.dagger.hilt.android") version "2.44" apply false }
判断为版本兼容性问题,需解决。
解决方案
这个错误确实是Compose runtime版本不匹配导致的:你添加的runtime-livedata:1.6.0-alpha03属于Compose 1.6.x系列,但项目中通过Compose BOM 2022.10.00引入的核心Compose库(如ui、material3等)都是1.4.x左右的版本,而PrimitiveSnapshotStateKt是Compose 1.5+才新增的API,低版本核心库中没有这个类,因此抛出找不到类的异常。
提供两种解决思路:
方案一:统一Compose版本到与runtime-livedata匹配的系列
- 替换Compose BOM版本:将
androidx.compose:compose-bom:2022.10.00替换为androidx.compose:compose-bom:2024.04.00(该BOM对应Compose 1.6.x稳定版) - 升级Kotlin Compiler Extension Version:将
composeOptions中的kotlinCompilerExtensionVersion '1.3.2'改为'1.5.1'(Compose 1.6.x需搭配KCE 1.5.x,同时建议将根build.gradle中的Kotlin版本从1.7.20升级到1.8.20或更高) - 同步项目后重新构建
方案二:降级runtime-livedata到与现有Compose版本匹配的版本
若不想升级整个Compose版本,可将implementation "androidx.compose.runtime:runtime-livedata:1.6.0-alpha03"改为implementation "androidx.compose.runtime:runtime-livedata:1.4.3"(与BOM 2022.10.00对应的版本),同步后重新构建即可。
额外注意
根build.gradle中的Hilt版本(2.44)与模块中使用的Hilt版本(2.46.1)不一致,建议统一为2.46.1,避免潜在兼容性问题。
内容的提问来源于stack exchange,提问作者oolyvi
相关产品推荐
相关产品推荐

