3

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?

shizhen
  • 12,251
  • 9
  • 52
  • 88
Rui Rodrigues
  • 382
  • 5
  • 20

1 Answers1

1

There are a few ways to decrease the binary size:

  • using strip command to strip the debug symbols, e.g.

    <ndk-path>/arm64-v8a/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/aarch64-linux-android/bin/strip -g -S -d --strip-debug libopencv_dnn.a
    
  • using compiler option -Os

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.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • Any way to do the strip command while building with build_sdk.py scrip? I didn't find such option there. – Rui Rodrigues Aug 14 '18 at 12:57
  • If your concern is to minimize the size of your final apk, then actually all those .so will be stripped automatically. – shizhen Aug 15 '18 at 00:31
  • 1
    I mean when you build your apk, these is a step from the build system to slim down all the .so – shizhen Aug 15 '18 at 00:32
  • I was concern about the static libraries size (the libopencv_**.a files) because I wasn't understanding why the size difference. I am using static libraries to have just one final .so. And off course I didn't want to increase the final .so/.apk size. – Rui Rodrigues Aug 16 '18 at 09:47