Failed to resolve project: Android library and Java library module dependency

Sir Codesalot :

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?

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

azizbekian :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=431364&siteId=1