?

Log in

Previous Entry | Next Entry

Use NDK in Android Studio, part 3

This time we'll talk about compiling and linking our C++ code with third-party libraries: .a or .so files. Just in case you forgot, .a files called "staic libraries" in Linux realm and .so files (shared objects) are called shared libraies. The rule of thumb in Android world is that "Only shared libraries will be installed/copied into your application package. Static libraries can be used to generate shared libraries though". And because you can define more that one module in each Android.mk file, its pretty possible to build shared library from the static one. For example:

LOCAL_PATH:= $(call my-dir)
# The purpose of this part of make is to create wrapper around libfastcv.a library
# Build module will be also called 'libfastcv'
LOCAL_MODULE    := libfastcv
LOCAL_SRC_FILES := ../../libs/libfastcv.a
include $(PREBUILT_STATIC_LIBRARY)
....
LOCAL_MODULE    := libfastcvUtils
LOCAL_CFLAGS    := -Werror
LOCAL_C_INCLUDES += $(JNI_DIR)
LOCAL_SRC_FILES := \
FastCVUtil.cpp
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := liblog libGLESv2
LOCAL_STATIC_LIBRARIES := libfastcv
include $(BUILD_SHARED_LIBRARY)

and vice versa: from prebuilt shared library to shared library:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := fastcvUtils
LOCAL_SRC_FILES := $(LIB_PATH)/libfastcvUtils.so
include $(PREBUILT_SHARED_LIBRARY)

LOCAL_MODULE    := libfastcvFeatDetect
LOCAL_CFLAGS    := -Werror
LOCAL_SRC_FILES := Corner.cpp
LOCAL_STATIC_LIBRARIES := libfastcv
LOCAL_SHARED_LIBRARIES := liblog libGLESv2 fastcvUtils
LOCAL_C_INCLUDES += $(JNI_DIR)/fastcv
LOCAL_C_INCLUDES += $(JNI_DIR)
include $(BUILD_SHARED_LIBRARY)