javagradleguavaintellij-14google-cloud-vertex-ai

How to exclude one of the variants of com.google.guava dependency while building Gradle projects


I have included the below dependency in the build.gradle file of my gradle project to work with VertexAi LLM Libraries.

implementation group: 'dev.langchain4j', name: 'langchain4j-vertex-ai', version: '0.22.0'

I am facing a problem while testing the main function.

The Idea IntelliJ prompt says that it is unable to choose between two variants of com.google.guava:guava:32.1.2-jre (which is a dependency for the VertexAi dependency)

I got the following error message

The consumer was configured to find an API of a library compatible with Java 11, preferably in the form of class files, and its dependencies declared externally.

However we cannot choose between the following variants of com.google.guava:guava:32.1.2-jre
      - androidApiElements
      - jreApiElements

I think I need to use the jreApiElements variant since I am dealing only with a web application. It looks like the IDE is confused because both variants provided the same number of parameters.

All of them match the consumer attributes:
  - Variant 'androidApiElements' capabilities com.google.collections:google-collections:32.1.2-jre and com.google.guava:guava:32.1.2-jre declares an API of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally:
      - Unmatched attributes:
          - Provides attribute 'org.gradle.jvm.environment' with value 'android' but the consumer didn't ask for it
          - Provides release status but the consumer didn't ask for it
  - Variant 'jreApiElements' capabilities com.google.collections:google-collections:32.1.2-jre and com.google.guava:guava:32.1.2-jre declares an API of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally:
      - Unmatched attributes:
          - Provides attribute 'org.gradle.jvm.environment' with value 'standard-jvm' but the consumer didn't ask for it
          - Provides release status but the consumer didn't ask for it

Please let me know how to choose the jreApiElements variant while excluding the androidApiElements variant. Preferable to do it in build.gradle/project gradle settings since I want the code to be portable.

I have tried the below solutions:

  1. Setting the right gradle version in the gradle wrapper properties file

  2. Adding the parameter applicationDefaultJvmArgs = ["-org.gradle.jvm.environment=standard-jvm"] to build.gradle file (This made the build fail so I removed it)

  3. Adding the below 2 dependencies in build.gradle

    implementation group: 'com.google.cloud', name: 'google-cloud-aiplatform', version: '3.24.0'

    implementation group: 'com.google.guava', name: 'guava', version: '32.1.2-jre'

None of the above solutions worked. Please let me know how can exclude the the android variant of the guava dependency


Solution

  • The recommendation that we've gotten from some Gradle experts is listed in our release notes. (I should really mention it in our home page, sorry.)

    sourceSets.all {
      configurations.getByName(runtimeClasspathConfigurationName) {
        attributes.attribute(Attribute.of("org.gradle.jvm.environment", String), "standard-jvm")
      }
      configurations.getByName(compileClasspathConfigurationName) {
        attributes.attribute(Attribute.of("org.gradle.jvm.environment", String), "standard-jvm")
      }
    }
    

    We're also told that upgrading from Gradle 6 to Gradle 7 may eliminate the need for that.

    That said, we've heard of a few different but related problems. So if this doesn't help in your case, someone may be able to provide another possible solution.