An implementation of the Gradle Remote Cache that's backed by Google Cloud Storage buckets.
- Gradle 8.4 or newer
In your settings.gradle.kts file add the following
import androidx.build.gradle.gcpbuildcache.GcpBuildCache
import androidx.build.gradle.gcpbuildcache.GcpBuildCacheServiceFactory
import androidx.build.gradle.gcpbuildcache.ExportedKeyGcpCredentials
plugins {
id("androidx.build.gradle.gcpbuildcache") version "1.0.1"
}
buildCache {
remote(GcpBuildCache::class) {
projectId = "foo"
bucketName = "bar"
credentials = ExportedKeyGcpCredentials(File("path/to/credentials.json"))
isPush = inCi
}
}projectId,bucketNameare requiredcredentialsdefaults toApplicationDefaultGcpCredentials, but can also be set toExportedKeyGcpCredentialsisPushdefaults tofalse.
If you are using Groovy, then you should do the following:
plugins {
id("androidx.build.gradle.gcpbuildcache") version "1.0.1"
}
import androidx.build.gradle.gcpbuildcache.GcpBuildCache
import androidx.build.gradle.gcpbuildcache.GcpBuildCacheServiceFactory
import androidx.build.gradle.gcpbuildcache.ExportedKeyGcpCredentials
buildCache {
remote(GcpBuildCache) {
projectId = "projectName"
bucketName = "storageBucketName"
credentials = new ExportedKeyGcpCredentials(new File("path/to/credentials.json"))
push = inCi
}
}- Install
gcloudCLI on your machine - Create a GCP project
YOUR-GCP-PROJECTand set up billing. - Create a Google Cloud Storage bucket
gsutil mb –p YOUR-GCP-PROJECT gs://YOUR-BUCKET-NAME- Create IAM roles for read and read/write
gcloud iam roles create CacheReadWrite --project=YOUR-GCP-PROJECT --title=CacheReadWrite --description="Have access to read and write to remote Gradle cache" --permissions=storage.buckets.get,storage.objects.create,storage.objects.delete,storage.objects.get,storage.objects.getIamPolicy,storage.objects.list
gcloud iam roles create CacheRead --project=YOUR-GCP-PROJECT --title=CacheRead --description="Have access to read from remote Gradle cache" --permissions=storage.buckets.get,storage.objects.get,storage.objects.getIamPolicy,storage.objects.list- Create IAM Service Accounts
gcloud iam service-accounts create cache-read-write --project=YOUR-GCP-PROJECT
gcloud iam service-accounts create cache-read --project=YOUR-GCP-PROJECT- Grant the service account roles that we just created
gcloud projects add-iam-policy-binding YOUR-GCP-PROJECT --member=serviceAccount:cache-read@YOUR-GCP-PROJECT.iam.gserviceaccount.com --role=projects/YOUR-GCP-PROJECT/roles/CacheRead
gcloud projects add-iam-policy-binding YOUR-GCP-PROJECT --member=serviceAccount:cache-read-write@YOUR-GCP-PROJECT.iam.gserviceaccount.com --role=projects/YOUR-GCP-PROJECT/roles/CacheReadWrite- Use
YOUR-GCP-PROJECTandYOUR-BUCKET-NAMEin the plugin configuration with exported service account credentials.