Return custom class from Java to c++

I am implementing Stripe library for Android (https://github.com/stripe/stripe-android) in QT using QAndroidJniObject. I am able to generate token from stripe's Java code, but not able to get how do I pass the token back c++ class. Is it possible to return User defined class's object from Java to c++ or is there any other way of doing this.

1 answer

  • answered 2017-10-11 11:11 mko

    If you want to pass Java object from Java to C, via JNI, the short answer is: yes, it's possible.

    But it requires some effort :(

    You need to access fields using JNI's methods.

    /* Get objarg's class - objarg is the one we pass from Java 
       We need to know what we are dealing with. */
    jclass cls = (*env)->GetObjectClass(env, objarg);
    
    /* As soon as we know the class of object we can look for the
       fields */
    jfieldID fidInt = (*env)->GetFieldID(env, cls, "iVal", "I");
    jint iVal = (*env)->GetIntField(env, objarg, fidInt);
    printf("iVal: %d\n", iVal);
    

    Take a look here, to get the feeling of what effort you have to put into it:

    Accessing fields of object passed as argument