Create iOS static library from robovm project (BAD_ACCESS in JNI)

I have a large Java code (only calculation functions, no UI) that I want to reuse as a static library in iOS. My approach was to use robovm and follow an unofficial way to create a static library, described in two articles on the robovm forum: 1 Basic method and 2 Updated version

Trying to follow the steps exactly as described, I got stuck, unfortunately, after creating a shared library using a script, linking the library (.a) in Xcode and successfully creating the project.

At runtime, I see that my C ++ bridge code is being called, but JNI is accessing the library with BAD_ACCESS. For example, the following line of alarms:

jclass myJavaClass = jniEnv->FindClass("com/test/robovm/bridge/MyJavaRoboCode"); 

in this method:

 void callSomethingInJava(const char* arg) { // To call into java from your native app, use JNI Env* rvmEnv = rvmGetEnv(); JNIEnv* jniEnv = &(rvmEnv->jni); jclass myJavaClass = jniEnv->FindClass("com/test/robovm/bridge/MyJavaRoboCode"); jmethodID myJavaMethod = jniEnv->GetStaticMethodID(myJavaClass, "callJava", "(Ljava/lang/String;)V"); jstring argAsJavaString = jniEnv->NewStringUTF(arg); jniEnv->CallStaticVoidMethod(myJavaClass, myJavaMethod, argAsJavaString); 

}

The same is true if I try to use rvmXX methods directly instead of JNI and try to access something in my Java classes. It seems that rvmEnv is not fully initialized. (I double checked for package name or typo errors).

It would be great if someone already managed to create a shared static library from the robovm project and could share their experience here or point me in the right direction to solve the problem.

+4
java objective-c jni static-libraries robovm
Jul 29 '14 at 15:59
source share
1 answer

As you mentioned, you probably have not finished initializing robovm.

You will need to create a method, such as initRoboVM (), to slightly mirror the main bc.c. method This will be called by your code if you want to initialize robovm. You will need to go the way to the application, which you can hard-code during testing.

initRoboVM () will require some changes, namely, it should not call your main Java application method, well, at least, what works well, libraries should not do IMO. Also should not be called rvmShutdown.

+3
Aug 01 '14 at 11:24
source share



All Articles