I’m trying to test this library in my Android app,...
# koog-agentic-framework
j
I’m trying to test this library in my Android app, which consumes a KMP library. I’ve added the Koog dependency to the KMP project and exposed a simple use case for the Android app to interact with the agent. However, I keep hitting this error:
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class java.lang.String (kind = null)
. I’m using a
SimpleTool
and only doing something very basic. Any ideas on how to resolve this would be much appreciated.
Copy code
val agent = AIAgent(
    promptExecutor = executor,
    llmModel = AnthropicModels.Sonnet_4_5,
    systemPrompt = INITIAL_SYSTEM_PROMPT,
    toolRegistry = ToolRegistry {
        tool(GetTeamsTool())
    },
    temperature = 0.1,
) {
    install(ChatMemory) {
        chatHistoryProvider = historyProvider
    }
}

agent.run(userPrompt)
d
Hello Julian. Would you mind sharing a full stack trace please?
j
Hi Didier, sure. See the stack trace below. I'm using Koog 0.7.3.
Copy code
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class java.lang.String (kind = null)
	at kotlin.reflect.jvm.internal.KClassImpl.createSyntheticClassOrFail(KClassImpl.kt:515)
	at kotlin.reflect.jvm.internal.KClassImpl.access$createSyntheticClassOrFail(KClassImpl.kt:62)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.descriptor_delegate$lambda$0(KClassImpl.kt:100)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.accessor$KClassImpl$Data$lambda1(Unknown Source:0)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$$Lambda$1.invoke(Unknown Source:2)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:70)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getDescriptor(KClassImpl.kt:86)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.kmClass_delegate$lambda$0(KClassImpl.kt:72)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.accessor$KClassImpl$Data$lambda0(Unknown Source:0)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$$Lambda$0.invoke(Unknown Source:4)
	at kotlin.SafePublicationLazyImpl.getValue(LazyJVM.kt:125)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getKmClass(KClassImpl.kt:66)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.typeParameters_delegate$lambda$0(KClassImpl.kt:180)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.accessor$KClassImpl$Data$lambda8(Unknown Source:0)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$$Lambda$8.invoke(Unknown Source:4)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:70)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
 	at kotlin.reflect.jvm.internal.KClassImpl$Data.getTypeParameters(KClassImpl.kt:177)
 	at kotlin.reflect.jvm.internal.KClassImpl.getTypeParameters(KClassImpl.kt:423)
 	at kotlin.reflect.jvm.internal.types.CapturedKTypeKt.allTypeParameters$lambda$1(CapturedKType.kt:105)
 	at kotlin.reflect.jvm.internal.types.CapturedKTypeKt.accessor$CapturedKTypeKt$lambda1(Unknown Source:0)
 	at kotlin.reflect.jvm.internal.types.CapturedKTypeKt$$Lambda$1.invoke(Unknown Source:2)
 	at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:363)
 	at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:351)
 	at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:828)
 	at kotlin.reflect.jvm.internal.types.CapturedKTypeKt.allTypeParameters(CapturedKType.kt:105)
 	at kotlin.reflect.full.KClassifiers.createTypeImpl(KClassifiers.kt:60)
 	at kotlin.reflect.full.KClassifiers.createTypeImpl$default(KClassifiers.kt:50)
 	at kotlin.reflect.full.KClassifiers.createType(KClassifiers.kt:47)
 	at kotlin.reflect.jvm.internal.CachesKt.CACHE_FOR_BASE_CLASSIFIERS$lambda$0(caches.kt:35)
 	at kotlin.reflect.jvm.internal.CachesKt.accessor$CachesKt$lambda2(Unknown Source:0)
 	at kotlin.reflect.jvm.internal.CachesKt$$Lambda$2.invoke(Unknown Source:2)
 	at kotlin.reflect.jvm.internal.ComputableClassValue.computeValue(CacheByClass.kt:49)
 	at kotlin.reflect.jvm.internal.ComputableClassValue.computeValue(CacheByClass.kt:46)
 	at java.lang.ClassValue.getFromHashMap(ClassValue.java:230)
 	at java.lang.ClassValue.getFromBackup(ClassValue.java:212)
 	at java.lang.ClassValue.get(ClassValue.java:116)
 	at kotlin.reflect.jvm.internal.ClassValueCache.get(CacheByClass.kt:63)
 	at kotlin.reflect.jvm.internal.CachesKt.getOrCreateKType(caches.kt:53)
 	at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.typeOf(ReflectionFactoryImpl.java:121)
 	at kotlin.jvm.internal.Reflection.typeOf(Reflection.java:128)
 	at ai.koog.agents.core.agent.AIAgentSimpleStrategiesKt.singleRunWithParallelAbility(AIAgentSimpleStrategies.kt:101)
 	at ai.koog.agents.core.agent.AIAgentSimpleStrategiesKt.singleRunStrategy(AIAgentSimpleStrategies.kt:37)
 	at ai.koog.agents.core.agent.AIAgentSimpleStrategiesKt.singleRunStrategy$default(AIAgentSimpleStrategies.kt:35)
 	at ai.koog.agents.core.agent.AIAgent$Companion.invoke$default(AIAgent.kt:219)
 	at com.workyard.crew.shared.supervisorassistant.CrewBeaconAgent.agent_delegate$lambda$0(CrewBeaconAgent.kt:41)
 	at com.workyard.crew.shared.supervisorassistant.CrewBeaconAgent.$r8$lambda$4VYSwpsD1JZaEDHu7C0AmFmh6JM(Unknown Source:0)
 	at com.workyard.crew.shared.supervisorassistant.CrewBeaconAgent$$ExternalSyntheticLambda3.invoke(D8$$SyntheticClass:0)
 	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:86)
 	at com.workyard.crew.shared.supervisorassistant.CrewBeaconAgent.getAgent(CrewBeaconAgent.kt:28)
 	at com.workyard.crew.shared.supervisorassistant.CrewBeaconAgent.sendMessage(CrewBeaconAgent.kt:55)
 	at com.workyard.crew.shared.supervisorassistant.SendMessageToAssistant.invoke(SendMessageToAssistant.kt:14)
