Skip to content

JNI_OnLoad-like callback to load APK classes with env.FindClass()? #169

@astonbitecode

Description

@astonbitecode

Hi,

I am having classloading issues when using android-activity and try to load classes of code that is being shipped within the same apk.
The system classes that are available by default (eg. java.lang.String) can be found and loaded with no issues. However, the custom classes cannot be found...

I wonder if the reason is what is described here:

You can get into trouble if you create a thread yourself (perhaps by calling pthread_create and then attaching it with AttachCurrentThread). Now there are no stack frames from your application. If you call FindClass from this thread, the JavaVM will start in the "system" class loader instead of the one associated with your application, so attempts to find app-specific classes will fail.

I see in android-activity README that indeed the rust applications do not run on application's main thread:

run an android_main() function in a separate thread from the Java main thread and marshal events (such as lifecycle events and input events) between Java and your native thread.

Can it be the reason?

I also tried to use the JNI_OnLoad in order to cache the JavaVM and use it later, as it is proposed, but it does not gets called either.

So, my question is, does android-activity support using jni to call classes shipped within the same apk? Do you have any pointers on how to achieve that?

I have this gitjub repo, where you can find my scenario if this can help.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions