2

I want to create android library using c++ stl.

my build tools are visual studio 2015, Visual GDB.

source code is

.cpp

#include <jni.h>
#include "AndroidProject2.h"
#include <vector>
void foo() { std::vector<int> aaa; aaa.push_back(1); }

Android.mk

# Generated by VisualGDB
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := AndroidProject2-shared
LOCAL_SRC_FILES := AndroidProject2.cpp
COMMON_SRC_FILES := $(LOCAL_SRC_FILES)
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := AndroidProject2-static
LOCAL_SRC_FILES :=  $(COMMON_SRC_FILES)
include $(BUILD_STATIC_LIBRARY)

Application.mk

APP_MODULES := AndroidProject2-static AndroidProject2-shared
APP_ABI := all
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION :=4.9

I succeed build, so, created lib files libAndroidProject2-static.a, libAndroidProject2-static.so

than, I try to change APP_STL option in Application.mk

APP_MODULES := AndroidProject2-static AndroidProject2-shared
APP_ABI := all
APP_STL := gnustl_shared
NDK_TOOLCHAIN_VERSION :=4.9

surely, succeed build. so, created lib files libAndroidProject2-shared.a, libAndroidProject2-shared.so

than, I Compare it and previous build outputs.

I Found the difference about .so file. gnustl_static option's .so file is more bigger than gnustl_shared option's it. but .a is same.

why? I used nm, readelf but can't find difference. what is diff???

user3416447
  • 119
  • 1
  • 2
  • 9
  • If you are only creating one shared library for your Android project you can use either. If your Android project consists of multiple shared libraries you will need to use the shared version. – Richard Critten Mar 29 '17 at 09:56

2 Answers2

2

There are two questions here:

Why does using gnustl_static make larger shared libraries than gnustl_shared?

When you use a static library you're including code from that library directly into your shared library, so your library grows. When you use a shared library you load the code that would have been included from the other shared library instead of including it. The size you need to compare is libAndroidProject2-static.so + libgnustl_shared.so, since both must be present at runtime.

Why didn't using gnustl_static make a larger static library?

Static libraries (libAndroidProject2-static.a, in this case) aren't linked; they're just archives of the compiled sources. libgnustl_static.a doesn't get included until you actually link libAndroidProject-static.a into something, at which point you'll also need to link libgnustl_static.a.

You'd see the size difference when you linked libAndroidProject2-static.a (and libgnustl_static.a) into a shared library or exeuctable.

Dan Albert
  • 10,079
  • 2
  • 36
  • 79
0

The difference is that when you use static, the code of std is compiled into resulting .so file, while using shared, resulting so file requires to have std in separate so file

V-master
  • 1,957
  • 15
  • 18