18

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
  • 60,783
  • 13
  • 169
  • 249
Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56

1 Answers1

18

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.

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • I thought it might not be supported. Unfortunately neither your answer nor the one you linked to provides any other way to solve the problem (I know the other answer lists 3 "solutions" but none of them actually solves the problem, they all suggest to do something else instead). – Sir Codesalot Apr 16 '18 at 11:30
  • The point is - you cannot do what you want to do, that is not supported by Android Gradle Plugin. I cannot see how you can achieve that considering the limitation that exists. – azizbekian Apr 16 '18 at 12:30
  • 1
    brilliant answer! Exactly the problem I had. Great explanation! – Tom Rutchik May 16 '20 at 01:28