user3788055
user3788055

Reputation:

make native code access java methods and data members

I am working on an android project with native code that is supposed to update a List and some other boolean variables in an object.

consider the following code

some java class in my code looks like:

 class ReturnObject
{
    boolean a, b;
    public List<String[]> listA;

}

public class foo
{
    public native void someFunction(ReturnObject returnObject);
}

and the native code looks like:

JNIEXPORT void JNICALL Java_com_example_androidtest_TestActivity_someFunction
  (JNIEnv * env, jobject jObj, jobject returnObject) {
    std::string f = "foo";
    // missing code here
    // returnObject.add(f) // add like in java
}

How can I set the values of the boolean variables?

and for the the List (knowing my data in the native code are stored as std::string), how can I invoke add(String[] string) method of the List class from native code?

Upvotes: 2

Views: 1266

Answers (3)

alijandro
alijandro

Reputation: 12147

Here is the sample code to set the boolean values, follow the same guideline to call the add method of the list.

#include <stdio.h>
#include <jni.h>
#include "com_example_Foo.h"

JNIEXPORT void JNICALL Java_com_example_Foo_someFunction
(JNIEnv *env, jobject object, jobject returnObject)
{
    jclass clzReturnObject = env->FindClass("com/example/ReturnObject");
    jfieldID fieldA = env->GetFieldID(clzReturnObject, "a", "Z");
    jfieldID fieldB = env->GetFieldID(clzReturnObject, "b", "Z");

    env->SetBooleanField(returnObject, fieldA, true);
    if (env->ExceptionCheck()) {
        fprintf(stderr, "error set boolean for a");
        env->ExceptionDescribe();
    }
    env->SetBooleanField(returnObject, fieldB, false);
    if (env->ExceptionCheck()) {
        fprintf(stderr, "error set boolean for b");
        env->ExceptionDescribe();
    }

    jfieldID fieldListID = env->GetFieldID(clzReturnObject, "list",
            "Ljava/util/List;");
    jobject listObject = env->GetObjectField(returnObject, fieldListID);

    jclass clzList = env->FindClass("java/util/List");
    jmethodID addMethodID = env->GetMethodID(clzList, "add",
            "(Ljava/lang/Object;)Z");

    jclass clzString = env->FindClass("java/lang/String");

    jstring initElement = env->NewStringUTF("0000");

    jobjectArray toAdd = env->NewObjectArray(10, clzString, initElement);

    if (env->ExceptionCheck()) {
        fprintf(stderr, "error create string array");
        env->ExceptionDescribe();
    }

    env->CallBooleanMethod(listObject, addMethodID, toAdd);
    env->DeleteLocalRef(initElement);
}

Following is the java code

public class Foo {

static {
    System.loadLibrary("Foo");
}

public static void main(String[] args) {

    ReturnObject returnObject = new ReturnObject();
    returnObject.list = new ArrayList<>();

    Foo foo = new Foo();
    foo.someFunction(returnObject);
    System.out.println("size is " + returnObject.list.size());

    foo.someFunction(returnObject);
    System.out.println("size is " + returnObject.list.size());

    foo.someFunction(returnObject);
    System.out.println("size is " + returnObject.list.size());

    // we have three element added from JNI
    for (String string : returnObject.list.get(2)) {
        System.out.println(string);
    }
}

public native void someFunction(ReturnObject returnObject);
}

Upvotes: 2

alijandro
alijandro

Reputation: 12147

Following is an example to read the text of EditText from JNI.

void Java_com_example_foo_MainActivity_someFunction(JNIEnv* env, jobject object,
    jstring objString, jobject objEditText) {
    __android_log_print(ANDROID_LOG_INFO, "Foo", "%s\n", "JNI call");

    // get edittext value
    jclass clzEditText = env->GetObjectClass(objEditText);
    jmethodID methodGetText = env->GetMethodID(clzEditText, "getText", "()Landroid/text/Editable;");

    jobject objEditable = env->CallObjectMethod(objEditText, methodGetText);
    jclass clzEditable = env->GetObjectClass(objEditable);
    jmethodID methodToString = env->GetMethodID(clzEditable, "toString", "()Ljava/lang/String;");

    jstring editText = reinterpret_cast<jstring>(env->CallObjectMethod(
        objEditable, methodToString));

    jboolean isCopy = true;
    __android_log_print(ANDROID_LOG_INFO, "Foo", "From java: %s\n", env->GetStringUTFChars(editText, &isCopy));
}

Assume there is a jni declared in your app's MainActivity.

private native void someFunction(String s, EditText editText);

I didn't find a good solution to set String from JNI directly. If you want to manipulate String in JNI, you could pass the result to Java from return value.

private native String Foo();

Upvotes: 0

alijandro
alijandro

Reputation: 12147

Here is an example to call string constructor to create jstring in JNI.

jbyteArray initValue = env->NewByteArray(10);
env->SetByteArrayRegion(initValue, 0, 9, (const signed char*)"ABCDEFG");
jstring enc = env->NewStringUTF("UTF-8");
jclass clzString = env->FindClass("java/lang/String");
jmethodID cMethodID = env->GetMethodID(clzString, "<init>", "([BLjava/lang/String;)V");
jstring result = reinterpret_cast<jstring>(env->NewObject(clzString, cMethodID, initValue, enc));

Upvotes: 0

Related Questions