Question

I am trying to get Qt 5.2.1 on Android C++ to Java interop working.

I have a piece of C++ code that uses QAndroidJniObject to invoke a static method on a Java class.

This works (C++ first, then Java):

QAndroidJniObject::callStaticObjectMethod(
    "org/example/AndroidTest",
    "doSomething",
    "()Ljava/lang/String;"
);

public static String doSomething() {
    return "Done something.";
}

However, I have a method that has a void return type, so:

QAndroidJniObject::callStaticObjectMethod(
    "org/example/AndroidTest",
    "doSomething",
    "()V"
);

public static void doSomething() {
}

The method signature is ()V and I understood this to mean a method that takes no parameters and has a void return type - so it should match my Java method.

The problem is that on execution this causes a SIGSEGV in libc when the callStaticObjectMethod invocation returns.

If I add debug trace I see the Java class enter and exit correctly, the SIGSEGV occurs on return to the C++ code. In fact, if I change the method signature to return an int, e.g. ()I, I still see a SIGSEGV so I am pretty sure the problem relates to the return type but I can not understand why it fails.

Was it helpful?

Solution

If you're calling Java methods that returns void or one of the primitive types, you should use callStaticMethod instead. Additionally, if the Java method doesn't take any arguments the signature can be omitted, e.g.:

QAndroidJniObject::callStaticMethod<void>("org/example/AndroidTest", "doSomething");

public static void doSomething()
{
}

jint value = QAndroidJniObject::callStaticMethod<jint>("org/example/AndroidTest", "doSomethingElse");

public static int doSomethingElse()
{
    return 1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top