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个回答

151

使用Android Studio 2.3(AS)时,该项目可正常工作,并且我能够运行应用程序。将AS更新为Android Studio 3.0后,我也遇到了以下有关库和构建类型的错误。

Unable to resolve dependency for ':app@dexOptions/compileClasspath': Could not resolve project : library_Name.

Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve project : library_Name.

解决问题很简单。

无论是什么

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

在你的(app) build.gradle文件中,你必须包含所有与以下名称相同的buildTypes{ }

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

在项目中包含的所有库/模块的build.gradle文件中。

清除和重新构建该项目,问题将得到解决。

如果问题仍未解决,请更新gradle-wrapper.properties文件。

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

113
包括所有名称相同的 buildTypes { }?你在说什么? - lolololol ol
13
我也不知道这是什么意思。 - Sir Neuman
7
可惜的是,这种做法已经不再适用于现代构建。 - mochadwi
3
尽管这个回答有一定道理,但并不实用……我不想将此构建类型添加到所有模块中……除了我的应用程序外,它们绝对不该出现在其他任何地方!我的应用程序需要知道后备构建类型,并从中获取其配置! - TacB0sS
3
这是一个解决方案,但只是一种权宜之计。@JackHang的答案才是正确处理这个问题的方法(https://dev59.com/hFYN5IYBdhLWcg3wwKdZ#47029361)。 - Keivan Esbati
显示剩余7条评论

130
正如官方迁移指南所述,当出现以下情况时会遇到此错误:

您的应用程序包含一个库依赖项不具备的构建类型

android {
  buildTypes {
      release {
          ...
      }
      dexOptions {
          ...
        // release & debug is in project animators
        matchingFallbacks = ['release', 'debug']
      }
      debug {
          ...
      }
    }
}

现在找到信息的正确位置是 此文档


18
根据官方迁移指南中的解释,这应该是正确的答案。 https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#resolve_matching_errors - griable
3
这是最佳答案,解决了我的问题。我只需要在Flavors中使用matchingFallbacks = ['release','debug']。 - Poorya
3
太棒了!谢谢你救了我的一天。 - Dmitry Smolyaninov
1
Gradle 5.1.1。不幸的是,它仍然无法工作,错误仍然相同。我已经在库中有了相同的buildTypes,并添加了匹配的fallbacks到flavors,但无济于事。 - m0skit0

95

确保您在自己的项目中

  1. 通过点击文件 > 设置(对于Mac, Android Studio > 偏好设置),打开首选项。
  2. 在左侧窗格中,单击构建、执行、部署> Gradle
  3. 取消选中/禁用 离线工作 复选框。
  4. 单击应用确定

2
最简单和最好的答案 - Daksh Agrawal
1
我忘记了我已经启用了那个选项。谢谢。 - SudoPlz
1
这是答案。谢谢,您节省了我的时间。 - Pouya Samie
6
不适用于当地图书馆。选项已取消勾选,但仍存在问题。 - Christopher Smit
一个关于Android Studio 3问题的旧答案! - Emad
@XStylish 这很合乎逻辑。 - Emad

75

我花了很多时间在这个问题上,但以上的解决方案都对我不起作用。在应用程序和库项目中,构建类型的名称和数量也完全相等。

我唯一犯的错误是- 在库项目的build.gradle文件中,我使用了以下代码:

apply plugin: 'com.android.application'

而实际上应该使用以下代码:

apply plugin: 'com.android.library'

修改后,此错误得到解决。


10
太棒了!这正是我的问题所在。需要注意的是,在defaultConfig { }中的applicationId也需要在库中删除。 - ObjectType
1
我正准备发布这个答案,就像你一样,经过了艰苦的、令人沮丧的工作和尝试各种选项,然后再试试。在发布之前,我决定看看最新的帖子,然后..哇!这个答案需要以某种方式优先考虑。 - nat101
爱你兄弟 :* - Sumit Kumar
谢谢,就这样了。 - Musketyr
不适用于我。而且以上都不起作用。 - philtz
看起来这并不简单。我的主要模块应用插件:'com.android.application' 依赖于另一个基础模块,该模块也必须应用插件:'com.android.application'。因此,问题在于在一个项目中不能有两个声明为应用程序的模块。 - murt

66
将所有的 compile project(':library:yourproject') 修改为 implementation project(path: ':library:yourproject', configuration:'default'),并注意配置行,在您的应用程序 build.gradle 中。请保留所有html标签。

1
我看到它开始工作了,但是当我在构建变体之间切换时:调试/发布库模块不会发生改变。 - murt
1
为你感到骄傲,越南开发者 :D 非常感谢 - Long Nguyen
如果我有更多的配置,你知道我是否需要在配置参数中指定buildTypes吗? - Alejandro Traver
在我的情况下,这对我起作用了。app 有构建类型 debugstagerelease,而 mylib 则有构建风格 sdk1sdk2 - iroiroys

35

我尝试了各种方法,从 取消离线工作匹配fallbacks,但都没有效果。

然后,在 app.gradle 的依赖项中

用以下代码替换

implementation project(':lib-name')

代码如下:

implementation project(path:':lib-name', configuration: 'default')

例如:implementation project(path:':myService', configuration: 'default')

这样就可以像魔法一样解决问题啦。 :)

我正在添加一个具有服务依赖模块,并不是制作库,因为它是AOSP项目的一部分。

以防对某人有所帮助。


4
这是我唯一有效的方法,我已经为此纠结了几个小时...非常感谢你! - sunny-mittal
3
这种方式没有Gradle错误,但仍然无法从Java中引用该库:/ - Nathan Schwermann
3
谢谢,最终我不得不为图书馆里的所有东西都准备相匹配的口味,这样做起来很顺利。 - Nathan Schwermann
1
运行完美。选择“default”的配置有什么特殊原因吗? - Radhey
1
没有Gradle错误,但仍然无法从应用程序模块引用该文件。 - androidbash
显示剩余2条评论

17

解决方案:

下载最新版本的Gradle

http://services.gradle.org/distributions/

gradle-4.x-rc-1-all.zip.sha256 09-Jan-2018 01:15 +0000 64.00B

解压分发

打开Android Studio -> 文件 -> 设置 -> Gradle -> 使用本地gradle分发,搜索文件并确认

在gradle:app中写入以下内容,implementation(path: ':animators', configuration: 'default')

dependencies {
   .
   .
   .


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


}

我之前也遇到了这个问题。 但是implementation project(path: ':animators', configuration: 'default')implementation project(path: ':animators')有什么不同呢?(这将会在 Android Studio 中生成,但却无法正常工作) - Andreas

10

这是我解决问题的方法:

不要使用

compile project(':library_name')
compile project(':library_name')

在应用的Gradle中,我使用了

implementation project(':library_name')
implementation project(':library_name')

例如,在我的构建类型中

demoTest {
 .........
}

我添加了这一行

demoTest {
   matchingFallbacks = ['debug', 'release']
}

7

我也遇到了同样的问题,通过在build.gradle(Project)中添加“mavenCentral()”解决了它。

allprojects {
    repositories {
        ...
        mavenCentral()
    }
}

7
我的问题如下
Unable to resolve dependency for ':app@debug/compileClasspath': Could not download rxjava.jar (io.reactivex.rxjava2:rxjava:2.2.2)

通过勾选 启用嵌入式Maven仓库 解决问题。

enter image description here


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