https://kotlinlang.org logo
Join SlackCommunities
Powered by
# kapt
  • m

    Martin Brehovsky

    10/26/2022, 6:40 PM
    Hi there, I have a custom annotation processor which looks for annotations attached to data class properties. Everything works fine for regular classes and primitives, but I get no annotations for a property of value class type. For example in this example:
    Copy code
    @Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
    @Retention(AnnotationRetention.RUNTIME)
    annotation class CustomAnnotation(
        val value: Boolean = true
    )
    
    @JvmInline
    value class ValueStringClass(
        val value: String
    )
    
    data class InlineClassTest(
        @CustomAnnotation
        val stringValue: ValueStringClass,
    
        @CustomAnnotation
        val anotherStringValue: String
    )
    In my AP I can get the annotation for
    anotherStringValue
    of type
    String
    , but there is no annotation present for
    stringValue
    of type
    ValueStringClass
    which is a value class. When I debugged this, I don't see
    stringValue
    exposed as a regular property from the class. Here's what I get from my AP when getting the type elements and their annotation mirrors for the `InlineClassTest`:
    Copy code
    element 'stringValue', kind 'FIELD', annotationMirrors '@org.jetbrains.annotations.NotNull'
    element 'anotherStringValue', kind 'FIELD', annotationMirrors '@org.jetbrains.annotations.NotNull'
    element 'equals', kind 'METHOD', annotationMirrors '@java.lang.Override'
    element 'hashCode', kind 'METHOD', annotationMirrors '@java.lang.Override'
    element 'toString', kind 'METHOD', annotationMirrors '@org.jetbrains.annotations.NotNull,@java.lang.Override'
    element '<init>', kind 'CONSTRUCTOR', annotationMirrors ''
    element 'component2', kind 'METHOD', annotationMirrors '@org.jetbrains.annotations.NotNull'
    element 'getAnotherStringValue', kind 'METHOD', annotationMirrors '@org.jetbrains.annotations.NotNull'
    element 'getAnotherStringValue$annotations', kind 'METHOD', annotationMirrors '@com.doordash.protobuf.mapper.test.CustomAnnotation,@java.lang.Deprecated'
    Unfortunately the code depends on 3rd party library which switched from using a wrapper around a String to a value class representing the string. Is there anything I can do to make the value class type to behave like a regular class from the AP perspective? Or something else to look for in the AP to get the annotation associated with that field in the data class? Thx!
    • 1
    • 1
  • o

    Odin

    11/06/2022, 6:29 PM
    Hi, I was wondering, is there a way to check that you are using the new JVM IR backend for the KAPT stub generation task? I have added the flag and bumped to Kotlin 1.7.20 as by https://kotlinlang.org/docs/whatsnew1720.html#support-for-the-jvm-ir-backend-in-kapt-stub-generating-task suggests. However, I can’t seem to get repeatable annotations to work for my annotation processor 🤔
    t
    • 2
    • 2
  • j

    Jerv Norsk

    02/21/2023, 12:21 PM
    Hello! I'm trying to use MapStruct on Kotlin Multiplatform. At the moment I'm using Kotlin Multiplatform 1.8.0, same version for kotlin-kapt with Gradle 8.0 and OpenJDK 19. I had try to use kapt in the dependencies block of my gradle but I can not found the function. Is kapt dependency function removed from plugin? If yes: how I can tell kapt to use the mapstruct-processor? My not buildable gradle build file block:
    Copy code
    dependencies {
        kapt("org.mapstruct:mapstruct-processor:latest.release")
    }
    b
    e
    • 3
    • 22
  • s

    Sha Sha Chu

    03/03/2023, 5:49 PM
    we're seeing this in our android build output. is it an us problem or a kotlin/kapt problem?
    Copy code
    The following annotation processors are not incremental: kotlin-compiler-embeddable-1.7.10.jar (org.jetbrains.kotlin:kotlin-compiler-embeddable:1.7.10).
    Make sure all annotation processors are incremental to improve your build speed.
    t
    • 2
    • 9
  • s

    Sha Sha Chu

    03/15/2023, 2:29 PM
    what are the levers we have in terms of how much memory is available to kapt? is it just the kotlin compiler daemon memory params?
    t
    • 2
    • 6
  • m

    Marek Kubiczek

    03/16/2023, 8:21 PM
    It looks like kapt/ksp have biggest impact on incremental compilation times in our Android project. Even if I change a little detail in a class (non-abi change) that doesn’t even contain any Hilt/dagger annotations and is hidden behind interface, I still see kapt task taking significant time. I suppose a lot of stubs are being regenerated which has further impact on compilation times. Is there a way to know what prevented the kapt task from considering being up-to-date?
    t
    • 2
    • 1
  • b

    Brais Gabin

    04/27/2023, 3:27 PM
    Why gradle doesn't reexecute the
    kapt
    task after I change an argument in the extensions?
    Copy code
    kapt {
        arguments { 
            arg("foo", "bar") // I change "bar" for anything else and kapt is not reexecuted.
        }
    }
    t
    • 2
    • 2
  • j

    juliocbcotta

    05/10/2023, 10:02 AM
    anyone here was able to migrate to JDK 17 and keep kapt working on android projects? Related link
    t
    t
    • 3
    • 9
  • g

    Gavin Ray

    08/06/2023, 5:02 PM
    The
    KaptOptions.Builder()
    API has an argument for passing options to
    javac
    , but it expects
    Map<String, String>
    Does anyone know how you can pass arguments like
    --enable-preview
    which don't have an
    -flag=X
    value?
    Copy code
    public final class KaptOptions : org.jetbrains.kotlin.base.kapt3.KaptFlags {
       // ...
        public final class Builder public constructor() {
            // ...
            public final val javacOptions: kotlin.collections.MutableMap<kotlin.String, kotlin.String> /* compiled code */
    I tried to search Github code usage and dug through hundreds of repos, but I couldn't find a single one which passes an argument like
    --enable-preview
    to
    Kapt
    ✅ 1
    t
    • 2
    • 3
  • k

    kevin.cianfarini

    08/29/2023, 2:33 PM
    We’re running into an issue where
    kapt
    does not seem to see sources in our
    commonMain
    sourceset when generating stubs for android after the upgrade to Kotlin 1.9.0. In Kotlin 1.8.22 stubs were generated for everything in the
    androidMain
    sourceset and the
    commonMain
    sourceset, but after upgrading to 1.9.0 we’re only seeing stubs generated for the
    androidMain
    sourceset. Does anyone have any tips on how we might begin to debut this? How does kapt locate source files to generate stubs for?
    t
    r
    • 3
    • 18
  • b

    Brais Gabin

    09/14/2023, 10:08 AM
    Hello! I have a annotation processor that generates some files. Now I want a gradle task to read those files and do things based on them. Is there an easy way to find those files? I'm hardcoding
    generated/source/kapt
    on the configuration of my task but it doesn't look safe. Is there something on the kapt gradle api I can use to get that path?
    t
    • 2
    • 2
  • a

    Anders Carlsen

    09/16/2023, 10:42 AM
    hi, I am trying to make a multiplatform project with jvm and androidTarget - in the Android part I use Hilt to annotate some shared viewmodels, this works fine when running on android, but when trying to build jvm target it fails:
    Copy code
    Execution failed for task ':moduleName:kaptKotlinJvm'.
    > Cannot query the value of this provider because it has no value available.
      The value of this provider is derived from:
        - task ':moduleName:kaptGenerateStubsKotlinJvm' property 'kotlinCompileDestinationDirectory'
    If I remove Kapt plugin it works on jvm, but the Hilt kapt tasks won’t be run and I get runtime error when using viewModel on Android. this is the build.gradle.kts file:
    Copy code
    plugins {
        kotlin("multiplatform")
        id("com.android.library")
        kotlin("kapt")
    }
    
    kotlin {
        jvm()
        androidTarget()
    
        sourceSets {
    
            val commonMain by getting {
                dependencies {
                    implementation(Deps.kotlinCoroutines)
                }
            }
    
            val commonTest by getting {
                dependencies {
                    implementation(kotlin("test"))
                }
            }
    
            val jvmMain by getting {
                dependencies {
                    implementation(Deps.voyagerNavigator)
                    implementation(Deps.voyagerKoin)
                }
            }
    
            val androidMain by getting {
                kotlin.srcDir("src/commonMain/kotlin")
                dependencies {
                    implementation(Deps.kotlinCoroutinesAndroid)
                    implementation(Deps.androidxLifecycleViewModel)
                    implementation(Deps.googleHilt)
                    configurations["kapt"]?.dependencies?.add(project.dependencies.create(Deps.googleHiltCompiler))
                }
            }
        }
    }
    
    android {
        namespace = "package"
        compileSdk = ConfigData.androidCompileSdkVersion
    
        defaultConfig {
            minSdk = ConfigData.androidMinSdkVersion
        }
        sourceSets["main"].apply {
            manifest.srcFile("src/androidMain/AndroidManifest.xml")
            res.srcDirs("src/androidMain/resources")
            resources.srcDirs("src/commonMain/resources")
        }
        compileOptions {
            sourceCompatibility = Versions.javaVersion
            targetCompatibility = Versions.javaVersion
        }
        kotlin {
            jvmToolchain(Versions.javaJvmTarget.toInt())
        }
    }
    t
    m
    • 3
    • 11
  • m

    Mattia Tommasone

    09/19/2023, 4:26 PM
    Hi, I’m having issues publishing a multimodule project to maven central:
    Copy code
    > Task :modules:mockk-agent-android:dokkaJavadoc FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    A problem was found with the configuration of task ':modules:mockk-agent-android:dokkaJavadoc' (type 'DokkaTask').
      - Gradle detected a problem with the following location: '/Users/raibaz/dev/mockk/modules/mockk-agent-android/build/tmp/kapt3/classes/release'.
    
        Reason: Task ':modules:mockk-agent-android:dokkaJavadoc' uses this output of task ':modules:mockk-agent-android:kaptReleaseKotlin' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
        Possible solutions:
          1. Declare task ':modules:mockk-agent-android:kaptReleaseKotlin' as an input of ':modules:mockk-agent-android:dokkaJavadoc'.
          2. Declare an explicit dependency on ':modules:mockk-agent-android:kaptReleaseKotlin' from ':modules:mockk-agent-android:dokkaJavadoc' using Task#dependsOn.
          3. Declare an explicit dependency on ':modules:mockk-agent-android:kaptReleaseKotlin' from ':modules:mockk-agent-android:dokkaJavadoc' using Task#mustRunAfter.
    Thing is, I don’t know how to declare an explicit dependency on
    kaptReleaseKotlin
    Any pointers?
    t
    s
    • 3
    • 2
  • s

    statmark56

    09/29/2023, 12:46 PM
    Can we somewhat disable/"unregister" a Processor that previously registered either via @AutoService or META-INF? My case is I want to disable some processor from kapt when running unit test because it's unnecessary to execute them. Thanks
    t
    b
    • 3
    • 4
  • b

    Brais Gabin

    11/13/2023, 10:07 AM
    I'm developing a Gradle Plugin that needs to read an output generated by an annotation processor with kapt. For that reason I'm depending on kapt task and all of that works fine. But now I want to test it because I want to also support the Android flavours and without tests that's a mess. When I run:
    Copy code
    val project = ProjectBuilder.builder()
        .build()
    
    project.pluginManager.apply(KotlinPluginWrapper::class.java)
    project.pluginManager.apply(Kapt3GradleSubplugin::class.java)
    project.pluginManager.apply(LightsaberPlugin::class.java)
    I can see the tasks generated by kotlin and my plugin but I can't see any task generated by kapt. This makes that my tests pointless because want I want is to assert that the tasks that my plugin adds depends on the correct kapt tasks (for example, that
    :lightsaberDebugCheck
    depends on
    :kaptDebugKotlin
    ). I have some tests using
    GradleRunner
    but those tests are way slower and I can't assert the dependencies I just can assert that "X" task was executed without knowing the reason. Is there a way to make this work with
    ProjectBuilder
    ? Any other idea to how to test this?
    t
    • 2
    • 4
  • e

    eygraber

    11/23/2023, 7:28 PM
    Edit: Solved After updating to Kotlin 1.9.20 I get the following error:
    Copy code
    Plugin [id: 'org.jetbrains.kotlin.kapt'] was not found in any of the following sources:
    
    - Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
    - Plugin Repositories (plugin dependency must include a version number for this source)
    It was working fine before like this:
    Copy code
    plugins {
      kotlin("kapt")
    }
    It seems to work if I manually add the version:
    Copy code
    plugins {
      kotlin("kapt") version "1.9.20"
    }
    t
    • 2
    • 2
  • s

    scana

    12/22/2023, 11:42 AM
    Hi all! Does it make sense to only enable KAPT for modules only if they are being tested? E.g. run it only if
    testDebug
    task is being called. Using Anvil right now with
    generateDaggerFactories
    flag for regular app building flow but we have to use
    kaptTest
    to build local Dagger components in tests. I feel like this causes unnecessary stub generation tasks to run for regular compilation and was wondering if I could get rid of it somehow while keeping KAPT in tests. think smart
    b
    • 2
    • 1
  • z

    Zac Sweers

    12/30/2023, 3:46 AM
    Is there an issue tracking Gradle project isolation support for kapt? I’ve looked around for one but not found one
  • z

    Zac Sweers

    12/30/2023, 3:55 AM
    filed this for a specific issue https://youtrack.jetbrains.com/issue/KT-64627
    thank you color 1
  • r

    Rahul

    01/01/2024, 5:26 AM
    We are using KAPT with map struct for a kotlin springboot project with gradle as build tool
    Copy code
    implementation('org.mapstruct:mapstruct:1.5.5.Final')
    kapt('org.mapstruct:mapstruct-processor:1.5.5.Final')
    Wanted to know if there is a way to restrict the annotation processing to be applied only to src code produced by the us and exclude the 3rd party libraries.
    t
    • 2
    • 2
  • r

    ritesh

    01/31/2024, 5:24 PM
    Running into kapt cache issue while switching branches (when code-gen source-code gets changed, like Dagger). Cleaning, incremental cache fixes the issues. Ideally, source change should be caught by kapt compiler and should re-generate / remove the generated files accordingly. Any one noticed similar issue with recent upgrades - I am on kotlin
    1.9.21
    and dagger -
    2.48.1
    t
    • 2
    • 2
  • a

    Antoni Zwolski

    02/10/2024, 6:23 PM
    Guys, any ideas about what can cause the problem? I am stuck with this: https://stackoverflow.com/questions/77964966/failure-occurred-while-executing-org-jetbrains-kotlin-gradle-internal-kaptwithou
  • a

    Antoni Zwolski

    02/11/2024, 10:45 PM
    Any ideas? I did a little update, adding a kotlin interface leads to
    Copy code
    WARNING: An illegal reflective access operation has occurred
    and
    Copy code
    Execution failed for task ':modules:backend-api:category:category-api:kaptGenerateStubsTestKotlin'.
    > java.io.IOException: The system cannot find the path specified
    t
    • 2
    • 9
  • a

    Antoni Zwolski

    02/15/2024, 3:46 PM
    Still same issue:
    • 1
    • 6
  • y

    Yurii Surzhykov

    02/29/2024, 7:08 PM
    Good day everyone! I have a question about KAPT(or maybe about KSP). I'm trying to write own ksp plugin to automatically generate Room's DAOs, database class, entities based on the classes created by me. There is custom annotation -
    @PersitableEvent
    , which is used to annotate the class for which my KSP plugin have to generate entity class, mapper class, DAO interface and there is also autogenerated database class with all those DAOs. The situation is the following, let's assume there is a class
    Copy code
    @PersitableEvent
    data class PreviouslyCreatedClass(
      val someProperty: String
    )
    The problem is next. When I'm trying to compile everything from scratch(without '_build/generated'_ folder), everything works as expected, each class is generated and everything compiles and runs. But then, I'm adding a new class with
    @PersitableEvent
    annotation, and ksp task completes successfully, but kapt shows me error: kaptGenerateStubsDebugKotlin: Unresolved reference
    PreviouslyCreatedClassDao
    in my AutoGeneratedDatabase class. And there is also message below this error: kaptGenerateStubsDebugKotlin: Failed to restore task output as snapshot file app/build/snapshot/kotlin/kaptGenerateStubsDebugKotlin/2.zip does not exist! I don't understand what is wrong with it, but when I compile again, it compiles successfully(maybe because second time it compiles everything from scratch and not uses cache). Can someone explain me what the error means, and how to solve it? Currently I removed kapt plugin and the error is disappeared but I would probably need kapt in the future(for dagger). P.s.
    PreviouslyCreatedClassDao
    is an autogenerated class P.p.s My Kotlin version is 1.9.20, and ksp version is 1.9.20.-1.0.14
    e
    • 2
    • 5
  • d

    Dmytro Serdiuk

    02/29/2024, 11:42 PM
    Hello! I have a question - why for caching classloaders we need to disable AP discovery? Thanks!
  • b

    Berkeli Alashov

    08/05/2024, 10:56 PM
    Hello, Seeing
    w: Kapt currently doesn't support language version 2.0+. Falling back to 1.9.
    in logs means it applies only for kapt right? i.e rest of the project is using kotlin 2.0, but kapt itself is falling back kotlin 1.9? Context: we have upgraded most of our modules to use kotlin 2.0 & ksp, but only 2 modules are using
    id("kotlin-kapt")
    (because of room-java) I'm guessing we will still be able to use kotlin 2.0 in those modules using kotlin-kapt, right?
    👌 2
    d
    • 2
    • 1
  • s

    Suyash

    09/17/2024, 6:49 AM
    “Does anyone here use the
    compile
    value in KAPT's
    aptMode
    ? If so, how do you use it, and what would be the impact if it were deprecated?”
  • r

    rkeazor

    10/11/2024, 10:58 AM
    Hey in Kapt4 for k2 it seems that when trying use element.getAnnontation(...) , annotation like @delegate:annontation are not being picked up. Anyone know a work around for this
    t
    u
    • 3
    • 3
  • e

    edrd

    12/05/2024, 3:02 PM
    Does kapt with K2 means kapt will run as a compiler plugin (possibly during the frontend phase of the compilation)?
    d
    • 2
    • 2