77 ms·
> A little bit of this also has to do to stick it to all those luddites people on the internet who post "that's impossible" or "you're doing it wrong" to Stack
by matheusmoreira 7y ago
> A little bit of this also has to do to stick it to all those luddites people on the internet who post "that's impossible" or "you're doing it wrong" to Stack Overflow questions...
> Requesting permissions in the JNI "oh you have to do that in Java" or other dumb stuff like that.
> If you want a bunch of examples of how to do a ton of things in C on Android that you "need" java for, scroll to the bottom of this file:
> https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver.c https://github.com/cntools/rawdraw/blob/master/CNFGEGLDriver...
> it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land.
I respect this attitude but it's wrong to say Java isn't being used or executed. The truth is Java code is being called from C.
jclass ClassActivity = env->FindClass(envptr, "android/app/Activity");
jmethodID MethodrequestPermissions = env->GetMethodID(envptr, ClassActivity, "requestPermissions", "([Ljava/lang/String;I)V");
env->CallVoidMethod(envptr, activity, MethodrequestPermissions, perm_array, 0);
This code directly interfaces with the Java virtual machine. It obtains a handle to a Java class and a handle to one of its methods. Then it instructs the virtual machine to invoke the method with parameters supplied by code written in C.
I searched the repository for JavaVM and JNI_CreateJavaVM and got no results. So this code is being dynamically loaded by a pre-existing Java virtual machine at runtime.
- 411111111111111 7y ago> I respect this attitude but it's wrong to say Java isn't being used or executed. The truth is Java code is being called from C. He isn't wrong, because he didn't say that. It's admittedly very close to that, but not quiet. You just don't have to use Java to ask for permission. If that ultimately still executes Java code... Well, he didn't say.
- matheusmoreira 7y agoThe author wrote: > it shows how to use the JNI to marshall a ton of stuff to/from the Android API without needing to jump back into Java/Kotlin land The C code "jumps back into Java/Kotlin land" when it calls the CallVoidMethod function.
- foota 7y agoI think the point is that the marshalling is done in C, and calls into Java, but the marshalling itself isn't done in Java.
- ReverseCold 7y agoI think it's pretty clear that OP meant that you can trigger that Java from C, not that you never need to access Java. The part of the Android operating system that handles permissions is written in Java, so obviously to use it you would need to use Java.