2

I have a static Java function in an Activity. This function has int parameter. I want to call this function from JNI. I use below code to call, but I don't know how to pass int parameter:

In Java:

public static void updateTopScoreLeaderboard(int score) {
    Log.i("", "updateTopScoreLeaderboard " + score);
}

In JNI:

if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/myApp/MyActivity", "updateTopScoreLeaderboard", "()V"))
{
    methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
}
methodInfo.env->DeleteLocalRef(methodInfo.classID);

That code work well if Java function has no param. But in this case (has int param), it not work.

1 Answer 1

5

You need to change the method signature to match the int parameter, so it's (I)V instead of ()V, and also add your int parameter to the method invocation:

int myInt = 0;

if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/myApp/MyActivity", "updateTopScoreLeaderboard", "(I)V"))
{
    methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, myInt );
}
methodInfo.env->DeleteLocalRef(methodInfo.classID);
Sign up to request clarification or add additional context in comments.

3 Comments

@huync To find signatures use javap. For example, javap -s -public com/nch/myApp/MyActivity | egrep -A 2 "updateTopScoreLeaderboard". Just as in running java, the code needs to be compiled first and be sure to pass in a classpath if needed.
That is a great answer.
You should call CallStaticVoidMethod, instead of CallStaticObjectMethod, since the Java method returns void, and not an object.

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.