Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.ProductFlavor:environment'

I have update gradle version in my library project from gradle 7 to gradle 8 and after publishing it, I'm not able to use it in my project that uses gradle 8 as well:
- Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.ProductFlavor:environment' with value 'production' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.ProductFlavor:environment' with value 'dev' - Other compatible attributes: - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.5.0') - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug') - Doesn't say anything about com.android.build.api.attributes.ProductFlavor:device (required 'google') - Doesn't say anything about com.android.build.api.attributes.ProductFlavor:type (required 'module') - Doesn't say anything about its target Java environment (preferred optimized for Android)
Answer
Starting of gradle 8, gradle has Strict Variant Matching, and when publishing library using gradle 8 or higher, it will publish metadata with library(.module file) and it will contain attributes like below:
"attributes": {
"com.android.build.api.attributes.ProductFlavor:environment": "production",
"environment": "production",
"org.gradle.category": "library",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "androidJvm"
}
and in your project, gradle will try to match selected environment to production and to solve it, you need to disable metadata publishing with library so gradle doesn't find anything to match.
Inised library build.gradle, Add below top-level configuration to file root, outside of android or any other function
tasks.withType(GenerateModuleMetadata) {
enabled = false
}
Enjoyed this article?
Check out more content on our blog or follow us on social media.
Browse more articles