- Home
- Dependencies and caching
- Remote build caching
- Remote build cache for Gradle
Remote build cache for Gradle
The Bitrise remote build cache allows you to utilize the Gradle build cache for both local and CI builds. The solution caches Gradle task outputs on a remote server. When you run a build, Gradle will not execute tasks whose output is available in the remote cache and download the cache entry instead.
The Gradle build cache helps you save time by reusing outputs produced by other builds. The Bitrise remote build cache allows you to utilize it for both local and CI builds.
Gradle builds are a series of tasks with inputs and outputs. Bitrise caches task outputs on a performant remote server. When you start a Gradle build, Gradle will skip executing tasks whose output is available in the remote cache and will instead download and reuse the cache entry.
Dependency caching vs build caching
Build caching and dependency caching are separate concepts. The best practice is to use both to minimize build times. Read more: Caching Gradle dependencies.
Use key-based dependency caching
We recommend that you do not use the legacy dependency caching Steps in combination with remote build caching. Instead, combine build cache only with key-based dependency caching. This is to prevent unintended consequences that can happen when the legacy dependency cache carries build settings over between builds.
Configuring remote caching for Gradle in the Bitrise CI environment
In the Bitrise CI environment, you only need our official Step to use the remote build cache for your Gradle builds.
If you want to use the Bitrise remote build cache in your local builds, you need to first activate the remote build cache in our CI environment.
Workflow Editor
bitrise.yml
-
Open your app on Bitrise.
-
Click the Workflows button on the main page.
-
On the Workflows & Pipelines page, find the Workflow you need.
-
Click the
button next to the name of the Workflow.
-
Add the Activate Bitrise Build Cache for Gradle Step to your Workflow.
The Step should be before any Step that executes Gradle tasks, such as Gradle Runner or Android Build.
-
If you want your build to update the remote cache, set the Push new cache entries input to true.
-
Configure cache entry validation if you need it: the Validation level input determines what happens if invalid cache entries are encountered.
-
off: No cache entry validation happens.
-
warning: Prints a warning about invalid cache entries but the build is not interrupted.
-
error: Prints an error about invalid cache entries and interrupts the build.
-
-
Open the
bitrise.yml
file and add theactivate-build-cache-for-gradle
Step to your Workflow.The Step should be before any Step that executes Gradle tasks, such as
gradle-runner
orandroid-build
.your-workflow: steps: - git-clone@8: {} - activate-build-cache-for-gradle:
-
If you want your build to update the remote cache, set the
push
input to true.Default value of
push
The default value of
push
istrue
. If you do not explicitly set the value ofpush
, the Step will write new cache entries.your-workflow: steps: - git-clone@8: {} - activate-build-cache-for-gradle: inputs: - push: 'true'
-
Configure cache entry validation if you need it: the
validation_level
input determines what happens if invalid cache entries are encountered.-
off
: No cache entry validation happens. -
warning
: Prints a warning about invalid cache entries but the build is not interrupted. This is the default value of the input. If you do not set the value of the input, warnings will be printed but the build will not be interrupted. -
error
: Prints an error about invalid cache entries and interrupts the build.
your-workflow: steps: - git-clone@8: {} - activate-build-cache-for-gradle: inputs: - push: 'true' - validation_level: warning
-
Configuring remote build caching for Gradle in local builds
You can use the Bitrise remote build cache even for local Gradle builds. In this way your local builds and CI builds will use the same build cache, minimizing build times in both environments.
There are two ways to set up remote caching for a Gradle project:
-
Configuring caching in a separate file, without changing Gradle project files.
-
Configuring caching directly in the Gradle project build files.
Config without changing project files
Config with changing project build files
If you want to get started with remote caching without changing the project's build configuration (and affecting others working on the same project), you can do so with a Gradle init script.
-
Enable the build cache in Gradle: in the
gradle.properties
file of your project, setorg.gradle.caching=true
. -
Enable the remote build cache in the Bitrise CI environment.
-
Generate a Personal Access Token on Bitrise: Creating a personal access token.
-
Find your Workspace ID. You can do so by navigating to the Workspace's page and find the ID in the URL.
-
Create a file called
bitrise-init.gradle
somewhere outside the project folder with this content:initscript { repositories { mavenCentral() maven { url 'https://jitpack.io' } } dependencies { classpath 'io.bitrise.gradle:remote-cache:1.2.0' } } import io.bitrise.gradle.cache.BitriseBuildCache import io.bitrise.gradle.cache.BitriseBuildCacheServiceFactory gradle.settingsEvaluated { settings -> settings.buildCache { local { enabled = false } registerBuildCacheService(BitriseBuildCache.class, BitriseBuildCacheServiceFactory.class) remote(BitriseBuildCache.class) { enabled = true push = false // read-only is recommended for local builds endpoint = 'grpcs://pluggable.services.bitrise.io' authToken = 'workspace ID' + ':' + 'personal access token' } } }
-
In the
bitrise-init.gradle
file, replaceworkspace ID
andpersonal access token
with your actual Workspace ID and personal access token. -
Run a test build to make sure everything works:
./gradlew clean ./gradlew :app:assembleDebug --init-script /path/to/bitrise-init.gradle
-
You can integrate the init script into every Android Studio execution by defining the
--init-script
flag in Android Studio settings.
You can make the remote caching configuration part of the project's build configuration. In this case, you will need to make changes to the project's settings.gradle
file.
-
Enable the build cache in Gradle: in the
gradle.properties
file of your project, setorg.gradle.caching=true
. -
Enable the remote build cache in the Bitrise CI environment.
-
Generate a Personal Access Token on Bitrise: Creating a personal access token.
-
Find your Workspace ID. You can do so by navigating to the Workspace's page and find the ID in the URL.
-
Add the following content to the project's
settings.gradle
(if you are using Groovy) orsettings.gradle.kts
(if you are using Kotlin) file:Groovy
Kotlin
buildscript { repositories { mavenCentral() maven { url 'https://jitpack.io' } } dependencies { classpath 'io.bitrise.gradle:remote-cache:1.2.0' } } import io.bitrise.gradle.cache.BitriseBuildCache import io.bitrise.gradle.cache.BitriseBuildCacheServiceFactory buildCache { local { enabled = false } registerBuildCacheService(BitriseBuildCache.class, BitriseBuildCacheServiceFactory.class) remote(BitriseBuildCache.class) { enabled = true push = false // read-only is recommended for local builds def localProperties = new Properties() file('local.properties').withInputStream { localProperties.load(it) } authToken = 'workspace ID' + ':' + localProperties.getProperty('io.bitrise.gradle.remoteCache.authToken', '') } }
import io.bitrise.gradle.cache.BitriseBuildCache import io.bitrise.gradle.cache.BitriseBuildCacheServiceFactory import java.util.Properties pluginManagement { ... } dependencyResolutionManagement { ... } buildscript { repositories { mavenCentral() maven { setUrl("https://jitpack.io") } } dependencies { classpath("io.bitrise.gradle:remote-cache:1.2.0") } } buildCache { local { isEnabled = false } registerBuildCacheService<BitriseBuildCache>(BitriseBuildCacheServiceFactory::class) remote<BitriseBuildCache> { isEnabled = true isPush = false // read-only is recommended for local builds val localProperties = Properties() val inputStream = file("local.properties").inputStream() localProperties.load(inputStream) authToken = "workspace ID" + ':' + localProperties.getProperty("io.bitrise.gradle.remoteCache.authToken", "") } }
-
Replace the
workspace ID
in the script with the actual Workspace ID. -
Have each developer working on the project define their own personal access token in their
local.properties
file:io.bitrise.gradle.remoteCache.authToken = personal_access_token
This way the personal access token will not be hardcoded into the build configuration files and is instead loaded from the
local.properties
file.
A Step is a block of script execution that encapsulates a build task on Bitrise: the code to perform that task, the inputs and parameters you can define for the task, and the outputs the task generates.
A Workspace is an environment that allows you to manage your Bitrise apps and the team members working on the apps. You can create multiple Workspaces, and you can be invited to Workspaces by other Bitrise users. To be able to add apps and run builds, you either need to be part of a Workspace, or you have to be an outside contributor on an app's team.
A Workspace is an environment that allows you to manage your Bitrise apps and the team members working on the apps. You can create multiple Workspaces, and you can be invited to Workspaces by other Bitrise users. To be able to add apps and run builds, you either need to be part of a Workspace, or you have to be an outside contributor on an app's team.