简体   繁体   中英

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). The Java library depends on the Android library. Both are modules in my project like this:

MyProject    
|-android
|-java

Both appear in settings.gradle:

include ':android', ':java'

And the Java library depends on the Android library like this:

java ( build.gradle ):

apply plugin: 'java-library'

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

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

Why?

PS The other way around (Android depending on Java) works just fine.

First let's try to fix the build error. Let's run ./gradlew build --stacktrace to see a more detailed output:

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

Cannot choose between the following configurations of project :androidLibrary :

  • debugApiElements
  • releaseApiElements

AGP is confused which variant to choose. 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' . Java modules cannot depend on Android modules

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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