Eugene H
Eugene H

Reputation: 3568

ProductFlavor not reference a local library still requires matchingFallbacks. How do I prevent this error or suppress it?

I have been facing many issues with updating a project with very complex flavors to gradle 3.+.

The problem: Some flavors that do not depend on local libraries. Although, some flavors do not rely on the library, each app flavor is forcing me to add a matching fallback of the lib flavor. Even if I put the local lib dependency declared inside a single flavor of the app, it doesn't change anything. Is there any way to suppress this error for specific app flavors or do I just have to put matching fallbacks for each flavor regardless of its dependence on the local lib?

If I do have to put matchingFallback in every flavor even if the app doesn't depend on the lib, why didn't they allow us to declare matchingFallbacks, inside of the default config, and override it inside our app flavors like missingDimensionStrategy?

Local Library build.gradle

Note: This gradle has two distinct flavors for a single dimension. Requiring matchingFallbacks in app to reference a flavor that share the same dimension.

apply plugin: 'com.android.library'

android {
    ...
    defaultConfig {
        ...
    }
    buildTypes {
        release {...}
        debug {...}
    }
    flavorDimensions flavor.default
    productFlavors {
        libFlavor1 {
        }
        libFlavor2 {
        }
    }
}

dependencies {
...
}

App build.gradle

apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {...}
    buildTypes {
        release {...}
        debug {...}
    }
    flavorDimensions flavor.default
    productFlavors {
        // Flavor that requires library
        appFlavor1 {
            matchingFallbacks = ['libFlavor1', 'libFlavor2']
        }
        // Does not require library but still requires matchingFallbacks?
        appFlavor2 {}
    }
}

dependencies {
    implementation project(':library')
}

Failed solution

I have tried to move the local lib to the flavor but the other libs still require a matching fallback.

apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {...}
    buildTypes {
        release {...}
        debug {...}
    }
    flavorDimensions flavor.default
    productFlavors {
        // Flavor that requires library
        // I have tried to move the local lib to the flavor
        // But the other flavor requires a fallback
        appFlavor1 {
            matchingFallbacks = ['libFlavor1', 'libFlavor2']
            dependencies {
                implementation project(':library')
            }
        }
        // Does not require library but still requires matchingFallbacks?
        appFlavor2 {}
    }
}

dependencies {
    // implementation project(':library')
    ...
}

Upvotes: 3

Views: 4023

Answers (1)

xiaomi
xiaomi

Reputation: 6703

If you want to add a dependency to a particular flavor, you can use <flavor's name>Implementation key word:

android {
    ...
    productFlavors {
        appFlavor1 {
            //MatchingFallbacks is required because of its dependencies to the project 'library'
            matchingFallbacks = ['libFlavor1', 'libFlavor2']
            //Don't use dependencies node here, it will add dependencies to the other flavors
        }
        appFlavor2{/*no need to use matchingFallbacks here, there is no dependencies to project 'library'*/}
    }
    ...
}

dependencies {
    appFlavor1Implementation project(':library')
}

Upvotes: 5

Related Questions