1

I have to write some Android platform specific code in Qt and need to use JNI. I have a problem with how to create an array of some object. In this case I want to construct an array of strings from C++.

In the two code snippets below the first one creates a java string and it works as expected. In the second code snippet I want to create a java string array, but I get the debug message: "Java string array not valid" so I assume the signature and/or parameters passed to the "QAndroidJniObject javaStringArray()" function is not correct.

I have been looking at the documentation, but was not able to find or properly understand how to do this.

I assume I have to send in the size of the java string array object I want to construct as well.

Any help is appreciated!

QAndroidJniObject javaString("java/lang/String");
if (!javaString.isValid()) {
    qDebug() << "Java string not valid";
    return false;
}

QAndroidJniObject javaStringArray("[Ljava/lang/String;");
if (!javaStringArray.isValid()) {
    qDebug() << "Java string array not valid";
    return false;
}

1 Answer 1

3

The QAndroidJniObject constructor you are using takes a class name, so I'm afraid passing a string array signature won't work. You'll probably have to get your hands dirty and call JNI NewObjectArray(). Try something like:

QAndroidJniEnvironment env;
jobjectArray stringArray = env->NewObjectArray(5, env->FindClass("java/lang/String"), NULL);
QAndroidJniObject jniArray = QAndroidJniObject::fromLocalRef(stringArray);
// ...

This would create an array of 5 null strings, and transfer the ownership to QAndroidJniObject if you prefer, or else you'll have to take care of calling DeleteLocalRef().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.