Android Studio 3.0:无法解析依赖项:对于:app @ dexOptions / compileClasspath',无法解析项目:animators。

175

我迁移到了Android Studio 3.0。因此,项目无法编译名为“:animator”的模块,并显示以下错误:

 Error:Unable to resolve dependency for
 ':app@dexOptions/compileClasspath': Could not resolve project
 :animators. <a
 href="openFile:/home/mobilepowered/MobilePowered/MyInnovalee/trunk17-10-2017/app/build.gradle">Open
 File</a><br><a href="Unable to resolve dependency for
 &#39;:app@dexOptions/compileClasspath&#39;: Could not resolve project
 :animators.">Show Details</a>

并显示详细信息会给出这个日志:
 Unable to resolve dependency for ':app@dexOptions/compileClasspath':
 Could not resolve project :animators.

 Could not resolve project :animators. Required by:
     project :app
 Unable to find a matching configuration of project :animators:
      - Configuration 'debugApiElements':
          - Required com.android.build.api.attributes.BuildTypeAttr 'dexOptions' and found incompatible value 'debug'.
          - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and
 found compatible value 'Aar'.
          - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but
 wasn't required.
          - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
      - Configuration 'debugRuntimeElements':
          - Required com.android.build.api.attributes.BuildTypeAttr 'dexOptions' and found incompatible value 'debug'.
          - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and
 found compatible value 'Aar'.
          - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but
 wasn't required.
          - Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.
      - Configuration 'releaseApiElements':
          - Required com.android.build.api.attributes.BuildTypeAttr 'dexOptions' and found incompatible value 'release'.
          - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and
 found compatible value 'Aar'.
          - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but
 wasn't required.
          - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
      - Configuration 'releaseRuntimeElements':
          - Required com.android.build.api.attributes.BuildTypeAttr 'dexOptions' and found incompatible value 'release'.
          - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and
 found compatible value 'Aar'.
          - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but
 wasn't required.
          - Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.

你做了什么? - Pandiri Deepak
作为使项目在Android Studio 3.0中运行的第一个临时解决方案,我维护 distributionUrl=https://services.gradle.org/distributions/gradle-3.3-all.zipcompileSdkVersion 25buildToolsVersion "25.0.3" 以及 **classpath 'com.android.tools.build:gradle:2.3.3'**。 - Imene Noomene
@ImeneNoomene,应该选择JackHuang的答案。它基于官方文档,而Sackurise所说的并不总是可行的(想想React Native项目,其中大多数模块都来自Web)。 - Leo supports Monica Cellio
我遇到了由于多种口味库而引起的这个问题。解决方案来自: https://dev59.com/qGAf5IYBdhLWcg3wQQu4#48718124 - Yeung
我通过简单地更新“Android Studio”解决了我的问题。 - Frank Goortani
32个回答

5

我也曾面临过同样的问题。

我通过参考上述“解决方案2”并使用新版本的gradle-4.1来修复了这个问题:

我进行了以下gradle更改。似乎通过maven Amazon下载资源帮助我解决了这个问题,appcompat库存在问题。请检查并确保您的系统中下载了与appcompat兼容的支持库。我认为,只是资源没有通过maven下载,导致编译错误。请确保在您的本地驱动器中找到这些资源以解决此问题。

仅仅是玩弄了一下

  1. Maven Amazon url
repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
    jcenter()
}
  1. 在驱动器中下载兼容的支持库,并在gradle中引用兼容的库。
dependencies {
    implementation fileTree(dir: 'libs', include: \['*.jar'\])
    implementation 'com.android.support:appcompat-v7:26.0.0-alpha1'
    implementation  'com.android.support:support-v4:26.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

}

完整文件

        apply plugin: 'com.android.application'

        repositories {
            maven {
                url "https://s3.amazonaws.com/repo.commonsware.com"
            }
            jcenter()
        }

        android {
            compileSdkVersion 26
            defaultConfig {
                applicationId "com.cognizant.interviewquestions.cognizantiqpractice2"
                minSdkVersion 18
                targetSdkVersion 26
                versionCode 1
                versionName "1.0"

                javaCompileOptions {
                    annotationProcessorOptions {
                        includeCompileClasspath false
                    }
                }

                dexOptions {
                    javaMaxHeapSize "4g"
                    preDexLibraries = false
                }
            }

            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                }
            }
        }

        dependencies {
            implementation fileTree(dir: 'libs', include: \['*.jar'\])
            implementation 'com.android.support:appcompat-v7:26.0.0-alpha1'
            implementation  'com.android.support:support-v4:26.0.0-alpha1'
            implementation 'com.android.support.constraint:constraint-layout:1.0.2'

        }
---------------------------------------------------------------------------

enter image description here

enter image description here


5

将gradle中的代码更改为:

  compile project(':yourLibrary')

to

   implementation project(path: ': yourLibrary', configuration:'default')

