https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Philip Dukhov

04/12/2021, 3:38 PM
Hi all! I’m trying to build a local annotation processor for my kotlin multiplatform module, and I’ve faced a couple of problems. 1. Cache looks not working at all in KMP common code. When I update annotation params or remove it, it doesn’t gets regenerated - actually I get old params in the
process
function. Only removing
build/tmp/kapt3
helps. If I use same annotation in my android target, this works fine. 2. In both cases (multiplatform/android modules) generated classes are not recognized by the AS - they are marked as
Unresolved reference
. But the build goes fine. My setup:
annotations/build.gradle.kts
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android()
    jvm()
    ios()
}
annotationProcessor/build.gradle.kts
Copy code
plugins {
    kotlin("multiplatform")
}

kotlin {
    jvm()
    sourceSets {
        val jvmMain by getting {
            dependencies {
                implementation("com.squareup:kotlinpoet:1.8.0")
                implementation(project(":annotations"))
            }
        }
    }
}
src/jvmMain/resources/META-INF/services/javax.annotation.processing.Processor
file contains my processor
shared/build.gradke.kts
Copy code
plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    kotlin("kapt")
}

kapt {
    useBuildCache = false // even this didn't helped with the cash problem
}

kotlin {
    android()
    ios()
    cocoapods {
        this.frameworkName = "shared"
        summary = "shared"
        homepage = "-"
        license = "-"
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(project(":modules:annotations"))
                configurations["kapt"].dependencies.add(project(":modules:annotationProcessor"))
            }
        }
    }
}
Have anyone faced any of these?
r

russhwolf

04/12/2021, 3:45 PM
kapt is really a JVM tool. It gets messy trying to use it for multiplatform because it only wants to run as part of the JVM compilation. It sounds like we might see multiplatform KSP eventually which would be better for multiplatform annotation processing, but that's waiting on some upstream changes in the compiler first so I think it's 1.5.20 at the earliest (and probably later). You could also try out mpapt but it hasn't had a ton of attention recently https://github.com/Foso/MpApt
👍 1
m

Michal Klimczak

04/12/2021, 3:49 PM
1. I also have issues with cache. I never really tried to thoroughly test or summarise the issues with cache, though. It's just that sometimes I need to remove build dir or gradle clean for the processor to properly generate again. If you find out what's wrong I'd be glad to know as well. 2. I had this problem when I tried to have a pure jvm processor. I think they disappeared when I made it depend on multiplatform plugin. But maybe there was something more, I will look at my gradle files, maybe I'm gonna find it in my memory.
👍 1
Oh, you meant that the generated classes are not visible? I'm not sure how you configured your processor, but my is not generating to
build/tmp/kapt3
, but to
build/generated/source/kaptKotlin
. It's set here: https://github.com/FutureMind/koru/blob/master/koru-processor/src/jvmMain/kotlin/com/futuremind/koru/processor/Processor.kt#L40 And the client of the lib has to add it to srcDirs like this
Copy code
kotlin.srcDir("${buildDir.absolutePath}/generated/source/kaptKotlin/")
See README
p

Philip Dukhov

04/12/2021, 5:15 PM
No,
build/tmp/kapt3
is a folder that contains cache files, not the generated ones. I just was looking for a way to reset cache without cleaning whole folder, and found this one. Yes, I’m saving my file using
“kapt.kotlin.generated”
value Generated files are not visible by IDE, but visible at build time. Now sure why it’s so - files generated by other plugins works fine
m

Michal Klimczak

04/12/2021, 5:41 PM
I have also noticed that you don't have the java-library plugin in your processor and I do, but I don't think it would cause that issue
tbh I just had an issue of old cache in dagger. so it's probably not just multiplatform
p

Philip Dukhov

04/14/2021, 6:11 AM
I think it’s possible to add gradle task to clean build/tmp/kapt3 folder on each build, will check out if it’ll increase build time dramatically. I’ve fixed “Unresolved reference” problem by specifying custom kapt output folder and including it to commonMain sources.
👍 1
14 Views