androidgradleandroid-gradle-pluginandroid-productflavorsandroid-flavors

Adding dependencies for a specific combination of buildType & productFlavor (`flavornameDebugImplementation`)


I have a gradle file in app/build.gradle with these flavors defined:

android {
    //...
    flavorDimensions 'cheese'
    productFlavors {
        brie {
            dimension 'cheese'
        }
        cheddar {
            dimension 'cheese'
        }
    }
}

I'm able to define dependencies specific to cheddar, brie, release or debug fine:

dependencies {
    //...
    implementation 'example:dependency:1.2.3'
    brieImplementation 'example:brie-only-1.2.3'
    cheddarImplementation 'example:cheddar-only-1.2.3'
    debugImplementation 'example:debug-only-1.2.3'
    releaseImplementation 'example:release-only-1.2.3'
}

I want to define a dependency that is specific to only brieDebug. I know I can use this combination in other ways, because I can define a source set for it in app/src/brieDebug/.

So now I try to define the brieDebug-specific dependency like this:

dependencies {
    //...
    brieDebugImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
}

But this produces a Gradle error:

A problem occurred evaluating project ':app'.
> Could not find method brieDebugImplementation() for arguments [com.squareup.okhttp3:okhttp:4.12.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I get the same error even like this:

dependencies {
    //...
    debugBrieImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
}

What am I doing wrong?

Is it even possible to define dependencies for a specific combination of build type and flavor like this?


Solution

  • According to relevant the Android Docs, dependency configuration methods which combine a product flavor and a build type need to be initialized manually (implementation and debugImplementation are examples of dependency configuration methods, as is brieDebugImplementation):

    //.gradle.kts example
    val brieDebugImplementation by configurations.creating
    
    dependencies {
        brieDebugImplementation("com.squareup.okhttp3:okhttp:4.12.0")
    }
    
    //.gradle example
    configurations {
        brieDebugImplementation {}
    }
    
    dependencies {
        brieDebugImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
    }
    

    Source: https://developer.android.com/build/dependencies#configure_dependencies_for_a_specific_build_variant