java-native-interface

Send MotionEvent from Java to JNI

江枫思渺然 提交于 2021-01-29 02:10:17
问题 Is it possible to send MotionEvent from Java to C++ through JNI? I have a method in C++ that should receive a pointer to AInputEvent to send it to the Game class: JNIEXPORT void JNICALL Java_com_game_ActivityMain_onTouch(JNIEnv *jenv, jclass obj,jobject event) { AInputEvent* inputEvent=(AInputEvent*)event; game->OnInput(inputEvent); } }; in Java I declared the native method as: public static native void onTouch(MotionEvent event); but the application crashes when I tab on the screen. Edit: I

Send MotionEvent from Java to JNI

本小妞迷上赌 提交于 2021-01-29 02:04:58
问题 Is it possible to send MotionEvent from Java to C++ through JNI? I have a method in C++ that should receive a pointer to AInputEvent to send it to the Game class: JNIEXPORT void JNICALL Java_com_game_ActivityMain_onTouch(JNIEnv *jenv, jclass obj,jobject event) { AInputEvent* inputEvent=(AInputEvent*)event; game->OnInput(inputEvent); } }; in Java I declared the native method as: public static native void onTouch(MotionEvent event); but the application crashes when I tab on the screen. Edit: I

Portability of JNI native function registration via RegisterNatives() in C++ on Android

本秂侑毒 提交于 2021-01-28 11:45:56
问题 One of the ways to register native functions in JNI is by using the function RegisterNatives() . An example (although seemingly with some errors) can be found in the Android documentation here. Here's a code example demonstrating this technique: #include <jni.h> void JNICALL test(JNIEnv* const environment, jobject const objectOrClass) { } extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* const vm, void* const reserved) { JNIEnv* environment; vm->GetEnv(reinterpret_cast<void**>(&environment), JNI

Portability of JNI native function registration via RegisterNatives() in C++ on Android

别说谁变了你拦得住时间么 提交于 2021-01-28 11:45:40
问题 One of the ways to register native functions in JNI is by using the function RegisterNatives() . An example (although seemingly with some errors) can be found in the Android documentation here. Here's a code example demonstrating this technique: #include <jni.h> void JNICALL test(JNIEnv* const environment, jobject const objectOrClass) { } extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* const vm, void* const reserved) { JNIEnv* environment; vm->GetEnv(reinterpret_cast<void**>(&environment), JNI

Can't find JNI_OnLoad_libname in example program

可紊 提交于 2021-01-28 06:23:31
问题 How exactly do I call native static libraries from Java? I'm using Java 8. It looks to me like I should be able to define a JNI_OnLoad_library in a C++ program with an embedded JVM but my VM keeps dying when I call System.loadLibrary. The following only prints "Hello from main". main.cpp: #include <jni.h> #include <iostream> extern "C" { JNIEXPORT jint JNI_OnLoad_hello(JavaVM *vm, void *reserved) { std::cout << "Hello World" << std::endl; return JNI_VERSION_1_8; } } int main(int argc, char**

Specify targets in externalNativeBuild of the build.gradle file-> No signature of method

感情迁移 提交于 2021-01-28 05:13:31
问题 Start a new native c++ project in android studio 4.1.1. Go to build.gradle of the module Add a targets line: externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" version "3.10.2" targets "native-lib" // New line } } Somehow I get an error when I click the green play button (Run 'app'): Build file '<project folder>/app/build.gradle' line: 5 A problem occurred evaluating project ':app'. > No signature of method: build_bcdq4hni531na6stswx8a7txx.android() is applicable for argument

Must I DeleteLocalRef an object I have called NewGlobalRef on?

瘦欲@ 提交于 2021-01-28 04:06:28
问题 As we know, creating Java objects in a thread owned by C/C++ we are responsible to call DeleteLocalRef or Push/Pop LocalFrame as needed. I have some code like jbyteArray buffer = env->NewByteArray((jsize)requiredSize); longTermBufferReference = (jbyteArray)env->NewGlobalRef(buffer); where native code creates a byte array, which it re-uses across multiple threads/calls, once we are done with it, I call DeleteGlobalRef on the buffer. The question is, must I call DeleteLocalRef on buffer when I

Is it safe to cast an array to a struct with one array as member?

六眼飞鱼酱① 提交于 2021-01-28 02:03:51
问题 In the following setup typedef struct { unsigned char data[64]; } mystruct; int myfunc(mystruct* arg); // fills arg with data is it safe to call myfunc with a pointer to a 64 byte array? E.g. unsigned char buffer[64]; myfunc((mystruct*) buffer) In my concrete application I am using a JNI direct ByteBuffer which should be filled from myfunc . unsigned char* dbbuffer = (unsigned char*) (*env)->GetDirectBufferAddress(env, jbuffer); If the cast is not safe, I would have to create a mystruct ,

Call static Java method from separate thread using JNI

半世苍凉 提交于 2021-01-27 18:44:28
问题 I'm trying to use JNI in android to make a function pointer that a native library I'm using uses forward it's call to java. When initializeStateController is called, a new thread is made using pthread_create that calls the function pointer whenever the state of the State Controller changes. However, when I try to call GetStaticMethodID from state_exec in C, I'm getting the following error: JNI DETECTED ERROR IN APPLICATION: jclass is an invalid local reference: 0xec500019 (0xdead4321) in call

JNI Environment Pointer

拟墨画扇 提交于 2021-01-27 18:32:09
问题 I have a Java class in which I have a function which must be called from my C code. The function is the following: public void endTrial(){ //Code } So I have created the following code in my C file: JNIEXPORT void JNICALL package_endTrialJava(); JNIEXPORT void JNICALL package_endTrialJava(){ jobject javaObjectRef = env->NewObject(javaClassRef, javaMethodRef); env->CallVoidMethod(javaObjectRef, javaMethodRef); } But to be able to call this function with the env variable, I have created this