Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include prebuilt static library

I created a static C++ library for testing. It only defines a class, MyLibrary, and the constructor MyLibrary::MyLibrary(). I built it in qtcreator, and got a libMyLibrary.a file, which is a prebuilt static library.

I would like to use this library in an Android project (using the NDK). In a working NDK test project, I therefore added a folder called inc at the same level as jni, in which I put libMyLibrary.a and its corresponding header mylibrary.h.

My Android.mk is as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := MyLibrary
LOCAL_SRC_FILES := ../inc/libMyLibrary.a

include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp

LOCAL_C_INCLUDES += inc
LOCAL_STATIC_LIBRARIES := MyLibrary

LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)

The ndk-build command compiles without any error. But my static library is apparently not linked (i.e. it does not appear in obj/local/armeabi/objs).

I have tried to include it in my mainActivity.cpp file, but even though the header (mylibrary.h) is found, the library is not and I cannot create an object as in:

MyLibrary test = MyLibrary();

What am I doing wrong? I have read tens of similar questions on StackOverflow, but I still don't get it.

like image 859
JonasVautherin Avatar asked Sep 24 '13 13:09

JonasVautherin


People also ask

How does JNI work on Android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is NDK build?

To compile and debug native code for your app, you need the following components: The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android. CMake: an external build tool that works alongside Gradle to build your native library.


1 Answers

try to use this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
LOCAL_LDLIBS    := -llog -L$(LOCAL_PATH)/inc/ -lMyLibrary
include $(BUILD_SHARED_LIBRARY)

move libMyLibrary.a & mylibrary.h to jni/inc/libMyLibrary.a

like image 73
yushulx Avatar answered Oct 11 '22 17:10

yushulx