# Configuring the build cache for Gradle in local builds

You can use the Bitrise Build Cache for local Gradle builds too. Your local builds then read from the same cache as your CI builds, so a task your CI already compiled doesn't have to be compiled again on your machine.

The Bitrise Build Cache CLI sets this up for you. It writes a Gradle init script to `~/.gradle/init.d/`, so your project files stay untouched.

You can also set this up with an AI coding agent instead of following the steps manually. See [Getting started with AI](/bitrise-build-cache/getting-started-with-the-build-cache/getting-started-with-ai).

## Before you start

Ensure you have:
- A working Gradle project on your machine (macOS or Linux).
- A Bitrise workspace with the Build Cache enabled. Check it on the [Build Cache page](https://app.bitrise.io/build-cache/).
- Network access to the Bitrise hosts the Build Cache uses, if your machine is behind a VPN, a firewall, or an outbound proxy. See [Network endpoints for firewalls and VPNs](/bitrise-build-cache/getting-started-with-the-build-cache/network-endpoints-for-firewalls-and-vpns).

:::note[Android projects with native code]

If your project builds C/C++ code (NDK, JNI, or native modules), set up `ccache` alongside Gradle. The Gradle plugin caches Java and Kotlin task outputs, but native compiles bypass it and go through `ccache` instead. Install it with `brew install ccache`, then select both **Gradle** and **ccache (C/C++)** in the wizard.

:::

## Installing the CLI

Install the CLI with Homebrew (**recommended**):

```bash
brew install bitrise-io/bitrise-build-cache/bitrise-build-cache
```

Or, without Homebrew:

```bash
curl --retry 5 -sSfL \
  'https://raw.githubusercontent.com/bitrise-io/bitrise-build-cache-cli/main/install/installer.sh' \
  | sh -s -- -b ~/.local/bin
```

Make sure the install location is on your `PATH`, then check the install:

```bash
bitrise-build-cache --version
```

## Activating the cache

Run the interactive wizard:

```bash
bitrise-build-cache activate --interactive
```

The wizard asks for the following:

1. **Sign in to Bitrise**: opens your browser for authentication on the first run. The CLI stores the credentials in the OS keychain and refreshes them automatically, so later runs skip this step. On a machine without a usable keychain, the CLI falls back to storing them in a config file.
1. **Select a workspace**: pick the workspace whose Build Cache you want to use. The CLI selects it automatically if you only have access to one.
1. **Which build tools should I set up**: ensure **Gradle** is selected, plus **ccache (C/C++)** if your project builds native code. Use space to toggle an option and enter to confirm.
1. **Cache for all projects, or only opted-in ones?**: keep the default **Always** unless several projects live on this machine and you only want a subset to use the cache. See [Per-project cache scoping](/bitrise-build-cache/getting-started-with-the-build-cache/per-project-cache-scoping).
1. **Display name for this machine's local invocations**: the name your local builds show up under in the Build Cache dashboard, for example `local-<yourhandle>`.
1. **Enable cache push**: keep the default **Yes, push too**. See [Cache push mode](#cache-push-mode).
1. **Keep the cache proxies running in the background**: select **Yes, install + start** if you set up `ccache`. This registers the helper processes with the OS so they survive shell restarts.

:::note[Environment Variables take precedence]

If `BITRISE_BUILD_CACHE_AUTH_TOKEN` and `BITRISE_BUILD_CACHE_WORKSPACE_ID` are already set in your shell, the CLI uses those instead of the stored credentials. Unset them if you want the sign-in to apply.

:::

## Verifying the setup

Run the CLI's health check:

```bash
bitrise-build-cache doctor
```

It reports the status of every part of the local setup — credentials, backend connectivity, helper processes, and log directories — and ends with an overall verdict:

```bash
Bitrise Build Cache - doctor
CLI version: 3.x.y

Healthy:
  ✓ auth                   OAuth login (keychain) (workspace <id>), token valid until <iso-timestamp>
  ✓ keychain-smoke         Set/Get/Delete round-trip OK
  ✓ auth-backend           latency <ms>, source=keychain, workspace=<id>
  ✓ project-scope          mode=always; cache_push=true (machine config); no .bitrise-build-cache.json found in <cwd> or parents.; would gate this directory: no
  ✓ ccache-binary          found at /opt/homebrew/bin/ccache
  ✓ ccache-helper          running (~/.local/state/ccache/ccache.sock)
  ✓ log-dirs               all log dirs present + writable

Overall: ok
```

To let the CLI repair the issues it can fix on its own, run `bitrise-build-cache doctor --fix --interactive`.

## Running a build

1. Clean the project's local build outputs first, so the build has to fetch from the remote cache. Pass `--no-daemon` as well: a Gradle daemon started before the activation doesn't pick up the new configuration.

   ```bash
   cd path/to/your/gradle/project
   ./gradlew clean --no-daemon
   ```

1. Run the build:

   ```bash
   ./gradlew :app:assembleDebug
   ```

A build that hits a warm cache ends like this:

```bash
> Task :app:compileDebugKotlin FROM-CACHE
> Task :feature:one:compileDebugKotlin FROM-CACHE

BUILD SUCCESSFUL in 18s
149 actionable tasks: 71 executed, 78 from cache

[Bitrise Analytics] 155 tasks uploaded. Check invocation at
    https://app.bitrise.io/build-cache/invocations/gradle/<uuid>
```

## Checking that it worked

| Signal | Where to find it | What success looks like |
|---|---|---|
| `Task :module:name FROM-CACHE` | Gradle build output, per task | The task line ends with `FROM-CACHE` |
| `N actionable tasks: X executed, Y from cache` | Gradle build summary | A non-zero `from cache` count |
| Invocation link | Printed at the end of the build | Opens the per-task metrics for the build |
| Dashboard | [Build Cache page](https://app.bitrise.io/build-cache/) | A row appears under your display name |
| Init script | `~/.gradle/init.d/bitrise-build-cache.init.gradle.kts` | The file exists and contains `buildCache {` |

## Cache push mode

The setup in this guide activates the Build Cache with cache push enabled: your local builds read from and write to the shared cache. Every task your machine builds becomes an entry your teammates and CI can reuse, and your activity shows up on the [Build Cache page](https://app.bitrise.io/build-cache/).

If you'd rather have your local builds only read from the cache, see [Opting into pull-only mode](#opting-into-pull-only-mode) below.

## Opting into pull-only mode

If your team already populates the cache from CI and you want your local builds to only read from it, turn cache push off. The trade-off is that your local builds contribute nothing, so if CI doesn't run yet the summary line stays at `0 from cache`.

Re-run `bitrise-build-cache activate --interactive` and answer **No, pull only** at the cache push prompt, or run the non-interactive activate command:

```bash
bitrise-build-cache activate gradle --cache --cache-push=false
```

If a build doesn't behave as expected, re-run the activation with debug logging:

```bash
bitrise-build-cache activate gradle --cache --cache-push=false --debug
```

## Troubleshooting

Start with the CLI's health check. It inspects every part of the local setup and repairs the issues it can fix on its own:

```bash
bitrise-build-cache doctor --fix --interactive
```

If you're still experiencing issues, check the following table:

| Issue | Fix |
|---|---|
| The wizard reports that it needs a terminal | Run `TERM=dumb bitrise-build-cache activate --interactive` for line-based mode. |
| `doctor` reports a problem it can't fix | Re-run it with `--debug` for the full context. |
| Your build ignores the new configuration | Stop any running Gradle daemons with `./gradlew --stop`, or pass `--no-daemon`. |
| You want to start over | It's safe to re-run the wizard. It re-reads the current state and applies the same activation again. |
| The build can't reach the cache, or the cache calls time out | Your VPN, firewall, or proxy may block the Bitrise hosts. Check them against [Network endpoints for firewalls and VPNs](/bitrise-build-cache/getting-started-with-the-build-cache/network-endpoints-for-firewalls-and-vpns). |
