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

使用Android导航架构组件遇依赖错误:Failed to resolve: androidx.lifecycle:lifecycle-viewmodel-ktx:1.1.1

解决Failed to resolve: androidx.lifecycle:lifecycle-viewmodel-ktx:1.1.1的问题

我一眼就看出问题所在了——你混淆了旧的Android Architecture Components(android.arch.lifecycle)和AndroidX Lifecycle组件的版本号与依赖结构,这直接导致了依赖找不到的错误。

问题根源拆解

  1. 版本号不匹配:AndroidX的Lifecycle组件根本没有1.1.1这个版本!旧的android.arch.lifecycle系列最高稳定版是1.1.1,但AndroidX迁移后,Lifecycle组件的版本从2.0.0开始迭代,所以你写的androidx.lifecycle:lifecycle-viewmodel-ktx:1.1.1是不存在的依赖。
  2. 依赖体系冲突:你同时引入了旧的android.arch.lifecycle:extensions和AndroidX的androidx.lifecycle:lifecycle-viewmodel-ktx,这两个属于完全不同的依赖体系,不能混合使用。

两种解决方案(选其一即可)

方案1:继续使用旧的Support库(不推荐,已停止维护)

如果你暂时不想迁移到AndroidX,只需移除AndroidX的Lifecycle依赖,保留旧的Architecture Components即可:

dependencies {
    def lifecycle_version = "1.1.1"
    // 旧版Architecture Components已包含ViewModel和LiveData,无需额外依赖
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    kapt "android.arch.lifecycle:compiler:$lifecycle_version" // 若需使用@ViewModelInject等注解则添加

    // 保留其他旧Support库依赖
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    
    // 移除这行错误的AndroidX依赖
    // implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

⚠️ 注意:旧的Architecture Components没有Kotlin扩展(-ktx)版本,所以不能用lifecycle-viewmodel-ktx,只能使用普通的ViewModel API。

方案2:迁移到AndroidX(推荐,Google官方支持)

这是长远的解决方案,因为Google已经停止维护旧的Support库,后续新特性只会在AndroidX中更新。步骤如下:

  1. 开启AndroidX迁移
    在项目根目录的gradle.properties文件中添加以下配置:
android.useAndroidX=true
android.enableJetifier=true

Jetifier会自动将第三方库中的旧Support依赖转换为AndroidX依赖。

  1. 更新项目配置与依赖
    修改你的app/build.gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 33 // 升级到至少28,推荐用最新稳定版
    defaultConfig {
        applicationId "com.mobile.codgin.newnavigation"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        // 替换旧的Test Runner为AndroidX版本
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def lifecycle_version = "2.6.2" // AndroidX Lifecycle最新稳定版
    def kotlin_version = "1.8.22" // 推荐使用最新Kotlin版本
    def appcompat_version = "1.6.1"
    def constraintlayout_version = "2.1.4"

    // AndroidX Lifecycle组件(带Kotlin扩展)
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    // AndroidX基础依赖
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version"

    // AndroidX测试依赖
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
}
  1. 同步项目
    点击Android Studio右上角的Sync Now按钮,等待依赖同步完成即可。

最后提醒

如果你要使用Android新导航架构组件(Navigation Component),必须迁移到AndroidX,因为Navigation Component本身就是AndroidX库的一部分,和旧的Support库不兼容。所以方案2是你最终的选择哦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:31:14