Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin Reflection not working on binding projects #9848

Open
rfsouto opened this issue Feb 26, 2025 · 3 comments
Open

Kotlin Reflection not working on binding projects #9848

rfsouto opened this issue Feb 26, 2025 · 3 comments
Assignees
Labels
Area: Bindings Issues in Java Library Binding projects.

Comments

@rfsouto
Copy link

rfsouto commented Feb 26, 2025

Android framework version

net8.0-android

Affected platform version

Jetbrains Rider 2024.3.5

Description

I have an aar native library that uses reflection to resolve json serialization (kotlinx-serialization-json-jvm-1.6.3.jar properly added too) in native the library works properly but when i put in my binding project and test it i receive this error:
Java.Lang.Error: Unresolved class: class java.lang.String (kind = null)

that becomes from the use of "Reflection.typeOf(String.class)" in the native library.

I tried to inject the class on the runtime but does not work, also i tried to get the class by name in my test project but this works correctly creating a Java.Lang.String class correctly:

Java.Lang.Class stringClass = Java.Lang.Class.ForName("java.lang.String");

Steps to Reproduce

  1. When a native library tries do deserialize using reflection
  2. Net Android Binding does not able to resolve the classes

Did you find any workaround?

No response

Relevant log output

Java.Lang.Error: Unresolved class: class java.lang.String (kind = null)
   at Java.Interop.JniEnvironment.InstanceMethods.CallVoidMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 20370
   at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 66
   at Com.Minsait.Veridas_android_sdk.VeridasHelper.StartDocumentCapture(IVeridasListener listener, Context applicationContext, IList`1 docs, IDictionary`2 configuration)
   at TestNet.MainActivity.<>c__DisplayClass8_0.<OnCreate>b__0(Object o, EventArgs e) in /Users/movilidad/Develop/Minsait/Librerias/XXXXXXiosxamarin/BindingXamarinAndroid/TestNet/MainActivity.cs:line 74
   at Android.Views.View.IOnClickListenerImplementor.OnClick(View v) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Views.View.cs:line 2410
   at Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_(IntPtr jnienv, IntPtr native__this, IntPtr native_v) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Views.View.cs:line 2374
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
  --- End of managed Java.Lang.Error stack trace ---
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class java.lang.String (kind = null)
	at kotlin.reflect.jvm.internal.KClassImpl.createSyntheticClassOrFail(KClassImpl.kt:340)
	at kotlin.reflect.jvm.internal.KClassImpl.access$createSyntheticClassOrFail(KClassImpl.kt:49)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:67)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:53)
	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:53)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:193)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:49)
	at kotlin.reflect.full.KClassifiers.createType(KClassifiers.kt:47)
	at kotlin.reflect.jvm.internal.CachesKt$CACHE_FOR_BASE_CLASSIFIERS$1.invoke(caches.kt:37)
	at kotlin.reflect.jvm.internal.CachesKt$CACHE_FOR_BASE_CLASSIFIERS$1.invoke(caches.kt:36)
	at kotlin.reflect.jvm.internal.ConcurrentHashMapCache.get(CacheByClass.kt:92)
	at kotlin.reflect.jvm.internal.CachesKt.getOrCreateKType(caches.kt:55)
	at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.typeOf(ReflectionFactoryImpl.java:123)
	at kotlin.jvm.internal.Reflection.typeOf(Reflection.java:128)
	at com.XXXXXX.resource.database.ResourcesDatabase$parseDocIdsByCountryMap$2.invokeSuspend(ResourcesDatabase.kt:232)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693)

  --- End of managed Java.Lang.Error stack trace ---
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class java.lang.String (kind = null)
	at kotlin.reflect.jvm.internal.KClassImpl.createSyntheticClassOrFail(KClassImpl.kt:340)
	at kotlin.reflect.jvm.internal.KClassImpl.access$createSyntheticClassOrFail(KClassImpl.kt:49)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:67)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:53)
	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:53)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:193)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:49)
	at kotlin.reflect.full.KClassifiers.createType(KClassifiers.kt:47)
	at kotlin.reflect.jvm.internal.CachesKt$CACHE_FOR_BASE_CLASSIFIERS$1.invoke(caches.kt:37)
	at kotlin.reflect.jvm.internal.CachesKt$CACHE_FOR_BASE_CLASSIFIERS$1.invoke(caches.kt:36)
	at kotlin.reflect.jvm.internal.ConcurrentHashMapCache.get(CacheByClass.kt:92)
	at kotlin.reflect.jvm.internal.CachesKt.getOrCreateKType(caches.kt:55)
	at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.typeOf(ReflectionFactoryImpl.java:123)
	at kotlin.jvm.internal.Reflection.typeOf(Reflection.java:128)
	at com.XXXXXX.resource.database.ResourcesDatabase$parseDocIdsByCountryMap$2.invokeSuspend(ResourcesDatabase.kt:232)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693)
@rfsouto rfsouto added the needs-triage Issues that need to be assigned. label Feb 26, 2025
@jpobst
Copy link
Contributor

jpobst commented Feb 26, 2025

It looks like you might need to also add the Kotlin Reflection package for it to work:
https://www.nuget.org/packages/Xamarin.Kotlin.Reflect

https://stackoverflow.com/a/34159517

@jpobst jpobst added Area: Bindings Issues in Java Library Binding projects. and removed needs-triage Issues that need to be assigned. labels Feb 26, 2025
@rfsouto
Copy link
Author

rfsouto commented Feb 26, 2025

Nope, i already had it added:

Image

@jpobst
Copy link
Contributor

jpobst commented Feb 26, 2025

These issues have a little more context around issues we have seen with Kotlin reflection in the past:

Your error message is different, but it might have the same root cause.

Specifically, I would try the workaround here: #8456 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Bindings Issues in Java Library Binding projects.
Projects
None yet
Development

No branches or pull requests

2 participants