Calling C functions from Java code
The powerful JNI interface, as referred to before, is able to manage interaction in both directions, from Java to C and from C to Java.
A regular Java class declaring a method with the keyword native
declares that the method behavior is implemented in native code. Like a regular Java method, the JNI native
method is able to receive Java objects or primitive types as arguments and return primitive types and objects.
Let's see how a native
method definition will look like in a Java class:
public class MyNativeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { ... cTv.setText(isPrime(2) ? "true" : "false"); } … private native boolean isPrime(int number ); }
The preceding activity will call the native code to check whether a number is prime or not and print the result on the UI.
Notice that our function receives a primitive as an argument and return a primitive boolean
as a result and does not have any...