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.
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++).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With