android-studiogradleguava

Gradle dependency `libs.guava` results in Gradle not Syncing anymore


After an upgrade whenever I try to Sync my gradle on a project I get this error:

Could not get unknown property 'guava' for extension 'libs' of type org.gradle.accessors.dm.LibrariesForLibs.

The line of code that causes the problem is:

implementation libs.guava

in the dependencies section of the build.gradle. That library is needed for ImmutableList for In App Purchase code.

You can recreate this error using the Narwal Android Studio:

  1. New Project

  2. Empty Views Activity

  3. Language Java, Min API 21, Groovy DSL

  4. Add the following to the dependencies section of the build.gradle and then SYNC the gradle:

    /* This is for the billing library we are using */
    implementation "com.android.billingclient:billing:8.0.0"
    
    /* Adverts */
    implementation 'com.google.android.gms:play-services-ads:23.3.0'
    
    /* GDPR */
    implementation "com.google.android.ump:user-messaging-platform:3.2.0"
    
    /* ImmutableList */
    implementation libs.guava
    
  5. My version catalog is as follows, not edited in any way by myself:

    [versions]
    agp = "8.11.1"
    junit = "4.13.2"
    junitVersion = "1.2.1"
    espressoCore = "3.6.1"
    appcompat = "1.7.1"
    material = "1.12.0"
    activity = "1.10.1"
    constraintlayout = "2.2.1"
    
    [libraries]
    junit = { group = "junit", name = "junit", version.ref = "junit" }
    ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
    espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
    appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
    material = { group = "com.google.android.material", name = "material", version.ref = "material" }
    activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
    constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
    
    [plugins]
    android-application = { id = "com.android.application", version.ref = "agp" }
    

Solution

  • You try to add the dependency guava from your version catalog with this:

    implementation libs.guava
    

    But your version catalog doesn't have a guava dependency defined in the [libraries] section. Hence the error

    Could not get unknown property 'guava' for extension 'libs' of type org.gradle.accessors.dm.LibrariesForLibs.

    Just add a new entry guava there, with the desired group, name and version - just like the other entries. The version should be specified as a reference to an appropriately named entry in the [versions] block.