JNI on Android: How callbacks paintings | by way of Clint Paul | Jan, 2022


Clint Paul

https://unsplash.com/footage/QckxruozjRg?utm_source=unsplash&utm_medium=referral&utm_content=creditShareLink

The item used to be at first posted at clintpauldev.com

Hello, I’m hoping everyone seems to be doing nice. I joined Sharechat closing 12 months, and the adventure is tremendous thrilling thus far. I’m ready to be told lots of recent issues each day at paintings. So, sharing a novel matter this is attention-grabbing and difficult on the identical time. This is JNI ( Java Local Interface ).

In easy phrases, what JNI does is that it will lend a hand us to keep in touch between two events. The 2 events are the code written in Kotlin/Java and the local code (C/C++). Generally, interplay with the local code isn’t important. Why? For the reason that framework APIs and Java/Kotlin code can do the process. The wish to succeed in out to local code is needed when coping with bodily software parts equivalent to sensors and contact enter, graphic comparable, or resuing our personal or different C or C++ libraries. I had a identical stumble upon with the JNI. I had to begin a callback to the Kotlin/Java code when an operation is succeeded/failed on the local facet.

Believe that we wish to get a string as a callback from the local facet. Let’s see how we will enforce it.

Kotlin facet code

amusing registerErrorCallback(error: String) {
Log.e(TAG, “error description $error”)
}

As you’ll be able to see, we’re seeking to get an error description as a callback. And this is a String knowledge sort.

CPP code

I believe it’s higher we step by step construct this section and give an explanation for.

An ordinary Cpp elegance title:

JNIEXPORT void JNICALL Java_com_org_moduleA_moduleAsubModule_className_initSomething
(JNIEnv *env)

A small rationalization of JNIEXPORT and JNICALL is that it is helping the RegisterNatives to find the purposes at run time with the proper calling conventions. In the end, the java package deal title can be adopted. Additionally, you’ll be able to see that there’s one parameter known as JNIEnv. It is helping us to get right of entry to Java components.

JNIEXPORT void JNICALL Java_com_org_moduleA_moduleAsubModule_className_initSomething
(JNIEnv *env)
{
jclass cls =
env->FindClass(“com/org/moduleA/moduleAsubModule/className”);
}

First, we wish to to find the category during which the callback manner is outlined on the Kotlin/Java facet. As discussed previous, we use env to get right of entry to a Java component known as FindClass. It’ll load a in the neighborhood outlined elegance should you give you the right kind trail of the category.

JNIEXPORT void JNICALL Java_com_org_moduleA_moduleA_subModule_className_initSomething
(JNIEnv *env)
{
jclass cls =
env->FindClass(“com/org/moduleA/moduleA_subModule/className”);

jmethodID methodid =
env->GetMethodID(cls,"registerErrorCallback",
(Ljava/lang/String;)V");
}

Subsequent, we wish to to find the process identity. Right here we wish to supply a couple of such things as the category title ( Which we received previous ), the title of the callback manner which we outlined at the Kotlin facet, and the signature. You could be puzzled seeing how the signature is outlined right here. Let’s spoil it down.

There are two portions to the signature. One that enclosed within the parenthesis is the process’s arguments. The second one portion following the parenthesis is the go back sort. Should you glance into our callback manner outlined on the Kotlin facet, you’ll be able to see that it has one argument, which is a string sort and no go back sort.

amusing registerErrorCallback(error: String) {
Log.e(TAG, “error description $error”)
}

What’s throughout the parenthesis is a string sort ( manner argument )

(Ljava/lang/String;)

What’s out of doors the parenthesis is a void sort ( manner go back sort )

(Ljava/lang/String;)V");

If you want extra data about manner signature, please test this hyperlink.

JNIEXPORT void JNICALL Java_com_org_moduleA_moduleA_subModule_className_initSomething
(JNIEnv *env)
{
jclass cls =
env->FindClass(“com/org/moduleA/moduleA_subModule/className”);

jmethodID methodid =
env->GetMethodID(cls,"registerErrorCallback",
(Ljava/lang/String;)V");

jstring jstr = env->NewStringUTF("Some callback"); env->CallVoidMethod(cls, methodid, jstr);
}

In the end, you name the CallVoidMethod that invokes an example manner with a void go back sort. We wish to go the category title, manner identity, and the true arguments.

Check it out and notice if the whole thing is operating as anticipated. If in case you have any doubts, please remark right here. Additionally, if somebody is aware of how you can go a Kotlin delegate/lambda as an issue the usage of the JNI, please resolution right here. I’m having doubts about how you can enforce it.

Thanks, all. Have a really perfect day.

Practice me on LinkedIn.



Supply hyperlink

Leave a Reply

Your email address will not be published.

Back To Top