How to Include Guava in a Gradle Project: Complete Setup Guide

Add Guava to your Gradle project by declaring com.google.guava:guava:33.6.0-jre (for JVM) or 33.6.0-android (for Android) in your dependencies block using either implementation or api configuration.

To include Guava in a Gradle project, you need to understand the library's dual-flavor publishing strategy and dependency scope selection. The google/guava repository distributes the library through Maven Central with distinct artifacts for standard Java and Android environments, each configured differently in your build.gradle or build.gradle.kts file.

Understanding Guava's Maven Coordinates and Flavors

Guava publishes to Maven Central under the group ID com.google.guava and artifact ID guava. The project maintains two binary variants as defined in guava/pom.xml and android/guava/pom.xml:

  • JRE flavor (suffix -jre): Optimized for Java 8+ applications. This is the default choice for standard JVM projects.
  • Android flavor (suffix -android): Compatibility build for Android projects or libraries targeting Android runtimes.

The version classifier determines which artifact Gradle resolves. For version 33.6.0, you would specify either 33.6.0-jre or 33.6.0-android depending on your target platform.

Adding Guava to Your Gradle Build

Standard JVM Projects

For Java applications using the JRE flavor, add the dependency to your module's build.gradle:

dependencies {
  implementation "com.google.guava:guava:33.6.0-jre"
}

In Kotlin DSL (build.gradle.kts):

dependencies {
  implementation("com.google.guava:guava:33.6.0-jre")
}

Android Projects

Android modules must use the Android flavor to ensure compatibility with Android's runtime constraints. According to the android/guava/pom.xml configuration:

dependencies {
  implementation "com.google.guava:guava:33.6.0-android"
}

Or in Kotlin DSL:

dependencies {
  implementation("com.google.guava:guava:33.6.0-android")
}

Implementation vs API Dependency Scope

When you include Guava in a Gradle project, you must choose between implementation and api configurations based on whether Guava types appear in your public signatures.

  • implementation: Use this when Guava is an internal implementation detail. The dependency remains private to your module and is not exposed to consumers.
  • api: Use this only when your public methods or classes expose Guava types (e.g., returning ImmutableList or accepting CacheBuilder). This follows Gradle's Java Library Plugin guidance on API-implementation separation.

As documented in the repository's README.md (lines 48-65) and the wiki guide UseGuavaInYourBuild.md, choosing the correct scope prevents dependency leakage and reduces compilation classpath bloat for downstream projects.

Practical Code Examples

Simple Java Library

For a basic Java library using Guava internally:

plugins {
  id 'java-library'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation "com.google.guava:guava:33.6.0-jre"
}

Library Exposing Guava Types

When your library's public API includes Guava classes:

plugins {
  id 'java-library'
}

repositories {
  mavenCentral()
}

dependencies {
  api "com.google.guava:guava:33.6.0-jre"
}

Android Module Configuration

Complete Android setup using Kotlin DSL:

plugins {
  id("com.android.library")
  kotlin("android")
}

android {
  compileSdk = 34
  defaultConfig {
    minSdk = 24
  }
}

dependencies {
  implementation("com.google.guava:guava:33.6.0-android")
}

Using Guava in Application Code

After adding the dependency, import and use Guava utilities:

import com.google.common.collect.ImmutableList;

public class Example {
  public static void main(String[] args) {
    ImmutableList<String> list = ImmutableList.of("a", "b", "c");
    System.out.println(list);
  }
}

Summary

  • Coordinates: Guava uses com.google.guava:guava with either -jre or -android version suffixes as defined in guava/pom.xml and android/guava/pom.xml.
  • Flavor Selection: Use -jre for standard Java 8+ projects and -android for Android compatibility.
  • Scope Selection: Use implementation for internal use only; use api only when exposing Guava types in your public API.
  • Repository: Always declare mavenCentral() in your repositories block to resolve the artifacts.

Frequently Asked Questions

What is the difference between the JRE and Android flavors of Guava?

The JRE flavor (guava-XX.X.X-jre.jar) contains the complete Guava API optimized for standard Java 8+ virtual machines. The Android flavor (guava-XX.X.X-android.jar) omits or replaces APIs incompatible with Android's runtime, ensuring the library functions correctly on Android devices. Both artifacts share the same Maven coordinates but use different version classifiers.

Should I use implementation or api for Guava dependencies?

Use implementation if your code uses Guava internally without exposing it in public method signatures. Use api only if your library's public interface accepts or returns Guava types (such as ImmutableList or Optional), forcing consumers to have Guava on their compilation classpath.

Where is the official Gradle configuration documented?

The official snippets are located in the README.md file at lines 48-65 of the google/guava repository. Detailed guidance on dependency scope selection and build configuration is available in the wiki file UseGuavaInYourBuild.md.

Can I use the JRE flavor in Android projects?

No. Android projects must use the Android flavor (33.6.0-android or similar) because the JRE flavor contains APIs and bytecode patterns incompatible with Android's runtime limitations. The android/guava/pom.xml explicitly configures the Android-compatible build variant for this purpose.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →