简体   繁体   English

无法解析项目:Android库和Java库模块依赖项

[英]Failed to resolve project: Android library and Java library module dependency

I'm trying to create a project which includes an Android library and a Java library in Android Studio (3.1). 我正在尝试在Android Studio(3.1)中创建一个包含Android库和Java库的项目。 The Java library depends on the Android library. Java库依赖于Android库。 Both are modules in my project like this: 这两个都是我项目中的模块,如下所示:

MyProject    
|-android
|-java

Both appear in settings.gradle: 两者都出现在settings.gradle中:

include ':android', ':java'

And the Java library depends on the Android library like this: Java库依赖于Android库,如下所示:

java ( build.gradle ): java( build.gradle ):

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':android')
}
...

android ( build.gradle ): android( build.gradle ):

apply plugin: 'com.android.library'
...

When trying to sync the project I'm getting the following error: 尝试同步项目时,我收到以下错误:

Failed to resolve: project::android 无法解决:project :: android

Why? 为什么?

PS The other way around (Android depending on Java) works just fine. PS反过来 (Android依赖于Java)工作得很好。

First let's try to fix the build error. 首先让我们尝试修复构建错误。 Let's run ./gradlew build --stacktrace to see a more detailed output: 让我们运行./gradlew build --stacktrace来查看更详细的输出:

Caused by: org.gradle.internal.component.AmbiguousConfigurationSelectionException: 引起:org.gradle.internal.component.AmbiguousConfigurationSelectionException:

Cannot choose between the following configurations of project :androidLibrary : 无法在以下项目配置之间进行选择:androidLibrary

  • debugApiElements debugApiElements
  • releaseApiElements releaseApiElements

AGP is confused which variant to choose. AGP很困惑选择哪种变体。 After inspecting this answer, we can find how to overcome the issue: 在检查了这个答案后,我们可以找到如何克服这个问题:

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

After trying to sync the project with that setup you'll see following output in console: 尝试将项目与该设置同步后,您将在控制台中看到以下输出:

Ignoring dependency of module 'androidLibrary' on module 'javaLibrary' . 忽略模块'androidLibrary'对模块'javaLibrary'的依赖 Java modules cannot depend on Android modules Java模块不能依赖于Android模块

Thus, seems like what you try to do is not supported by the plugin. 因此,插件似乎不支持您尝试执行的操作。 Refer to similar question , where Nick Cardoso tries to clarify the case. 请参阅类似的问题 ,Nick Cardoso试图澄清此案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM