简体   繁体   中英

Android NDK / General JNI issue: Converting object / jobject to c++ user defiend type

I have a native function call that calls my c++ code and this code creates an instance of my user defined class.

This function has the following signature:

public native Object loadEngine(int arg);

After this call has been made, I would like to call more native functions that would take the "Object" returned from the loadEngine() and modify it, or request data from it.

An example of signature:

public native String loadEngine(Object engine, int queryID);
  1. Is it even possible to do this ?
  2. If it is possible, how would i convert my GameEngine datatype to java Object or jni jobject.
  3. Java would automatically pass this as a reference so that it would never be copied and i would be able to modify it, right ?

The following scenario generates an error saying that it can't convert GameEngine to jobject which is understandable but that's my best shot having very very little experience with Java:

JNIEXPORT jobject JNICALL Java_package_loadEngine
  (JNIEnv *env, jobject obj, jint arg) {
     GameEngine engine(params);
     return (jobject)engine;
  }

JNIEXPORT jstring JNICALL Java_package_queryAction
  (JNIEnv *env, jobject obj, jobject engine, jint arg) {
     String ret = newString(Integer.toString((GameEngine)engine.unimportant()));
     return ret;
  }

Since I'm calling native java code via jni to c++, I unfortunatily can't define the native function as:

 public native GameEngine loadEngine(int arg);

Understandable, yet I can't seem to be able to figure out a workaround.

Thanks, Scarlet.

You could create Java wrapper classes that contain, own, and manage your native code. For instance (off the top of my head, brain-compiled):

class GameEngine {
    private long nativeGameEnginePointer;

    private native long loadEngine();
    public GameEngine() {
        super(...);
        nativeGameEnginePointer = loadEngine();
    }

    private native void destroyEngine(long nativePointer);
    protected void finalize() throws Throwable {
        try {
            destroyEngine(nativeGameEnginePointer);
        } finally {
            super.finalize();
        }
    }

    private native double nativePlayGameOrWhatever(long nativePointer);
    public double playGameOrWhatever() {
        return nativePlayGameOrWhatever(nativeGameEnginePointer);
    }
}

And in your JNI implementation:

class CxxGameEngine;

JNIEXPORT jlong JNICALL GameEngine_loadEngine(JNIEnv *env, jobject obj) {
     return (jlong)(new CxxGameEngine(params));
}

JNIEXPORT jvoid JNICALL GameEngine_destroyEngine(JNIEnv *env, jobject obj, jlong nativePointer) {
     delete (CxxGameEngine *)nativePointer;
}

JNIEXPORT jdouble JNICALL GameEngine_nativePlayGameOrWhatever(JNIEnv *env, jobject obj, jlong nativePointer) {
     return ((CxxGameEngine *)nativePointer)->playGameOrWhatever();
}

Note the use of long to represent a native pointer. This is how the Java runtime does so when needed (eg in the java.nio and java.util.zip packages.) long rather than int ensures that the type is wide enough to hold a pointer even on 64-bit systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM