08/30/2013

Android NDK: Passing complex data between Java and JNI methods

The NDK (Native Development Kit) is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages…

This post is NOT an introduction to using the NDK, so if this is the first time you are dealing with it, I suggest you take a look at a getting started tutorial. Here are some good ones:

Once you set up your NDK project, it is time to invoke native methods written in C/C++ from your Android Java code. I choose to use the C++ syntax because I like it better, here is our first method that will simply return a string from native code. I illustrate here the difference between C and C++ syntaxes:

The last thing I am adding to my project is the ability to run under an x86 emulator because it is much faster for testing. if you don’t know what I am talking about, take a look here: Fast debugging of Android applications

In order to do that, I simply add a file named “Application.mk” to my jni project folder:

Application.mk:

APP_ABI := all

This will compile the jni library for every platform. You can see the difference when you look at the command line output hen building the jni libraries with ndk-build:

Finally here is the output of my JniTester app running in my x86 emulator. I let you do the math to verify the output from native methods is valid

I’m trying to invoke Java method from native code. I was able to run your attached project and saw desired output.
But when I run the project by keeping only “invokeMemberFuncFromNative” call, I’m getting following error. Can you please help to identify the issue?
java.lang.NoSuchMethodError: no non-static method