Skip to content

JavaCpp

JavaCPP provides efficient access to native C++ inside Java, not unlike the way some C/C++ compilers interact with assembly language. No need to invent new languages such as C++/CLI or Cython. Under the hood, it uses JNI, so it works with all Java implementations, including Android. In contrast to other approaches (SWIG, CableSwig, JNIGeneratorApp, cxxwrap, JNIWrapper, Platform Invoke, GlueGen, JNIDirect, JNA, JNIEasy, JniMarshall, JNative, J/Invoke, HawtJNI, BridJ, etc.), it supports naturally and efficiently many features of the C++ language often considered problematic, including overloaded operators, template classes and functions, member function pointers, callback functions, functors, nested struct definitions, variable length arguments, nested namespaces, large data structures containing arbitrary cycles, multiple inheritance, passing/returning by value/reference/vector, anonymous unions, bit fields, exceptions, destructors and garbage collection. Obviously, neatly supporting the whole of C++ would require more work (although one could argue about the intrinsic neatness of C++), but I am releasing it here as a proof of concept. I have already used it to produce complete interfaces to OpenCV, FFmpeg, libdc1394, PGR FlyCapture, OpenKinect, videoInput, and ARToolKitPlus as part of JavaCV.

The new JavaCPP Presets subproject also demonstrates early parsing capabilities of C/C++ header files that already show promising and useful results with at least FFmpeg, libdc1394, OpenKinect, videoInput, and ARToolKitPlus.

Using Android NDK

JavaCPP already supports Android NDK with command option 'properties'. But some people may want to use more Android-way. This document explains how to use javacpp for cpp-generation and android-ndk for compilation with/without maven.

Configuration

  • Android NDK r8d or above. (http://developer.android.com/tools/sdk/ndk/index.html) r8c is a bit buggy.
  • JavaCPP 0.4 or above.
  • Proper javacpp annotation in java source.
  • Use @Platform(library="XXX") to decide cpp&so name which will be used in Android.mk
@Platform(library = "jniHello", include = { "hello.h"})
  • jni/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := jniHello
LOCAL_SRC_FILES := jniHello.cpp
LOCAL_C_INCLUDES := hello.h
LOCAL_LDLIBS := -llog
LOCAL_CPP_FEATURES := exceptions
  • jni/Application.mk
APP_ABI := armeabi armeabi-v7a x86 # or simply 'all' then it will generate armeabi, armeabi-v7a, x86, mips
APP_STL := gnustl_static

See also

Favorite site