Skip to content

Stack overflow with Dagger 2.56.2 #4702

Description

@jrgonzalezg

My app has been compiling fine until Dagger 2.56.1 (included). But if I try to update to Dagger/Hilt 2.56.2 I get this exception on every build:

Caused by: java.lang.StackOverflowError
        at com.squareup.kotlinpoet.TypeName.hashCode(TypeName.kt:111)
        at com.squareup.kotlinpoet.ParameterizedTypeName.hashCode(ParameterizedTypeName.kt:121)
        at com.squareup.kotlinpoet.TypeVariableName.hashCode(TypeVariableName.kt:92)
        at com.squareup.kotlinpoet.ParameterizedTypeName.hashCode(ParameterizedTypeName.kt:124)
        at com.squareup.kotlinpoet.TypeVariableName.hashCode(TypeVariableName.kt:92)
        at com.squareup.kotlinpoet.ParameterizedTypeName.hashCode(ParameterizedTypeName.kt:124)
        at com.squareup.kotlinpoet.TypeVariableName.hashCode(TypeVariableName.kt:92)
        at com.squareup.kotlinpoet.ParameterizedTypeName.hashCode(ParameterizedTypeName.kt:124)
        at com.squareup.kotlinpoet.TypeVariableName.hashCode(TypeVariableName.kt:92)

The stacktrace keeps repeating those lines which suggest some infinite recursion. And I have no information provided of which of my code leads to that.

Activity

  1. bcorso commented on Apr 23, 2025

    @bcorso

    This sounds similar to square/kotlinpoet#1737.

    It would be nice to have a repro, but we'd first need to know what type is causing this issue and for that we'd need more info in the stack trace to understand where this was called from in Dagger/Hilt. I'm not sure why the stacktrace isn't giving you more info about where the recursion originated from, but if you can somehow get that information we could do more to help.

  2. natros commented on Apr 24, 2025

    @natros
  3. jrgonzalezg commented on May 7, 2025

    @jrgonzalezg
    Author

    Sorry, but I have not yet been able to dig much into this due to some personal problems. If I get some free time and if I can create a reproducible example I will update later.

    From the comments above and the kotlinpoet issue my current guess is that it may be from types with self referencing generics for emulating self types like:

    interface PaginatedElement {
      val identifier: String
    }
    
    interface PaginatedSection<Element : PaginatedElement, Section : PaginatedSection<Element, Section>> {
      val identifier: String
      val elements: List<Element>
    
      fun withNewElements(elements: List<Element>): Section
    }
    

    and/or the actual classes implementing PaginatedSection and similar classes / interfaces.

  4. jrgonzalezg commented on Jun 4, 2025

    @jrgonzalezg
    Author

    I have been able to make a bit more progress here. I have not yet been able to identify which code causes the failure exactly, but there seems to be some incompatibility or odd interaction with the hilt extensions on AndroidX (JetPack). If I remove the dependency and the ksp compiler from that there is no longer an stack overflow happening. Upgrading from version 1.2.0 to version 1.3.0-alpha01 of AndroidX hilt does not help either. Only if I remove their hilt-compiler the build goes through with Dagger 2.56.2. But on Dagger 2.56.1 the build succeeded without issues with both ksp compilers included.

  5. emreozerdev commented on Feb 2, 2026

    @emreozerdev
    ### Workaround: Use KAPT instead of KSP
    
    I encountered the same `StackOverflowError` when upgrading to Dagger/Hilt 2.56.2+.
    
    **Root Cause:** The issue is in KotlinPoet's handling of recursively bound generics (e.g., `T : Comparable<T>`), which causes infinite recursion in `hashCode()` methods during annotation processing with KSP.
    
    **Workaround:** Switching from KSP to KAPT for Hilt annotation processing resolves this issue.
    
    // Before (KSP - causes StackOverflowError)
    plugins {
        id("com.google.devtools.ksp")
    }
    
    dependencies {
        ksp("com.google.dagger:hilt-compiler:2.59")
        ksp("com.google.dagger:hilt-android-compiler:2.59")
    }
    
    // After (KAPT - works fine)
    plugins {
        id("com.android.legacy-kapt")
    }
    
    dependencies {
        kapt("com.google.dagger:hilt-compiler:2.59")
        kapt("com.google.dagger:hilt-android-compiler:2.59")
    }
    
    

    Using this workaround, I was able to successfully upgrade to the latest version Dagger/Hilt 2.59.
    ⚠️ However, I'm not entirely comfortable with this solution since KAPT is an older annotation processing method and KSP is the recommended modern approach with better performance. It would be great to see a proper fix so we can use KSP with the latest Dagger/Hilt versions.

  6. bcorso commented on Feb 2, 2026

    @bcorso

    @emreozerdev, I looked into this issue recently and found that it was introduced by square/kotlinpoet#1477 and first released in KotlinPoet 1.13.0.

    Thus, another solution here is to try to downgrade your projects version of KotlinPoet to 1.12.0.

    Note that Dagger itself depends on KotlinPoet 1.12.0, but it's likely that something else in your build deps is pulling in a higher version.

  7. jrgonzalezg commented on Feb 2, 2026

    @jrgonzalezg
    Author

    Thanks for the findings!. Unfortunately Dagger Hilt for Android transitively depends on kotlinpoet 1.14.2 so it does have the issue. But that probably explains why I could use the non-Hilt variants without getting the same issue.

  8. bcorso commented on Feb 2, 2026

    @bcorso

    Unfortunately Dagger Hilt for Android transitively depends on kotlinpoet 1.14.2 so it does have the issue

    @jrgonzalezg, while not ideal, you can force Gradle to use a specific version of transitive dependencies (assuming the newer version of the transitive dependency isn't strictly necessary)

    I think there's a lot of ways to do it in Gradle, but something like this should work:

    configurations.all {
        resolutionStrategy.dependencySubstitution {
            substitute(module("com.squareup:kotlinpoet"))
                .using(module("com.squareup:kotlinpoet:1.12.0"))
                .because("Kotlinpoet 1.13+ introduces a bug (see https://github.com/square/kotlinpoet/issues/1737).")
        }
    }

    Note: The "com.squareup:kotlinpoet" artifact was changed to "com.squareup:kotlinpoet-jvm" in KotlinPoet 1.15.0, so you may also need to add a substitution for that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions