Best Practice for Handling Build Type-Specific Schema Endpoints in Android Multi-Module Setup

Hi Apollo Community,

I’m working on a multi-module Android project and trying to figure out the best way to handle schema downloads based on our app’s build types.

Our Setup:

  • :app module: Defines buildTypes (debug, stage, release) and uses buildConfigField to set the corresponding BASE_URL for each.

  • :graphql module: A separate Android library module responsible for Apollo code generation. It contains all our shared .graphql query/fragment files in src/main/graphql/com/example. It also needs to download the schema using introspection.

The Goal:

I want the :graphql module to download the schema from a different endpoint URL depending on the build type being compiled in the :app module (e.g., use http://dev/graphql for debug, https://stage/graphql for stage/release). I also have separate schema files for each environment (src/debug/graphql/.../schema.graphqls, src/stage/graphql/.../schema.graphqls, etc.).

The Problem:

The :graphql library module doesn’t have direct access to the buildTypes defined in the :app module, so I can’t easily read the BASE_URL or know which build type is active during the Apollo code generation phase.

My Attempt (which failed):

I tried defining separate Apollo service blocks in my :graphql/build.gradle.kts like this:

Kotlin

// In :graphql/build.gradle.kts
apollo {
    service("debug") {
        
        srcDir("src/debug/graphql/com/example")
        packageName.set("com.example.graphql")
        introspection {
            endpointUrl.set("http://dev/graphql")
            schemaFile.set(file("src/debug/graphql/com/example/schema.graphqls"))
        }
        outputDirConnection { connectToAndroidSourceSet("debug") }
    }
    service("stage") {
        srcDir("src/stage/graphql/com/example") 
        // ... similar config ...
    }
    service("release") {
        srcDir("src/release/graphql/com/example")
        // ... similar config ...
    }
}

This failed because my actual query files (.graphql) are located in the common src/main/graphql/com/example directory, not in the build-type specific directories. The configuration couldn’t find the queries.

The Question:

What is the recommended approach using the Apollo 4.x Gradle plugin to achieve this? How can I configure the schema download within my :graphql library module based on the :app module’s build type, while still using the shared query files from src/main/graphql?

Thanks for any guidance!