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

Android Studio默认Java项目依赖解析失败,求解决方案

依赖转换失败问题分析与解决

问题背景

在Android Studio 3.5.2中创建基于API 19的Java项目,app模块build.gradle代码如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 34
    buildToolsVersion "34.0.0"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 34
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

项目创建后立即出现以下错误:

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Failed to transform artifact 'activity.aar (androidx.activity:activity:1.6.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Failed to transform artifact 'annotation-experimental.aar (androidx.annotation:annotation-experimental:1.3.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Failed to transform artifact 'activity.aar (androidx.activity:activity:1.6.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Failed to transform artifact 'annotation-experimental.aar (androidx.annotation:annotation-experimental:1.3.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Failed to transform artifact 'activity.aar (androidx.activity:activity:1.6.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Failed to transform artifact 'annotation-experimental.aar (androidx.annotation:annotation-experimental:1.3.0)' to match attributes {artifactType=jar}.
Show Details
Affected Modules: app

已尝试Clean Project、Invalidate Caches / Restart、删除Gradle文件、切换离线模式等操作,均无效。

问题原因

Android Studio 3.5.2是2019年发布的旧版本,配套的Gradle插件版本(默认3.5.2)无法兼容compileSdkVersion 34(Android 14)以及高版本的AndroidX依赖库。高版本AndroidX依赖的构建逻辑与旧版Gradle插件不匹配,导致无法完成AAR到JAR的格式转换,从而抛出依赖解析错误。

解决方案

方案1:降低编译配置与依赖版本(无需升级AS)

修改app/build.gradle,将编译版本调整为AS 3.5.2支持的最高版本(如API 28),同时替换为兼容的AndroidX依赖版本:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // 使用兼容API 19和AS 3.5.2的AndroidX版本
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

修改后执行Build -> Rebuild Project即可。

方案2:升级Android Studio到最新稳定版

如果项目允许,直接将Android Studio升级到最新稳定版(如Iguana | 2023.2.1),新版本的Gradle插件完全支持compileSdkVersion 34和高版本AndroidX依赖,升级后重新同步项目即可解决问题。

方案3:指定兼容的AndroidX依赖版本

如果不想降低compileSdkVersion,可尝试指定与AS 3.5.2兼容的AndroidX依赖版本,同时排除冲突的传递依赖:

implementation('androidx.appcompat:appcompat:1.2.0') {
    exclude group: 'androidx.activity', module: 'activity'
    exclude group: 'androidx.annotation', module: 'annotation-experimental'
}
// 手动添加兼容的旧版本依赖
implementation 'androidx.activity:activity:1.0.0'
implementation 'androidx.annotation:annotation-experimental:1.0.0'

这种方式需要逐个调整依赖,可能会引发其他兼容问题,优先推荐方案1或方案2。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 22:12:07