Foso / Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native, WasmJs)) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
https://foso.github.io/Ktorfit
Apache License 2.0
1.6k stars 43 forks source link

Not working KSP (Unresolved reference 'createUserApi') #616

Closed alekseyHunter closed 1 month ago

alekseyHunter commented 4 months ago

Ktorfit version

2.0.0

What happened and how can we reproduce this issue?

Hello!

I got the same error as him: https://slack-chats.kotlinlang.org/t/18840838/hi-there-iam-able-to-update-kotlin-2-0-0-and-compose-android

image

libs.versions.toml

[versions]
agp = "8.2.0"
compose-plugin = "1.6.11"
kotlin = "2.0.0"
[libraries]
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinxSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

build.gradle

plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.composeCompiler) apply false
    alias(libs.plugins.jetbrainsCompose) apply false
    alias(libs.plugins.kotlinMultiplatform) apply false
    alias(libs.plugins.kotlinxSerialization) apply false

    id("com.google.devtools.ksp") version "2.0.0-1.0.23" apply false
    id("de.jensklingenberg.ktorfit") version "2.0.0" apply false
}

build.gradle

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsCompose)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.kotlinxSerialization)

    id("de.jensklingenberg.ktorfit") version "2.0.0"
    id("com.google.devtools.ksp")
}

kotlin {
    androidTarget{

    }

    sourceSets {
        all {
            languageSettings {
                optIn("org.jetbrains.compose.resources.ExperimentalResourceApi")
            }
        }
        val commonMain by getting {
            dependencies {
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material3)
                implementation(compose.material)
                implementation(compose.ui)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

                implementation("androidx.collection:collection:1.4.0")

                implementation("de.jensklingenberg.ktorfit:ktorfit-lib:2.0.0")
                implementation("de.jensklingenberg.ktorfit:ktorfit-converters-response:2.0.0")
                implementation("de.jensklingenberg.ktorfit:ktorfit-converters-call:2.0.0")

                implementation("io.insert-koin:koin-core:3.5.6")

                implementation("io.ktor:ktor-client-core:2.3.11")
                implementation("io.ktor:ktor-client-serialization:2.3.11")
                implementation("io.ktor:ktor-client-content-negotiation:2.3.11")
                implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.11")

                implementation("io.insert-koin:koin-compose:1.1.5")

            }
        }

        val androidMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation("io.insert-koin:koin-android:3.5.0")
                implementation("io.ktor:ktor-client-android:2.3.6")
            }
        }
    }
}

compose.resources {
    publicResClass = true
    packageOfResClass = "project.shared.resources"
    generateResClass = always
}

android {
    namespace = "project"
    compileSdk = 34

    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    sourceSets["main"].res.srcDirs("src/androidMain/res")
    sourceSets["main"].resources.srcDirs("src/commonMain/resources")

    defaultConfig {
        minSdk = 26
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.14"
    }
    buildFeatures {
        compose = true
    }
}

dependencies {
    ksp("de.jensklingenberg.ktorfit:ktorfit-ksp:2.0.0-1.0.23")
}

What did you expect to happen?

Can compile

Is there anything else we need to know about?

No response

Foso commented 3 months ago

Hi @alekseyHunter , are you using KSP2 (ksp.useKSP2=true) ? If so, Ktorfit is not compatible because of a bug in KSP2

Foso commented 2 months ago

I have an idea what the problem is. You are using the Kotlin multiplatform with only a single target. This might lead to the compile issues because the Kotlin compiler behaves different in that case. With Ktorfit 2.1.0 i fixed a issue related to that

alekseyHunter commented 2 months ago

With Ktorfit 2.1.0 i fixed a issue related to that Hello! It not works ;(

Foso commented 2 months ago

Is there a _UserApiImpl class in your build folder that gets generated? try it with get<Ktorfit>.create<UserApi>()

alekseyHunter commented 2 months ago

Yes: image Perhaps this is an error: image

alekseyHunter commented 2 months ago

java.lang.IllegalStateException: _UserApiProvider not found, did you apply the Ksp Ktorfit plugin? Use .createUserApi() image

alekseyHunter commented 2 months ago
dependencies {
    ksp("de.jensklingenberg.ktorfit:ktorfit-ksp:2.1.0-1.0.23")
}

Execution failed for task ':shared:kspDebugKotlinAndroid'.

Could not resolve all files for configuration ':shared:kspDebugKotlinAndroidProcessorClasspath'. Could not find de.jensklingenberg.ktorfit:ktorfit-ksp:2.1.0-1.0.23. Searched in the following locations:

Execution failed for task ':shared:javaPreCompileDebug'.

Could not resolve all files for configuration ':shared:_agp_internal_javaPreCompileDebug_kspClasspath'. Could not find de.jensklingenberg.ktorfit:ktorfit-ksp:2.1.0-1.0.23. Required by: project :shared

Foso commented 2 months ago

2.1.0 needs minimum KSP 1.0.24. Since Ktorfit 2.0.0 you don't need to add the ktorfit-ksp dependency manually, it will automatically be applied. Please take a look how it's done in the example project https://github.com/Foso/Ktorfit/tree/master/example/MultiplatformExample

alekseyHunter commented 2 months ago

Also doesn't works:

plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.composeCompiler) apply false
    alias(libs.plugins.jetbrainsCompose) apply false
    alias(libs.plugins.kotlinMultiplatform) apply false
    alias(libs.plugins.kotlinxSerialization) apply false

    id("com.google.devtools.ksp") version "2.0.20-1.0.25" apply false
    id("de.jensklingenberg.ktorfit") version "2.1.0" apply false

    id("app.cash.sqldelight") version "2.0.2" apply false
}

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.23.2")
    }
}

image

Foso commented 2 months ago

Try it again, I just released a version for ksp 1.0.25. I'm wondering why you didn't got an Grade error that ktorfit-ksp:2.1.0-1.0.25 could not been found, because it only exist till a few minutes

brsuslcn commented 2 months ago

I encountered the same issue. After clearing the cache and restarting Android Studio, the problem was resolved.

adden00 commented 1 month ago

I had the similar problem with KSP. I added all dependencies and plugins, but _apiImpl didn`t generated and createApi() extension too. I tried to use different versions, searched a lot of docs, but solution was very simple! in shared module build.gradle: ksp plugin "alias(libs.plugins.ksp)" must be before ktorfit plugin "alias(libs.plugins.ktorfit)". And after moving ksp plugin upper, it generated and built successful

sureshmaidaragi1919 commented 1 month ago

thanks @adden00 it works as moved ksp to prior to ktorfit