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

更新依赖后Compose动画(AnimateDpAsState等)失效问题

问题描述

更新Compose依赖解决Modifier.blur的初始报错后,AnimateDpAsState与AnimatedVisibility两个动画功能失效;Release模式构建时出现以下Kotlin版本不兼容警告(Debug模式无此问题):

lifecycle-runtime-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

确定问题出在Gradle配置中,相关配置文件如下:

模块级build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.dagger.hilt.android'
    id "org.jetbrains.kotlin.kapt"
}

android {
    namespace 'com.example.app'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.app"
        minSdk 21
        targetSdk 33
        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.4.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

    // Compose 
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0-alpha05"
    implementation "androidx.navigation:navigation-compose:2.6.0-alpha04"
    implementation "androidx.compose.material:material-icons-extended:1.3.1"
    implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01"


    //Dagger - Hilt
    implementation 'com.google.dagger:hilt-android:2.44.2'
    kapt 'com.google.dagger:hilt-android-compiler:2.44.2'

    // Room
    implementation "androidx.room:room-runtime:2.5.0"
    kapt "androidx.room:room-compiler:2.5.0"

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

    // Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:2.5.0"
}

项目级build.gradle

buildscript {
    ext {
        compose_ui_version = '1.3.3'
    }
}
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
    id 'com.google.dagger.hilt.android' version '2.44.2' apply false
    id "org.jetbrains.kotlin.kapt" version "1.8.0" apply false
}

仅更新了Android Studio提示的依赖与Compose版本,未添加新依赖或修改代码,期望功能与之前一致但动画失效。


解决方案

问题核心是Kotlin版本、Compose版本、Compose Compiler版本三者不匹配,同时存在多依赖版本不一致的问题,导致动画逻辑异常和版本警告。按以下步骤修复:

1. 对齐Compose Compiler与Kotlin版本

Compose Compiler版本必须和Kotlin版本严格对应,当前Kotlin用1.8.0,搭配的Compose Compiler应为1.4.3(适配Compose 1.3.3)。修改模块级build.gradle:

composeOptions {
    kotlinCompilerExtensionVersion '1.4.3'
}

2. 统一Lifecycle依赖版本

当前同时使用lifecycle-runtime-ktx:2.5.1和lifecycle-viewmodel-compose:2.6.0-alpha05,版本差异会引发兼容性问题,升级到稳定版:

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2"

3. 升级Navigation Compose到稳定版

测试版navigation-compose:2.6.0-alpha04存在潜在兼容性问题,替换为稳定版:

implementation "androidx.navigation:navigation-compose:2.6.0"

4. 升级Coroutines版本适配Kotlin 1.8.0

Kotlin 1.8.0建议搭配Coroutines 1.7.3及以上版本,更新依赖:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'

5. 清理缓存并重新构建

完成修改后执行:

  • 点击File → Invalidate Caches... → Invalidate and Restart重启Android Studio
  • 执行命令./gradlew clean build清理构建缓存并重新编译

以上操作可解决版本不兼容警告,同时修复动画失效问题——版本不匹配会导致Compose动画库内部逻辑无法正常执行。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 04:15:49