How to pass a complex structure between C and Java with JNI on Android NDK

Bibu :

I have a complex strucutre in the C code on my Android application and I would like to use it in the Java side. I've done some research with google and in stackoverflow, so I've created the java class from my C strucutre, but now how to get it in Java.

I've found these informations, about making a pointer in the class and use this on the C side :

Get the field ID : (*env)->GetFieldID(...)
Get the pointer : (*env)->GetLongField(...)
Set the pointer : (*env)->SetLongField(...)

But I don't understand how it really works ...

Above, you can find what I've done until now ... not so much ! On the C side :

ComplexStructure Java_com_main_MainActivity_listenUDP(JNIEnv* env, jclass clazz)
{
    int i,taille;
    ComplexStructure myStruct;    
    taille = -1;    
    taille = recvfrom(socket, &myStruct, sizeof(ComplexStructure ), 0, &rcvAddr, &sizeOfSock);
    if(taille != -1)
    {   
        return myStruct;
    }
    return NULL;
}

And on the Java side :

public void getFromUDP() {

    ComplexClass myClass = new ComplexClass();
    myClass = listenUDP();              
}

@Override
public void run() {
    initUDP();
    getFromUDP();
}


public static native ComplexClass listenUDP();
public static native void initUDP();
public static native void closeUDP();

/** Load jni .so on initialization */
static {
     System.loadLibrary("native-interface");
}

EDIT : I want to add that my structure is very complex like that :

typedef struct{
  TYPE_A myStructA;
  TYPE_B myStructB;
  TYPE_C myStructC;
  TYPE_D myStructD;
}ComplexStructure;

typedef struct{
  float rad;
  int size;
  bool isEmpty;
}TYPE_A;

typedef struct{
  float rad;
  bool isEmpty;
  float color;
  int temp;
}TYPE_B;

typedef struct{
  int temp;
  float rain;
  bool isEmpty;
}TYPE_C;

typedef struct{
  float rad;
  int idPerson;
  bool isOnTime;
}TYPE_D;

Even more complex, just an example to show you how it is !

Jakub Zaverka :

You cannot pass raw C structs into Java and expect it to treat these structs as classes. You need to create a class for your struct. I see you already did that, so the only thing you need to do is to convert this struct into an instance of the class.

The code on the Java side:

public static native ComplexClass listenUDP();

will translate to:

JNIEXPORT jobject JNICALL Java_com_main_MainActivity_listenUDP(JNIEnv *env, jclass);

In that C code, you need to load the ComplexClass using the env->FindClass(); function. Then to create a new instance of that class (it simplifies matters if you have zero-parameter constructor), you need to load a constructor method signature and "invoke" it in the env->NewObject() method. Full code:

jclass complexClass = env->FindClass("/com/main/ComplexClass");
jmethod constructor = env->GetMethodId(complexClass, "<init>", "()com/main/ComplexClass"); //The name of constructor method is "<init>"
jobject instance = env->NewObject(complexClass, constructor);

Then you need to set the fields of this class using env->setXXXField();. If you have more objects as fields and want to alse create them, then repeat the above process for another object.

This looks very complicated, but that's the price for using native C in managed Java code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass byte array from android java class to JNI C NDK?

From Java

Android ndk : Problem for call of Java method from c++ with jni

From Dev

How to pass a structure as an argument to java function or return to java from jni

From Dev

Android ndk pass OpenCV mat to jni containing double values

From Dev

Native/C++/Java Android NDK/JNI - Sharing Native Code Among Activities (MSDN hello-jni Sample Modification)

From Dev

Android NDK how to pass received DatagramPacket as arguments to the C function?

From Java

How to use NDK-compiled JNI library in a normal non-Android Java application?

From Java

How to pass List from Java to JNI C++ as vector by ref?

From Java

How to pass C structs back and forth to Java code in JNI?

From Java

How to pass a JNI C# class into Java or handle this situation?

From Java

How to pass null from C++ to Java via Qt JNI?

From Dev

How to pass a Java Integer array into C using JNI?

From Java

How to manage memory deallocation properly between C++ and Java with JNI?

From Dev

C++ / JNI brace-enclosed initializer Map (Android NDK)

From Java

How does NDK work in Android - What is the order that NDK, JNI etc are used?

From Dev

JNI Java Wrapper : How to pass byte[] argument

From Java

Can I create Bitmaps with the Android NDK in C++ and pass them to Java really fast?

From Java

Passing pointers between C and Java through JNI

From Dev

How to pass a Data from native c binary to android model using JNI?

From Dev

Android NDK C++ files keep file structure

From Dev

How to convert Android code to C in Android NDK

From Dev

How can I pass a Struct from C back to Java via JNI

From Dev

How to pass char array from C JNI function to Java method as byte[]

From Java

How to pass a C++ string to Java JNI in a well defined thread-safe way?

From Dev

Android NDK JNI array reference table overflow

From Dev

Is JNI_OnLoad normally used for Android NDK?

From Dev

Create a C++ class and Use that class In Another C++ Class Which contain JNI methods in ndk android

From Java

How to pass value between kotlin class and java class in android?

From Dev

How to kill a child thread in C (Android NDK)?

Related Related

  1. 1

    How to pass byte array from android java class to JNI C NDK?

  2. 2

    Android ndk : Problem for call of Java method from c++ with jni

  3. 3

    How to pass a structure as an argument to java function or return to java from jni

  4. 4

    Android ndk pass OpenCV mat to jni containing double values

  5. 5

    Native/C++/Java Android NDK/JNI - Sharing Native Code Among Activities (MSDN hello-jni Sample Modification)

  6. 6

    Android NDK how to pass received DatagramPacket as arguments to the C function?

  7. 7

    How to use NDK-compiled JNI library in a normal non-Android Java application?

  8. 8

    How to pass List from Java to JNI C++ as vector by ref?

  9. 9

    How to pass C structs back and forth to Java code in JNI?

  10. 10

    How to pass a JNI C# class into Java or handle this situation?

  11. 11

    How to pass null from C++ to Java via Qt JNI?

  12. 12

    How to pass a Java Integer array into C using JNI?

  13. 13

    How to manage memory deallocation properly between C++ and Java with JNI?

  14. 14

    C++ / JNI brace-enclosed initializer Map (Android NDK)

  15. 15

    How does NDK work in Android - What is the order that NDK, JNI etc are used?

  16. 16

    JNI Java Wrapper : How to pass byte[] argument

  17. 17

    Can I create Bitmaps with the Android NDK in C++ and pass them to Java really fast?

  18. 18

    Passing pointers between C and Java through JNI

  19. 19

    How to pass a Data from native c binary to android model using JNI?

  20. 20

    Android NDK C++ files keep file structure

  21. 21

    How to convert Android code to C in Android NDK

  22. 22

    How can I pass a Struct from C back to Java via JNI

  23. 23

    How to pass char array from C JNI function to Java method as byte[]

  24. 24

    How to pass a C++ string to Java JNI in a well defined thread-safe way?

  25. 25

    Android NDK JNI array reference table overflow

  26. 26

    Is JNI_OnLoad normally used for Android NDK?

  27. 27

    Create a C++ class and Use that class In Another C++ Class Which contain JNI methods in ndk android

  28. 28

    How to pass value between kotlin class and java class in android?

  29. 29

    How to kill a child thread in C (Android NDK)?

HotTag

Archive