Open BraisGabin opened 1 year ago
Agreed - I'm exploring this for v2 of the Gradle plugin.
I think that we should follow that lead and use the default variant to make it easier to execute detekt.
Oh TIL! Yup we should totally do that rather than doing every variant out there.
This issue is stale because it has been open 90 days with no activity. Please comment or this will be closed in 7 days.
I'm not confident this is possible, or at least, I don't see any way to exactly replicate the logic AGP uses for Lint default variant.
We need a fallback position for Android. My thinking is that we either don't hook anything up for now, and think of something better post-2.0.0, or we come up with our own defaults. AGP creates debug & release build types and no product flavors. Perhaps we enable detekt analysis tasks for all debug variants by default? And keep or uplift the DSL to filter flavors, build type and variants.
This is the Android Gradle plugin's code to select the default variant, for reference: https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/internal/variant/VariantModelImpl.kt;l=80-126;drc=4abb31f5ea388f9dfeb10bea17513543b101c203
This prioritizes by, in descending order of preference:
- The build author's explicit build type settings
- The build author's explicit product flavor settings, matching the highest number of chosen defaults
- The fallback default build type, which is the tested build type, if applicable, otherwise 'debug'
- The alphabetically sorted default product flavors, left to right
It also factors in variant filters that are in effect.
We can only get some of this information through the API. AGP has various extension points, we can hook into finalizeDsl
, beforeVariants
or onVariant
.
When using the application AGP plugin:
finalizeDsl
is closest to providing what we need, at least for determining what was set as default. We can access ApplicationExtension
here. However the values are mutable in this phase, and documentation says "After this callback finishes executing, the DSL objects are no longer useful and you should no longer hold references to them or modify their values." implying we should not hold references to the isDefault
property from here on. We also won't know which variants are filtered since that happens later.beforeVariants
ApplicationVariantBuilder
doesn't include isDefault
onVariants
ApplicationVariant
doesn't include isDefault
For reference: I raised a feature request to the android issue tracker: https://issuetracker.google.com/issues/372750810
Right now if you execute
androidMain
on an Android module you runandroidDebugMain
andandroidReleaseMain
. Usually they are exactly the same and we just consume our user's time.I just found that Android Lint uses something called "Default variant":
(extracted from: https://developer.android.com/studio/write/lint)
I think that we should follow that lead and use the default variant to make it easier to execute detekt.