Search code examples
androidc++boostjava-native-interface

Include Boost C++ library in android


I have been trying to marry Boost and android on windows for long time and tried lot of approaches but still no luck. I want to make a sample program using Boost library in android. I am following this tutorial here.

As this tutorial suggested i have kept my Boost lib in ****(Android NDK)\sources\boost_1_44_0**** compiled it successfully.

Then i made an Android.mk file inside sources/boost_1_44_0 and made the entry of each library which i want to use. In this case lib. file is libboost_date_time-gcc-mt-s-1_44.a available in boost_1_44_0/android/lib/
Here is the content of Android.mk file.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_date
LOCAL_SRC_FILES:= boost_1_44_0/android/lib/libboost_date_time-gcc-mt-s-1_44.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY) 

Now the next step is to make an Android.mk file in my project directory, inside jni folder.(this is to create a shared library.). Here are its contents.

LOCAL_PATH := $(call my-dir)
 include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.cpp
LOCAL_STATIC_LIBRARIES := boost_date
include $(BUILD_SHARED_LIBRARY)
$(call import-module,boost_1_44_0) 

Here is the Application.mk file placed on the same location, inside jni folder. Contents of Application.mk file are as follows:

APP_STL      = gnustl_static #(or APP_STL = stlport_static as required)
APP_CPPFLAGS = -fexceptions  

And finally here is my ndkfoo.cpp file

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <boost/date_time.hpp>

using namespace boost::gregorian;

void Java_com_ndkfoo_NdkfooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    date weekstart(2002,Feb,1);

}

this program might be incorrect but the problem is that it does not recognize any boost headers or function. and i always get compilation error.

Is there something i am missing or doing incorrectly? Any help would be really appreciated.

EDIT: This question contains everything you would need to include Boost library in android. For more tips look at my answer below. Hopefully this would also work for you.

Thanks.


Solution

  • My question contained almost complete steps for including BOOST library in Android. But still there are some important points you should remember while working with this.

    • Delete auto generated obj and libs folder Each time before you compile your native code.

    • If you are going to write your native code in C++, add LOCAL_CPP_EXTENSION := .cpp to your Android.mk(jni/Android.mk) file.

    • if you are going to code in C++, put your all cpp code inside extern "C" {}.

      extern C { /*cpp code*/ }