d
So it seems that the error happens when
singleRunWithParallelAbility(...)
calls
strategy(...)
which itself calls
typeToken()
for your input type (
String
here) which relies on Kotlin reflection. I don't know enough of the Android environment to understand why the Kotlin reflection would fail on a
typeOf<String>()
. But to me that smells a
ClassLoader
problem. Maybe @Konstantin, @Andrey Bragin or @Mark Tkachenko will chime in to help you more. Sorry I can't be of more help.
👀 1
k
Didier, did you try kotlinx-schema 0.5.0?
🔜 1
👀 1
j
@Didier Villevalois @Konstantin thanks for pointing me in the right direction. Using Claude Code, I reviewed the
typeToken<T>()
in
serialization-core/TypeToken.kt
and I see it calls
typeOf<T>()
unconditionally. On Android with R8, kotlin-reflect's built-in type mapping for platform types like
java.lang.String
gets stripped, so
KClassImpl.createSyntheticClassOrFail
throws because
java.lang.String
has no
@Metadata
annotation. I had seen a similar pattern in the Ktor library using
typeOf<T>()
, so on a local copy of the Koog library this is the fix I made: I patched
typeToken<T>()
to wrap
typeOf<T>()
in a try-catch, falling back to `KotlinClassToken(T::class)`:
Copy code
public inline fun <reified T> typeToken(): TypeToken {                                                                           
  val kType = try {                                                                                            
    typeOf<T>()                                                                                            
  } catch (_: Throwable) {                                                                                        
    null                                                                                               
  }                                                                                                    
  return if (kType != null) KotlinTypeToken(kType) else KotlinClassToken(T::class)                                                            
}
This is inspired by Ktor's
typeOfOrNull()
pattern (https://github.com/ktorio/ktor/blob/main/ktor-utils/common/src/io/ktor/util/reflect/Type.kt), which they introduced for the same R8 issue (KT-42913, KTOR-7479). Caveat: The fallback to
KotlinClassToken
loses generic type parameter information. It works for my use case (
String
,
@Serializable
data classes) because
findKSerializer
in
SchemaGenerator.jvmCommon.kt
resolves serializers from
KClass
just fine. But there may be edge cases with complex generic types where the
KType
is actually needed. I don't know enough about Kotlin reflection to be confident this is a proper fix.
v
cc @Andrey Bragin
@Julian Munoz thanks for investigating this issue — please feel free to open a PR 🙂
a
Hi, sorry for the delay. Do you have a PR? Also, can it be solved by adding Proguard exclusion rules instead? AFAIK this is a standard way to make reflection work properly on Android, by excluding relevant classes from minification