Yes, you can do the exact same than what you did in C#.
To create your new C++ Object:
jlong
Java_package_name_new(JNIEnv *, jobject) {
return (long)(new CPP_Object());
}
You can store the return value of this method in a Java ptr
variable, and pass it to all NDK methods that need it:
void
Java_package_name_doSomething(JNIEnv *, jobject, jlong ptr) {
CPP_Object *obj = (CPP_Object *)ptr;
// do whatever you want with the object
}
And finally delete it with something like:
void
Java_package_name_delete(JNIEnv *, jobject, jlong ptr) {
delete (CPP_Object *)(ptr);
}
Instead of passing ptr
to all methods that need it, you can also get it and set it directly from the NDK part using the SetLongField
and GetLongField
methods: this allows the Java ptr
variable to be managed only from the NDK part of the code, which I find safer and easier to manage.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…