Home » Android » c++ – Android: How to link my own static libraries correctly?

c++ – Android: How to link my own static libraries correctly?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an Android project written in C++ and have a problem in linking phase. The code is put in some static libraries which should be linked together.

I have found a lot of questions and answers on the net about this topic and most of them suggest to put my libraries to LOCAL_STATIC_LIBRARIES in the Android.mk file. But, if I do this, I found the content of LOCAL_STATIC_LIBRARIES is simply ignored: my libraries are not linked, and adding any dummy text here does not generate any error or warning message.

I tried it this way:

LOCAL_STATIC_LIBRARIES := MyLib.a

or with full path:

LOCAL_STATIC_LIBRARIES := $(LOCAL_PATH)/MyLib.a

none of them worked.

If I put my static libraries to LOCAL_LDLIBS then it is linked, but I got a warning message about non-system libraries are used, and probably the build will be wrong.

The content of my Android.mk file is:

LOCAL_LDLIBS := $(LOCAL_PATH)/MyLib.a ...

and I got this message:

Android NDK: WARNING:jni/Android.mk:myapp: non-system libraries in linker flags: jni/MyLib.a    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    

I could not find how to use LOCAL_STATIC_LIBRARIES right way, please help me!

I have android-ndk-r9 and android-sdk_r22.2.1 on a OpenSuSE x86 and using target=android-18

How to&Answers:

See JBL’s answer here.

The LOCAL_STATIC_LIBRARIES variable does not work that way. First you need a section that defines the library you want to include:

include $(CLEAR_VARS)
LOCAL_PATH = .
LOCAL_MODULE := curl
LOCAL_EXPORT_C_INCLUDES := ../curl/include
LOCAL_SRC_FILES := ../curl/lib/libcurl.a
include $(PREBUILT_STATIC_LIBRARY)

THEN, you can include it using

include $(CLEAR_VARS)
LOCAL_MODULE = mylib
CFLAGS = ...
...
LOCAL_STATIC_LIBRARIES = curl
include $(BUILD_STATIC_LIBRARY)

Answer:

Most probably the problem lies in that you are giving the extension of the library:

LOCAL_STATIC_LIBRARIES := MyLib.a

I think, it should be written as:

LOCAL_STATIC_LIBRARIES := MyLib