ksp: Bug: ClassNotFoundException, but it does exist in the project

Hello there, I am trying to read my annotation using

classDeclaration.getAnnotationsByType(ActivityReference::class).first()

and getting this error

Caused by: com.google.devtools.ksp.KSTypeNotPresentException: java.lang.ClassNotFoundException: com.veepee.vpcore.route.BarImpl

This is a bit odd since the full qualified name in the exception seems to correct.

I tried this, but I still got the same error.

A project to reproduce it is attached here

To my specific needs I don’t need to instantiate the annotation, but to get the String with the qualified name for ActivityReference.activityName, would it be possible with KSP ?

About this issue

Most upvoted comments

This is a deprecated answer and a better way for this issue: https://github.com/google/ksp/issues/1038#issuecomment-1958577350

Me too, but I found that I could get the corresponding class qualifiedName from the Exception, howerve It’s not a perfect solution;

import kotlin.reflect.KClass

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
annotation class AutoService(val target: KClass<*> = Unit::class)
val spi: AutoService = element.findAnnotationWithType()
  

val targetInterfaceClassName = try {
    spi.target.qualifiedName.toString()
} catch (e: Exception) {
    /**
     * Bug: ksp: com.google.devtools.ksp.KSTypeNotPresentException: java.lang.ClassNotFoundException: cn.jailedbird.arouter.ksp.test.TestInterface
     * Official document: https://github.com/google/ksp/issues?q=ClassNotFoundException++KClass%3C*%3E
     * temporary fix method as follows, but it is not perfect!!!
     * TODO completely fix it!
     * */
    ((e as? KSTypeNotPresentException)?.cause as? ClassNotFoundException)?.message.toString()
}

KSTypeNotPresentException is thrown from here

@KspExperimental
private fun KSType.asClass(proxyClass: Class<*>) = try {
    Class.forName(this.declaration.qualifiedName!!.asString(), true, proxyClass.classLoader)
} catch (e: Exception) {
    throw KSTypeNotPresentException(this, e)
}

Me too, I just need to class name, I think this is super odd, because the ClassNotFoundException message that we receive contains the class name.

Is there any updates for this issue? I’m having the same problem even for Any::class, when trying to get KClass from annotation property, it throws exception. My KSP version is: 1.9.21-1.0.15

Perhaps you can try this method:#1038 (comment)

@JailedBird Great explanation, thank you! At least this work around helped me!

personally, i don’t need to access the object, I just need to be able yoink the fully qualified name, but the processor errors out when trying to access any information of the class