其他分享
首页 > 其他分享> > 如何减小android本机共享库(.so文件)的大小?

如何减小android本机共享库(.so文件)的大小?

作者:互联网

我正在尝试使用ndk-18-beta1和c _static为Android构建opencv(版本3.4.2),因为此ndk不再支持gnustl_static.

我用armeabi-v7a架构创建了我的ndk-config.py文件:

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

使用c _static和clang工具链.

并建立它只是:

../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

与从opencv版本下载的文件相比,输出库大小非常大.

armeabi-v7a文件夹大小为178M.

每个库大小为:

 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

下载的opencv Android版本的armeabi-v7a文件夹只有28M,
每个库大小是:

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

我也尝试使用-Oz标志进行编译,并且大小没有那么大的变化(也许在创建最终的.so时它会有效).

我在这里错过了什么?

解决方法:

有几种方法可以减少二进制大小:

>使用strip命令去除调试符号,例如

<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

>使用编译器选项-Os

请在此处查看opencv官方文档:https://github.com/opencv/opencv/wiki/Compact-build-advice#results

编辑#1

内置的gradle任务yourapp:transformNativeLibsWithStripDebugSymbolForRelease将帮助你调整最终apk的大小.因此,如果您的目标是自Android Studio 2.x以来的最终apk,则不一定需要显式剥离调试符号.

标签:android,opencv,android-ndk,ndk-build
来源: https://codeday.me/bug/20191008/1875423.html