-
Notifications
You must be signed in to change notification settings - Fork 27
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
JavaKit: Class loader hints for Android #154
Comments
As a test, I cached the class loader in This gets me a few steps further but plenty of things like |
In theory this shouldn't be a problem if we resolve the classes directly within the native method entry point (as in, not JNI_OnLoad but the first API call). Needs more investigation at this end, ISTR perhaps classes were visible in OnLoad but not from application methods. Gah. |
Maybe we could have an optional protocol a Swift class could adopt that returns the preferred class loader as a static variable. That should be fairly easy to prototype at least. |
OK, I think something like this might work, still testing though. |
Swift projections of Java classes can conform to CustomJavaClassLoaderConstructible, to provide a custom class loader to be used when constructing new instances. Fixes: swiftlang#154
Swift projections of Java classes can conform to CustomJavaClassLoaderConstructible, to provide a custom class loader to be used when constructing new instances. Fixes: swiftlang#154
Confirmed this approach works (although still some issues casting with as(), may be related to #157. |
Swift projections of Java classes can conform to CustomJavaClassLoader, to provide a custom class loader to be used when initializing new instances. This is useful for platforms such as Android which modify class and class loader visibility depending on the call stack. Fixes: swiftlang#154
Swift projections of Java classes can conform to CustomJavaClassLoader, to provide a custom class loader to be used when initializing new instances. This is useful for platforms such as Android which modify class and class loader visibility depending on the call stack. Fixes: swiftlang#154
Swift projections of Java classes can conform to CustomJavaClassLoader, to provide a custom class loader to be used when initializing new instances. This is useful for platforms such as Android which modify class and class loader visibility depending on the call stack. Fixes: swiftlang#154
Android JVM class resolution is tricky, as which classes are resolvable depends not only on the JNI environment but also the stack frame.
From https://developer.android.com/training/articles/perf-jni:
Doing
FindClass()
lookups inJNI_OnLoad
isn't really viable unless JavaKit caches classes and we can ’poison’ the cache by resolving any classes we will need there. It'sis not ideal though as it requires a priori knowledge of all the application classes (I suppose, this could be done with a macro). Conversely, passing an instance of the class into functions that need it doesn't make for a great API. Neither of these is particularly ergonomic becausegetJNIClass()
can't be overridden (it's in an extension), so one ends up needing to reimplementdynamicJavaNewObjectInstance()
.Caching a reference to the ClassLoader object might be the best approach, this can be done in
JNI_OnLoad
(as long as you know the name of at least one application class), and then perhaps it could be an additional optional argument to initialisers. OrJNIEnvironment
becomes a class to which class loaders can be attached.Anyway, just flagging this as food for thought.
The text was updated successfully, but these errors were encountered: