androidopencvandroid-ndkndk-build

How to decrease the size of android native shared libaries (.so files)?


I am trying to build opencv (version 3.4.2) for Android using ndk-18-beta1 and c++_static since this ndk does not support gnustl_static anymore.

I created my ndk-config.py file with just armeabi-v7a architecture:

ABIs = [
    ABI("2", "armeabi-v7a", "arm-linux-androideabi-clang3.5", cmake_vars=dict(ANDROID_ABI='armeabi-v7a with NEON', ANDROID_STL="c++_static")),
]

to use c++_static and clang toolchain.

And build it just with:

../opencv-3.4.2/platforms/android/build_sdk.py --ndk_path [path_to_ndk-r18-beta1] --sdk_path [path_to_sdk_25] --config my-ndk-config.py  ./build ../opencv-3.4.2

The output libraries sizes are huge when comparing with the ones downloaded from opencv releases.

The armeabi-v7a folder size is 178M.

Each library size is:

 13M    libopencv_calib3d.a
 29M    libopencv_core.a
 48M    libopencv_dnn.a
 10M    libopencv_features2d.a
4.4M    libopencv_flann.a
528K    libopencv_highgui.a
5.6M    libopencv_imgcodecs.a
 25M    libopencv_imgproc.a
7.7M    libopencv_ml.a
4.9M    libopencv_objdetect.a
6.5M    libopencv_photo.a
2.5M    libopencv_shape.a
8.6M    libopencv_stitching.a
1.7M    libopencv_superres.a
2.9M    libopencv_video.a
2.8M    libopencv_videoio.a
4.1M    libopencv_videostab.a

The armeabi-v7a folder for downloaded opencv Android release is only 28M, and each library size is:

1.9M    libopencv_calib3d.a
4.5M    libopencv_core.a
7.6M    libopencv_dnn.a
1.2M    libopencv_features2d.a
1.1M    libopencv_flann.a
 92K    libopencv_highgui.a
796K    libopencv_imgcodecs.a
5.0M    libopencv_imgproc.a
1.4M    libopencv_ml.a
644K    libopencv_objdetect.a
1.2M    libopencv_photo.a
420K    libopencv_shape.a
1.0M    libopencv_stitching.a
260K    libopencv_superres.a
476K    libopencv_video.a
312K    libopencv_videoio.a
580K    libopencv_videostab.a

I also trying to compile with -Oz flag and the size didn't change that much (maybe it will be valid when creating the final .so).

What am I missing here?


Solution

  • There are a few ways to decrease the binary size:

    Please see the opencv official documents here: https://github.com/opencv/opencv/wiki/Compact-build-advice#results


    Edit #1

    The built-in gradle task yourapp:transformNativeLibsWithStripDebugSymbolForRelease will help you size down your final apk. So, you don't necessarily need to strip the debug symbols explicitly if your objective is the final apk since Android Studio 2.x.