4
我找到了两种解决方案:
  1. 使用旧版gradle-3.3的解决方案:

    作为第一步和临时解决方案,以使项目在Android Studio 3.0中运行,我保持了之前Android Studio 2.3的旧配置。

    distributionUrl=https://services.gradle.org/distributions/gradle-3.3-all.zip,compileSdkVersion 25 和 buildToolsVersion "25.0.3",以及classpath 'com.android.tools.build:gradle:2.3.3'

  2. 使用新版gradle-4.1的解决方案:

    为了使用gradle 4.1的新功能和classpath com.android.tools.build:gradle:3.0.0',我按照此链接https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html。因此,这是我的实现:

在gradle-wrapper.properties文件中:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

在项目的文件build.gradle中:
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        //classpath 'me.tatarka:gradle-retrolambda:3.3.1' remove this line
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

在应用的 build.gradle 文件中:
apply plugin: 'com.android.application'
//apply plugin: 'me.tatarka.retrolambda' remove this line

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
    jcenter()
    mavenCentral()
    maven { url "https://jitpack.io" }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.imennmn.myprojectid"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        /**
         * Enabling multidex support.
         */
        multiDexEnabled true
        missingDimensionStrategy 'minApi' , 'minApi24'

        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
            }
        }

        dexOptions {
            javaMaxHeapSize "4g"
            preDexLibraries = false
        }

    }
    buildTypes {
        release {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }

    /**
     * Solve the problem when using multiple free source libs
     * NOTICE or LICENSE files cause duplicates
     */
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
        //exclude duplicate butterknife and parceler libs
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/rxjava.properties'

    }


}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation files('libs/motwin-android-sdk-3.2.0-RELEASE-TLS.jar')
    implementation files('libs/notifmanager-android-lib-3.1.0-RELEASE.jar')
    implementation files('libs/commons-lang-2.4.jar')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support:support-v4:26.0.2'
    implementation 'com.android.support:design:26.0.2'
    implementation 'com.android.support:multidex:1.0.2'
    api 'com.jakewharton:butterknife:7.0.1'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.android.support:percent:26.0.2'
    implementation 'com.google.android.gms:play-services-maps:11.4.2'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.google.code.gson:gson:2.8.1'
    testImplementation 'junit:junit:4.12'
    implementation 'com.facebook.rebound:rebound:0.3.8'
    implementation 'com.google.android.gms:play-services-gcm:11.4.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.1'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

    implementation project(':animators')

    // Retrofit2 & XmlConverter
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    implementation('com.squareup.retrofit2:converter-simplexml:2.3.0') {
        exclude group: 'xpp3', module: 'xpp3'
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'stax', module: 'stax'
    }

    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'

    //Library to reports crash
    implementation('ch.acra:acra:4.5.0') {
        exclude group: 'org.json'
    }

    implementation 'com.github.kenglxn.QRGen:android:2.3.0'

}

在 library animator 的 build.gradle 中,我将 targetSdkVersion 升级为 26 :
apply plugin: 'com.android.library'

android {
  compileSdkVersion 26
  buildToolsVersion '26.0.2'

  defaultConfig {
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
  }
}

dependencies {
  implementation "com.android.support:support-compat:26.0.2"
  implementation "com.android.support:support-core-ui:26.0.2"
  implementation "com.android.support:recyclerview-v7:26.0.2"
}

3

请确保您已在Gradle设置中禁用了“离线工作”(设置->构建、执行、部署->构建工具->Gradle)。

我曾经犯过这个错误。我按照加速gradle构建的指南打开了离线工作选项。如果启用了该选项,Gradle将无法下载任何新的依赖项,因此会导致“无法解决依赖关系”错误。


1
这是最好的答案。 - Zia Ur Rahman
离线状态与本地项目有什么关系? - m0skit0
如果您处于离线状态,则无法下载所需的依赖项。 - Yashas

3

你只需要在app.gradle文件中重置依赖项,就像旧版本一样

androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'

转移到新的一个位置

androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

2
这也可能发生在你添加一个引用到使用了错误插件类型的特征模块时。只需将 com.android.application 更改为 com.android.featurecom.android.library

https://istack.dev59.com/NDjnG.webp


2

在应用的build.gradle文件中添加productFlavors{}解决了我的问题。请参见以下内容:

buildTypes {
        release {
             ...
        }
    }
productFlavors {
    }

2

我遇到过同样的问题并解决了。

根据我的情况,我猜测您的应用程序项目中的build.gradle文件包含以下片段:

android {
    ...
    buildTypes {
        debug {...}
        release {...}
        dexOptions {...}
    }
}

实际上,dexOptions 不是一个构建类型,你应该将 dexOptions 部分移到 buildTypes 之外,像这样:

android {
    ...
    dexOptions {
        ...
    }

    buildTypes {
        debug {...}
        release {...}
    }
}

希望这能帮助到某些人。

1

我尝试了上述所有解决方案,但它们都没有对我起作用,我确保我的库模块和应用程序模块中具有完全相同的构建类型和产品风格。


1

这个讨论似乎有些偏离了主题,但是关于原始问题的相关内容,不仅你需要在两个 build.gradle 文件中设置 buildTypes,如果你使用了 productFlavors(当然前提是你在使用它们),你也需要在两个 build.gradle 文件中设置。


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