3

I have read other questions similar to this on stack overflow but they are not having same like scenario.

I have FreeImage.a(23 MB file ) file which precompild static library for android. I also have source code of FreeImage Project which have header files.

I want to build .SO file from (.a) file I have with my JNI code(FreeImageCompilation.cpp) Below code compiles fine but it does produces SO File of (5KB only ) whre (*.a file is 23 MB )?

can somebody check if my code below for using *.a file is correct or not ?

In My Android.mk I have following code.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := FreeImage
LOCAL_SRC_FILES := libFreeImage.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/FreeImage/Source/
include $(PREBUILT_STATIC_LIBRARY)

#My Own SO file

LOCAL_STATIC_LIBRARIES := FreeImage
include $(CLEAR_VARS)
LOCAL_MODULE    := FreeImageSo
LOCAL_SRC_FILES := FreeImageCompilation.cpp
LOCAL_STATIC_LIBRARIES := FreeImage
include $(BUILD_SHARED_LIBRARY)
4

2 回答 2

3

To answer the question from the title, here is how you can create shared library from a static library:

# static library 1
include $(CLEAR_VARS)
LOCAL_MODULE := lib1
LOCAL_SRC_FILES := lib1.cpp
include $(BUILD_STATIC_LIBRARY)

# static library 2
include $(CLEAR_VARS)
LOCAL_MODULE := lib2
LOCAL_SRC_FILES := lib2.cpp
include $(BUILD_STATIC_LIBRARY)

# this shared library will have all symbols from two above libraries
include $(CLEAR_VARS)
LOCAL_MODULE := lib_shared
LOCAL_SRC_FILES := empty.cpp
LOCAL_WHOLE_STATIC_LIBRARIES := lib1 lib2
include $(BUILD_SHARED_LIBRARY)

Important option to note is LOCAL_WHOLE_STATIC_LIBRARIES. If you use regular LOCAL_STATIC_LIBRARIES, because you don’t use any symbols from lib1 or lib2 in lib_shared, they will be stripped at link time. To prevent this from happening LOCAL_WHOLE_STATIC_LIBRARIES adds following options to link line to make sure symbols are not stripped:

-Wl,--whole-archive -llib1 -llib2 -Wl,--no-whole-archive

More info in my blog post here: http://gosuwachu.io/

于 2016-04-13T19:03:40.957 回答
0

It's totally correct but you seem to be confused about concepts. Your static library is not included in your shared library as you seem to expect. Your static library is just linked to the shared library. In the end, your program needs an .so file and an .a to function properly, not just one big .so that holds everything.

于 2013-09-19T09:25:06.337 回答