Coder Social home page Coder Social logo

IDE not indexing generated source about ksp HOT 18 CLOSED

google avatar google commented on May 17, 2024 27
IDE not indexing generated source

from ksp.

Comments (18)

ting-yuan avatar ting-yuan commented on May 17, 2024 28

Fixed in e81d01f and will be available in 1.0.9. There should no longer workarounds be needed.

from ksp.

ZacSweers avatar ZacSweers commented on May 17, 2024 16

For now, I've manually supported this like so

sourceSets {
  main {
    java {
      srcDir(file("build/generated/ksp/src/main"))
    }
  }
  test {
    java {
      srcDir(file("build/generated/ksp/src/test"))
    }
  }
}

from ksp.

naingaungluu-codigo avatar naingaungluu-codigo commented on May 17, 2024 12

For android folks who want to set kotlin sourceSets, we can configure it this way

android.applicationVariants.all { variant ->
    kotlin.sourceSets {
        def name = variant.name
        getByName(name) {
            kotlin.srcDir("build/generated/ksp/$name/kotlin")
        }
    }
}

from ksp.

keyboardr avatar keyboardr commented on May 17, 2024 11

As a slight modification to the above, here's an approach that's a little more compact and flexible (for Android projects):

    applicationVariants.all {
        val variantName = name
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
            }
        }
    }

from ksp.

leinardi avatar leinardi commented on May 17, 2024 10

After upgrading to Android Studio Electric Eel the following solution didn't work anymore:

android {
    libraryVariants.all {
        kotlin.sourceSets {
            getByName(name) {
                kotlin.srcDir("build/generated/ksp/${name}/kotlin")
            }
        }
    }
}

But this one seems to work fine:

android {
    kotlin {
        sourceSets {
            debug {
                kotlin.srcDir("build/generated/ksp/debug/kotlin")
            }
            release {
                kotlin.srcDir("build/generated/ksp/release/kotlin")
            }
        }
    }
}

from ksp.

phucynwa avatar phucynwa commented on May 17, 2024 7

For android folks who want to set kotlin sourceSets, we can configure it this way

android.applicationVariants.all { variant ->
    kotlin.sourceSets {
        def name = variant.name
        getByName(name) {
            kotlin.srcDir("build/generated/ksp/$name/kotlin")
        }
    }
}

For build.gradle.kts

    android.applicationVariants.all {
        kotlin {
            sourceSets {
                getByName(name) {
                    kotlin.srcDir("build/generated/ksp/$name/kotlin")
                }
            }
        }
    }

from ksp.

eugene-krivobokov avatar eugene-krivobokov commented on May 17, 2024 6

I'd like to warn about issues in previously mentioned options.

    applicationVariants.all {
        val variantName = name
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
            }
        }
    }

sourceSets is not variant aware.
This code adds generated code from all build variants to the main source set.
E.g. generated code for "debug" and "release" build variants will be in "main" source set.
Print out source sets to see the final state.
It goes against the idea of build variants. This is probably not what you want.

configure<BaseExtension> {
    buildVariants.configureEach {
        sourceSets {
            named(name).configure {
                java.srcDir("build/generated/ksp/$name/kotlin")
            }
        }
    }
}

This alternative has another issue.
It adds generated code as input and breaks build cache.
I'll show this on example.

First clean build:

./gradlew :module:clean  :module:kspReleaseKotlin -Dorg.gradle.caching.debug=true

Appending input file fingerprints for 'source' to build cache key: xxx - RELATIVE_PATH{/project/module/src/main/java/.../Clazz.kt' / ...}

There were only sources as input for this task.
Now, lets run again:

./gradlew  :module:kspReleaseKotlin -Dorg.gradle.caching.debug=true

Appending input file fingerprints for 'source' to build cache key: yyy - RELATIVE_PATH{/project/module/build/generated/ksp/release/kotlin/<generated>, /project/module/src/main/java/.../Clazz.kt' / ...}

Here we see generated files as an addition to original sources.
It causes a cache miss. The output is not "stable".

from ksp.

Raven-L avatar Raven-L commented on May 17, 2024 4
    @Suppress("UnstableApiUsage")
    sourceSets.configureEach {
        kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
    }

it works well on Android Studio.
it come from

from ksp.

gmazzo avatar gmazzo commented on May 17, 2024 2

We use this logic in our tooling:

        plugins.withId("com.google.devtools.ksp") {
            the<BaseExtension>().variants.configureEach {
                addJavaSourceFoldersToModel(layout.buildDirectory
                    .dir("generated/ksp/$name/kotlin").get().asFile)
            }
        }

having:

val BaseExtension.variants: DomainObjectSet<out BaseVariant>
    get() = when (this) {
        is AppExtension -> applicationVariants
        is LibraryExtension -> libraryVariants
        is TestExtension -> applicationVariants
        else -> error("unsupported module type: $this")
    }

from ksp.

apkelly avatar apkelly commented on May 17, 2024 2

This worked for us in an android library module.

androidComponents.onVariants {variant ->
    kotlin.sourceSets.findByName(variant.name)?.kotlin?.srcDirs(
        file("$buildDir/generated/ksp/${variant.name}/kotlin")
    )
}

from ksp.

SeongUgJung avatar SeongUgJung commented on May 17, 2024 1
buildTypes {
    getByName("debug") {
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/debug/kotlin"))
            }
        }
    }
    getByName("release") {
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/release/kotlin"))
            }
        }

    }
}

I am using above script in build.gradle.kts and working as well for now.

from ksp.

eugene-krivobokov avatar eugene-krivobokov commented on May 17, 2024 1

Unfortunately, I haven't found it yet.

from ksp.

Jacob3075 avatar Jacob3075 commented on May 17, 2024 1

None of the above solutions work on Android Studio Flamingo | 2022.2.1 Canary 9
kotlin version: 1.7.20
ksp: 1.7.20-1.0.8
AGP: 8.0.0-alpha09
Gradle: 7.6

The solution that leinardi gave was working around canary 4 but I don't remember in which version it stopped.

from ksp.

ting-yuan avatar ting-yuan commented on May 17, 2024

The IDE extension, where KaptProjectResolverExtension resides, is bundled with the IntelliJ Kotlin plugin. Therefore that approach doesn't work for now.

from ksp.

ting-yuan avatar ting-yuan commented on May 17, 2024

Moving this to Q4 as it depends on upstreaming, which won't happen in Q3.

from ksp.

ting-yuan avatar ting-yuan commented on May 17, 2024

Deferring again to 2021Q1.

from ksp.

lukaszkalnik avatar lukaszkalnik commented on May 17, 2024

@eugene-krivobokov can you suggest correct solution for Android when using build variants? Thank you 🙏

from ksp.

eygraber avatar eygraber commented on May 17, 2024

@ting-yuan using 1.0.11 it looks like the following is still required sorry posted on the wrong issue

from ksp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.