detekt / detekt

Static code analysis for Kotlin
https://detekt.dev
Apache License 2.0
6.26k stars 773 forks source link

Support default variants on Android #6232

Open BraisGabin opened 1 year ago

BraisGabin commented 1 year ago

Right now if you execute androidMain on an Android module you run androidDebugMain and androidReleaseMain. 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":

If your project includes build variants, lint checks only the default variant. If you want to run lint on a different variant, you must capitalize the variant name and prefix it with lint.

(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.

3flex commented 1 year ago

Agreed - I'm exploring this for v2 of the Gradle plugin.

cortinico commented 1 year ago

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.

detekt-ci commented 1 year ago

This issue is stale because it has been open 90 days with no activity. Please comment or this will be closed in 7 days.

3flex commented 3 weeks ago

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:

BraisGabin commented 1 week ago

For reference: I raised a feature request to the android issue tracker: https://issuetracker.google.com/issues/372750810