Google Calendar API fails in production build but works in debug mode

I’m having trouble with the Google Calendar API when I build my Android app for production. Everything works perfectly in debug mode but breaks when I create a release build.

My Setup

I’m trying to fetch events from a public calendar using a service account. Here’s how I initialize the credentials:

AssetManager assets = getAssets();
InputStream credentialStream = assets.open("service-account.json");

GoogleCredential auth = GoogleCredential.fromStream(credentialStream);
auth = auth.createScoped(Collections.singletonList("https://www.googleapis.com/auth/calendar.readonly"));

Building the Calendar Client

Calendar calendarService = new Calendar.Builder(
    AndroidHttp.newCompatibleTransport(),
    new JacksonFactory(),
    auth
).setApplicationName("MyCalendarApp").build();

Fetching Events

com.google.api.services.calendar.model.Events upcomingEvents = 
    calendarService.events().list("[email protected]")
        .setTimeMin(new DateTime(new Date(), TimeZone.getDefault()))
        .setMaxResults(10)
        .setOrderBy("startTime")
        .setSingleEvents(true)
        .setShowDeleted(false)
        .execute();

The Problem

In debug mode, this works great. But when I sign the APK for release, I get a 404 error. I also tried adding an API key, but then I received a 403 error about IP restrictions, even though I configured both debug and release SHA-1 fingerprints in the console.

Has anyone else run into this issue? What am I missing for production builds?

This is definitely a ProGuard obfuscation issue. Happens all the time with Google API libraries when you build for release. ProGuard runs automatically on signed APKs and strips away classes or renames methods that the Calendar API needs. I hit the exact same problem last year with another Google API. Fixed it by adding specific ProGuard rules to preserve the classes. Throw these lines into your proguard-rules.pro file:

-keep class com.google.api.services.calendar.** { *; }
-keep class com.google.api.client.** { *; }
-dontwarn com.google.api.**

Also worth checking if you’ve got different network security configs between debug and release builds. Release builds sometimes have stricter network policies that block API calls. That 404 error screams service account authentication failing silently in the obfuscated build - which circles back to ProGuard being the culprit.