1

I'm working on an Android application that is currently written fully in C++, implemented as a Native Activity and using CMake to build the project.

Currently, I'm looking at trying to call methods in a Java derived WebView class from C++, but am having difficulties with how to use build/use this class with the Native Activity.

My build.gradle is located in the root of the project, and all of the C++ code is in src/. Simply adding the Java file to the project, under src/main/java/com/my/app/CustomWebView.java.

and calling:

JNIEnv * env = 0; 
JNIEnv ** envptr = &env; 
JavaVM * jniiptr = gapp->activity->vm; 
jniiptr->AttachCurrentThread( (JNIEnv**)&env, 0 ); 
env = (*envptr);
jclass customWebViewClass = env->FindClass("com/my/app/CustomWebView");

Results in customWebViewClass being null.

I am unsure on what I need to do for this to be found correctly. I haven't yet modified any build or manifest files (such as build.gradle or AndroidManifest.xml), and was wondering if I needed to for this java class to be found correctly?

dan123123
  • 67
  • 1
  • 10
  • 1
    JNIEnv * env = 0; JNIEnv ** envptr = &env; So you're making a null pointer, then a pointer to a null pointer? That's not right, and I'm shocked it didn't crash. – Gabe Sechan May 17 '23 at 22:44
  • doing that is fine (it is taking the address of the variable `env` which is not what is null...) where I am having difficulty is elsewhere in the code sample. Might you have insights into that? That code to get `env` is used elsewhere in the codebase and functions ok. – dan123123 May 17 '23 at 23:16
  • Unrelated but assigning 0 to a pointer is a bad practice. Use `nullptr` if your compiler supports it or at least `NULL`. – aled May 17 '23 at 23:19
  • Reminder: Java has garbage collection. There is no guarantee when or if the garbage collector runs. This is usually bad for embedded systems. – Thomas Matthews May 18 '23 at 00:01
  • You should check the result of every JNI operation. And why assign (*envptr) to env? Makes no sense – aled May 18 '23 at 03:30

0 Answers0