错误:无法在Android Studio 3.4中解析依赖项。

9

我之前使用的是 Android Studio 3.3,一切都很正常。但今天升级到 3.4 后,出现了这些错误。看起来问题源于 androidX 依赖库。这种情况在我依赖 androidX 的两个不同项目中也发生了。

我尝试过以下操作:

1. 清除并重新构建我的项目

2. 使缓存无效,并重启 Android Studio。

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve androidx.cursoradapter:cursoradapter:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve androidx.drawerlayout:drawerlayout:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve androidx.cursoradapter:cursoradapter:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve androidx.drawerlayout:drawerlayout:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve androidx.cursoradapter:cursoradapter:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve androidx.drawerlayout:drawerlayout:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve androidx.cursoradapter:cursoradapter:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve androidx.drawerlayout:drawerlayout:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve androidx.cursoradapter:cursoradapter:1.0.0.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve androidx.drawerlayout:drawerlayout:1.0.0.
Show Details
Affected Modules: app

我的 build.gradle (app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs"
apply plugin: 'com.apollographql.android'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "********"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0-alpha01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    //Apollo
    implementation 'com.apollographql.apollo:apollo-runtime:1.0.0-alpha5'
    implementation 'com.apollographql.apollo:apollo-http-cache:1.0.0-alpha5'
    implementation "com.apollographql.apollo:apollo-coroutine-support:1.0.0-alpha5"

    //okhttp3
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

    //Anko
    implementation "org.jetbrains.anko:anko:$anko_version"
    implementation "org.jetbrains.anko:anko-design:$anko_version"

    //Dependency injection
    implementation 'com.google.dagger:dagger:2.16'
    kapt 'com.google.dagger:dagger-compiler:2.16'

    //Room
    implementation "androidx.room:room-runtime:2.1.0-alpha06"
    implementation "androidx.room:room-coroutines:2.1.0-alpha04"
    kapt "androidx.room:room-compiler:2.1.0-alpha06"

    implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04"
    implementation "androidx.paging:paging-runtime-ktx:2.1.0"

    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
    implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'

    //Navigation
    implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
    implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"

    //Fresco - Image Loader.
    implementation 'com.facebook.fresco:fresco:1.12.1'

    //Maps and Location
    implementation "com.google.android.gms:play-services-location:16.0.0"
    implementation "com.google.android.gms:play-services-maps:16.1.0"

    //Preference
    implementation "androidx.preference:preference:1.0.0"

    //Multidex
    implementation 'androidx.multidex:multidex:2.0.1'

    //Google Ads
    implementation 'com.google.android.gms:play-services-ads:17.2.0'

    //ImagePicker.
    implementation 'com.github.nguyenhoanglam:ImagePicker:1.3.1'
}

build.gradle项目级别

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        anko_version = '0.10.8'
        kotlin_version = '1.3.30'
    }

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
        classpath 'com.apollographql.apollo:apollo-gradle-plugin:1.0.0-alpha5'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
        maven { //For coroutine support
            url 'http://dl.bintray.com/apollographql/android'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


我该如何解决它。

这可能是由于多种问题引起的。 1. 检查您的网络配置,可能您的网络正处于代理配置下 2. 打开新项目而不是现有项目,然后一切都可以正确编译,然后根据成功构建的项目配置旧项目 3. 如果启用了脱机功能,请禁用它。 4. 良好的网络连接也可能是答案。逐个尝试它们。 因为这经常发生在我身上,上述其中一种方法总是帮助了我。顺便提一句,在每个步骤之后不要忘记“同步项目”。 - Jasurbek
你需要在你的项目的gradle.properties文件中添加以下规则:android.enableJetifier=true android.useAndroidX=true - madhu s
当我尝试将旧式即时应用程序转换为新的动态模块时,我遇到了相同的问题。它发生在已安装的功能中。这一定是Gradle中的错误。糟糕的实现。 - Gillis Haasnoot
@GillisHaasnoot解决了吗? - Nux
是的,我已经进展了一点。 我的问题在于,在动态模块中没有包含核心Android X库,因此出现了错误。 因此,我创建了一个单独的“库”(不是动态模块),其中包含所有核心依赖项,例如: androidx.core:core:1.1.0-alpha05 androidx.fragment:fragment:1.1.0-alpha07 com.google.firebase:firebase-core:16.0.8 等等。我将该库包含在所有动态模块中。 现在至少可以构建了。当然,我现在面临一组全新的完全不同的问题,我必须解决 :) - Gillis Haasnoot
当我升级到gradle 5.1.1时,我遇到了相同的问题。清除缓存无效。 - ScruffyFox
2个回答

10
我通过将 gradle-wrapper.properties 中的 distributionUrl 属性还原,解决了我的问题。

还原前:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

返回

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

还需要在 build.gradle 文件中将 gradle 依赖项恢复为先前的版本。

classpath 'com.android.tools.build:gradle:3.4.0'

返回

classpath 'com.android.tools.build:gradle:3.3.2'

现在我的项目很好。


1
但是你能否在不降级的情况下解决这个问题呢? - Dr.jacky
@Dr.jacky 我尝试了很多次都失败了。我在等待下一个Gradle版本的发布,也许这个版本会有一些bug! - Nux
我在这里等待回复:https://dev59.com/ilMI5IYBdhLWcg3wmcwN#ox4moYgBc1ULPQZFzUDS - Dr.jacky
1
我也遇到了同样的问题。通过将每个库更新到最新版本来解决了这个问题。 - APP Bird

0
将以下内容添加到您的应用程序模块依赖列表中。
implementation 'androidx.drawerlayout:drawerlayout:1.0.0-alpha1'
implementation 'androidx.cursoradapter:cursoradapter:1.0.0-alpha1'

谢谢您的回复。我已经添加了这些依赖项,但仍然面临同样的问题。 - Nux

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接