Simple JNI Example with Eclipse CDT

1. Create a new Java Project and create a new class called and put this code inside:

public class HelloJNI {

    static {
    public native void displayMessage();
    public static void main(String[] args) {
        HelloJNI app = new HelloJNI();

2. Open a console and enter in your HelloJNI Project source directory and type:

javah HelloJNI

This will return a new file called HelloJNI.h

3. Create a new C++ Shared Library Project in Eclipse and put the HelloJNI.h file inside of it.
Now create a new C++ source file called HelloJNI.cpp and put this code inside:

#include <stdio.h>
#include “HelloJNI.h”

JNIEXPORT void JNICALL Java_HelloJNI_displayMessage
  (JNIEnv *env, jobject obj)
    printf(“Hello JNI!\n”);

4. Now in the Project Properties go to MinGW C++ Linker –> Miscellaneous –> Linker flags and paste -Wl,–kill-at

5. Finally go to Run configurations –> Arguments –> VM arguments and paste -Djava.library.path= and then in Working directory select other and put ${workspace_loc:HelloJNIC++/Debug}


Build Assimp for Android NDK

1. Download Assimp-2.0 SDK complete source and extract it wherever you want (mine is ~/Desktop/assimp–2.0.863-sdk)
2. Enter into the extracted directory and type: mkdir BUILD and then cd BUILD
4. Now run make
5. It will return a lot of errors, so here are the needed corrections:

  • “aiDefines.h” starting from line 237:
    #    if defined(__x86_32__) || defined(__i386__)
    #        define ASSIMP_BUILD_X86_32BIT_ARCHITECTURE
    #    elif defined(__x86_64__)
    #        define ASSIMP_BUILD_X86_64BIT_ARCHITECTURE
    #    elif defined(__ppc__)
    #   elif defined(__arm__)
    #    else
    #        error unknown architecture
    #    endif
    #    error unknown compiler
  • “Importer.cpp” –> starting from line 901
            << ” x86″
    #elif defined(ASSIMP_BUILD_X86_64BIT_ARCHITECTURE)
            << ” amd64″
            << ” itanium”
            << ” ppc32″
            << ” arm”
    #    error unknown architecture

    Once we have put this corrections run make again and it should work!
    Finally run make install!

Setting up Android standalone toolchain for CMake

It took me a while to figure out how to make the Android NDK working with CMake generated makefiles.
Recently I’m trying to port my simple test of character animation with Assimp Library on Android NDK so I needed to build an Assimp version for Android ARM CPU.
I could not find any infos on this topic on the net and officially, it seems that Assimp Library only supports iOS devices.
So I’ve experimented quite a bit and finally I come up with a solution!
Before we start, I have to say that I’ve tested my solution on OSX and probably it will work on Linux too but I seriously doubt that it will work on Windows + cygwin. You have been warned! 😀
Here we go!

1. Set up an Android Standalone Toolchain

Extract the downloaded Android NDK archive wherever you want (mine is ~/android-ndk-r8b)
Enter into this directory:

cd ~/android-ndk-r8b/build/tools

Fromt there run this command:

sh –platform=android-8 –ndk-dir=/Users/prometheus/android-ndk-r8b –install-dir=/Users/prometheus/android-toolchain –toolchain=arm-linux-androideabi-4.4.3

Now you should have a new directory called android-toolchain.

2. Download the Anroid CMake configuration file
Inside the directory where you have android-ndk-r8b and android-toolchain directories, run this command:

hg clone android-cmake

Now you should have these three directories: android-ndk-r8b, android-toolchain and android-cmake

3. Setup the needed Environment Variables
Edit your .profile file (if you don’t have it, just create it) as follows:

export PATH=$PATH:$HOME/android-sdk-macosx/tools
export PATH=$PATH:$HOME/android-sdk-macosx/platform-tools
export PATH=$PATH:$HOME/android-toolchain/bin

export ANDROID_NDK_TOOLCHAIN_ROOT=$HOME/android-toolchain
export ANDTOOLCHAIN=$HOME/android-cmake/toolchain/android.toolchain.cmake

4. Test CMake
Go inside this directory:

cd ~/android-cmake/samples/hello-cmake

There run this commands:

mkdir BUILD

It should build inside ~/android-cmake/samples/hello-cmake-test/libs/armeabi-v7a

And that’s it!

As you’ve probably noticed, we have specified which GCC version we need to use by adding the argument –toolchain=arm-linux-androideabi-4.4.3
At the time of this writing, this is mandatory because if you try to use CMake with GCC-4.6 it won’t work!