Hi all, today I’m going to talk about how to use JNI in android very easily. For the examples, I have used, I am passing data between Java & C.

Before we start let me tell you what is JNI, JNI is Java Native Interface to communicate Java and C/ C++ in your java program. If your search for more, you will find more examples for this in Windows/ Linux programs. But less in Android developments.

I will show you very briefly how to use JNI on Android.

There are few sections on using JNI with Android NDK. First, install Android studio & set up Android NDK as documented on the developer website.



Following are the key areas I’m going to talk about Android JNI.

  1. Java / Native data types
  1. Java / Native signatures
  1. Return data from JNI
  1. Pass data to JNI
  1. Pass java Objects to JNI
  1. Return java Object from JNI
  1. Native Callbacks from JNI

For this example, you can refer to the code I made in this git repo.

1. Java / Native data types

Following are the primitive data types equivalent in java / native codes.
Java Type
Native Type
java.lang.Class
jclass
java.lang.Object
jobject
java.lang.String
jstring
object array
jobjectArray
boolean array
jbooleanArray
char array
jcharArray
short array
jshortArray
int array
jintArray
long array
jlongArray
float array
jfloatArray
double array
jdoubleArray
Java Type
Native Type
Description
Boolean
jboolean
unsigned 8 bits
byte
jbyte
signed 8 bits
char
jchar
unsigned 16 bits
short
jshort
signed 16 bits
int
jint
signed 32 bits
long
jlong
signed 64 bits
float
jfloat
32 bits
double
jdouble
64 bits
void
void
N/A

2.Java / Native signatures

Following are the Java-type signatures used in Android NDK.
Signature
Java Type
Z
boolean
B
byte
C
char
S
short
I
int
J
long
F
float
D
double
L fully-qualified-class;
fully-qualified-class
[ type
type[]
( arg-types ) ret-type
method type
3. Return data from JNI
When we coding with C/ C++, normally we use return methods to return data/ error codes. We can easily use it in JNI as shown in bellow example.
Java Code:

Java Native Interface code:

public native String stringFromJNI();


Native code:



4.Pass data to JNI

We can pass data to C/ C++ native codes as an argument from java. We can do it as the example shown below.

Java Code:


Java Native Interface code:
public native int passingDataToJni(double[] tmpArray, int tmpInt, float tmpFloat);
Native code:



5. Pass java Objects to JNI

In java, we can pass objects as arguments and fill/ process data. We can do those things in C/ C++ as well using JNI. The following example will show how to do it easily.

Java Code:


Java Native Interface code:


  
  
  public native int 
  passObjectToJNI(SampleDataObj sampleDataObj);


Native code:



6. Return java Object from JNI

As we return Object in java functions, we can return Java objects from C/ C++ native codes. For that what we have to do is create a Java object from C/ C++ native side by sending an absolute java object classpath and create a Jobject and fill data & return it. Refer following example for this case.

Java Code:


Native Interface Code:
public native SampleDataObj getObjectFromJNI();
Native Code:




7. Native Callbacks from JNI

in C/C++ we use callbacks for notify events. In java, we use pooling or event publishing methods. we can use this in JNI as well. Following example will show how to implement such kind of thing.

Java Code:
Native Interface Code:
Native Code:




If you have any issues while trying those, don’t forget to put a comment so I can catch up later…


To catch this kind of small tutorials, and things I do for fun, don’t forget to subscribe to my BloggerYouTube & follow me on GitHub.

Do you facing any issues while trying this? Let’s discuss in bellow…


***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

Leave a Reply

Your email address will not be published. Required fields are marked *