[SOLVED] JNI error when requesting permission from library on Android

I’m working on a JUCE-based library, which requires audio input.
When I call RuntimePermissions::request I get a JNI crash, which seems to be because it cannot get a reference to the current Activity:

JNI DETECTED ERROR IN APPLICATION: can't call android.app.FragmentManager android.app.Activity.getFragmentManager() on null object

The line it crashes on is in FragmentOverlay::open:

    LocalRef<jobject> fm (env->CallObjectMethod (getCurrentActivity().get(), AndroidActivity.getFragmentManager));

Following the debugger, it looks like getCurrentActivity() is not returning a nullptr, however that might not be the case since its actually a Java object. I refactored my app so that it runs the function which calls RuntimePermissions::request in the onCreate method of my main Activity, but that didn’t help.

One line of attack might be to pass in the Activity context directly, but that would be mean hacking JUCE code which I would like to avoid, plus it looks like its designed to pass in the current activity with JuceActivityWatcher

Maybe I need to explicitly register the Activity where I am calling RuntimePermissions::request with the JuceActivityWatcher

Nope, the clue was in the JuceActivityWatcher which responds to onActivityStarted and onActivityStopped… I moved the call that invokes RuntimePermissions::request to the onStart of the Activity and it works now. Leaving here for future reference!!

1 Like

What do you mean by the onStart of the Activity? Running into a similar issue