Skip to content

Task-level excludes remove files from type resolution with AGP 9 built-in Kotlin #9304

@kamil-intive

Description

@kamil-intive

Expected Behavior

When running an Android variant Detekt task with type resolution, files excluded from rule analysis via Gradle task-level exclude(...) should still be available to Detekt/Kotlin Analysis API for type resolution.

In other words, excluded source files should not report findings, but other analyzed files should still be able to resolve types declared in those excluded files.

This appears related to the intended behavior from #4465 and #9082.

Observed Behavior

With AGP 9 built-in Kotlin/new DSL enabled, excluding generated source files at the Detekt task level removes them from Detekt's type-resolution context.

Example configuration:

tasks.withType<Detekt>().configureEach {
    exclude("**/designsystem/tokens/**")
}

The excluded files are generated token files. Other source files in the same Android library module reference types from those token files.

When running a concrete flavored Android variant task, for example:

./gradlew detektBrandADebug

Detekt no longer reports findings from the excluded token files, so the task-level exclude itself is applied. However, Detekt then reports many compiler errors during analysis:

There were 1004 compiler errors found during analysis. This affects accuracy of reporting.
Run detekt CLI with --debug or set `detekt { debug = true }` in Gradle to see the error messages.

After that, Detekt can report false positives in non-excluded files. For example:

src/main/java/.../SomeComposable.kt:142:9
This expression is unreachable code which should either be used or removed. [UnreachableCode]

If the same files are not excluded at the Gradle task level, type resolution works. If we instead exclude the generated token paths at the Detekt config/rule-set level, Detekt also works because the files remain available for analysis context but rule findings are suppressed.

This suggests the excluded files are removed from both:

  1. the rule input, and
  2. the type-resolution / Analysis API source context.

The second part is unexpected.

Steps to Reproduce

In an Android project using AGP 9 built-in Kotlin/new DSL:

  1. Use Detekt 2.0.0-alpha.3.
  2. Create an Android library module with product flavors, for example:
    • brandA
    • brandB
  3. Add generated Kotlin source files under a package/path such as:
src/main/java/com/example/designsystem/tokens/AppColors.kt
src/brandA/java/com/example/designsystem/tokens/BrandAColors.kt
src/brandB/java/com/example/designsystem/tokens/BrandBColors.kt
  1. Add normal source files outside that path that reference types from the tokens package.
  2. Configure Detekt with a Gradle task-level exclude:
import dev.detekt.gradle.Detekt

tasks.withType<Detekt>().configureEach {
    exclude("**/designsystem/tokens/**")
}
  1. Run a concrete flavored Android variant Detekt task:
./gradlew detektBrandADebug
  1. Observe that:
    • token files are excluded from reported findings,
    • Detekt reports compiler errors during analysis,
    • Detekt may report false positives in non-token files due to unresolved token references.

Context

We have generated files that violate some style/formatting rules, we do not want Detekt findings from those generated files.

Before moving fully to AGP 9 built-in Kotlin/new DSL, task-level excludes worked for this use case.

After removing these AGP workaround properties:

android.builtInKotlin=false
android.newDsl=false

the task-level exclude still removes generated files from Detekt task sources, but type resolution becomes incomplete and causes false positives in regular source files.

Workaround:

Instead of task-level excludes, we can exclude the generated path in detekt-config.yml at the rule-set level:

complexity:
  active: true
  excludes:
    - '**/designsystem/tokens/**'

style:
  active: true
  excludes:
    - '**/designsystem/tokens/**'

This works because the generated files remain in the analysis context, while rule findings are suppressed.

However, this is more verbose and does not match the expected behavior of task-level excludes with type resolution.

Your Environment

  • Version of detekt used: 2.0.0-alpha.3
  • Version of Gradle used: 9.4.1
  • Android Gradle Plugin version: 9.1.1
  • Kotlin version: 2.3.21
  • Operating System and version: macOS 26.4.1
  • IDE: Android Studio Panda 4 | 2025.3.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions