There is a problem to write an xml parser for android, using the ready-made class in c ++. The time is ticking, but I don’t understand how to use the class from c ++ from java, so that it stores data. In principle, I can work with native functions. But with the class I do not know how.

While writing the question, I thought that you could call some native Open () returning an identifier from Java, and work through it. Type as with files. And in the library under this identifier will be stored with ++ class.

Hmm, already quite a working idea. But maybe there is a solution more elegantly?

    1 answer 1

    I share like this. In a java-class, I get a field for storing native data:

    import java.nio.ByteBuffer; public class MyJavaClass { static { @SuppressWarnings("unused") // для обхода ошибки с NewDirectByteBuffer ByteBuffer dummy = ByteBuffer.allocate(0); dummy = null; System.loadLibrary("<МойJNIМодуль>"); } private ByteBuffer jnidata; // указывает на нативные данные private pdfPage() {} // запрет прямого вызова, создавать через native - функцию ... 

    In C ++. Initialization of links to access the java-class:

     jclass class_id=(jclass)env->NewGlobalRef(env->FindClass(javaclassname)); jfieldID field_id=env->GetFieldID(class_id, "jnidata", "Ljava/nio/ByteBuffer;"); jmethodID init_id=env->GetMethodID(class_id,"<init>","()V"); 

    NewGlobalRef is needed if you want to keep the class_id between calls in a global variable.

    Creating a java object:

     jobject _this=env->NewObject(class_id, init_id); 

    Associate data with a java-object (data points to data in C ++):

     void set_data(JNIEnv * env, jobject _this, data_t * data) { env->SetObjectField(_this, field_id, ( data? env->NewDirectByteBuffer(data, sizeof *data) : 0 )); } 

    Get a link to your data

     data_t * get_data(JNIEnv * env, jobject _this) { jobject obj=env->GetObjectField(_this, field_id); return obj? (data_t*) env->GetDirectBufferAddress(obj) : 0 ; } 
    • While implemented his version. It turned out just awesome. How to let go try to deal with yours. - mikelsv