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)
Kirtan
  • 1,782
  • 1
  • 13
  • 35

2 Answers2

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/

Piotr Wach
  • 913
  • 2
  • 9
  • 22
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.

eozgonul
  • 810
  • 6
  • 12
  • that means the compiled SO file is correct and I need to place . *.a file with SO file when packing apk ? – Kirtan Sep 19 '13 at 10:06
  • According to WinGDB site: "A static library is a utility library which can be used as a component to create shared libraries. It is not present in the APK file and not installed onto the device. Java code can't load it at runtime. Static libraries are built to {output dir}/obj/local/{ABI name} folder. This is also an intermediate folder for shared libraries, however the latter are automatically copied to their final destination in {output dir}/libs/{ABI name}. Static libraries remain in the intermediate folder where they are available for the linker to construct shared libraries." – eozgonul Sep 19 '13 at 12:28
  • So you don't have to place .a files into APK. That should be handled during the linking process. – eozgonul Sep 19 '13 at 12:29