I'm building an Android app with min SDK 15
, which uses OpenCV library.The problem is when I build apk size of it is more than 60
MB which is not very great.
I checked the app file, I could see that the size is too big due to libopencv_java3.so
file in all architectures like amr64
, armeabi
, mips
, x86
etc.
I am using opneCv only for image processing.This library has a lot of other features like video processing, 3d image, object detection and much more, which I don't need in my app like
If I remove all those files and build APK, then size will be reduced 50 MB, but to make this app work I need to install OpenCVManager
to run my app.
Is there any way by which I can reduce my apk size?
Or is it possible just to extract only the feature from OpenCV
which I am interested in and create .so
for those features?
This is a simplified version of this(1) SO answer. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive. Update build.
OpenCV is a popular open source image processing library that can easily run on Android. Although you will need to use the Android Native Development Kit (NDK) and may need C++, it's quick and easy to get started with OpenCV on Android.
You can not remove the library libopencv_java3.so
, it's mandatory.
Is there any way by which I can reduce my apk size?
Without modify the library libopencv_java3.so
, you can build multiple apks for different architectures rather than package a flat APK. Only one arch of libopencv_java3.so
will be packaged into your split APK. For example, you can use the configuration below to build only for x86
, armeabi-v7a
and mips
architectures in your build.gradle
splits { abi { enable true reset() include "x86", "armeabi-v7a", "mips" universalApk false } }
More information about reduce apk size by splitting, see here.
Or is it possible just to extract only the feature from OpenCV which I am interested in and create .so for those features?
Yes, of course. The building process for opencv
is fully customized. If you have already know how to build opencv for Android, go to step 2 directly.
opencv_build
opencv
you just clonedplatforms/android/build_sdk.py --ndk_path $ANDROID_NDK_ROOT --sdk_path $ANDROID_SDK_ROOT ../opencv_build .
The build procedure need android
command to build apk, so the Android SDK tool version must less than 25.3.0
, from 25.3.0
, android
command has been removed.
Assume the step 1 above run properly. Here I take armeabi-v7a
as an example.
opencv_build/o4a/lib/armeabi-v7a
libopencv_java3.so
also in this directoryCreate a tiny version share library from the module you need, e.g
$ arm-linux-androideabi-gcc -shared -o libopencv_tiny.so --sysroot=$ANDROID_NDK_ROOT/platforms/android-9/arch-arm -Wl,--whole-archive libopencv_core.a libopencv_imgcodecs.a libopencv_imgproc.a -Wl,--no-whole-archive $ du -h libopencv_tiny.so 5.2M libopencv_tiny.so $ arm-linux-androideabi-strip --strip-unneeded libopencv_tiny.so $ du -h libopencv_tiny.so 4.1M libopencv_tiny.so $ du -h libopencv_java3.so 9.8M libopencv_java3.so
The tiny version which include core
, image codecs
and image proc
has a size 4.1M
after strip, but the full version libopencv_java3.so
has a size of 9.8M
.
I use libopencv_tiny.so
as the name just for convenience, you have to use the same name libopencv_java3.so
in your project. Otherwise System.loadLibrary
in Java can not find the native library.
For the rest of the architecture you need, e.g arm64-v8a
, do the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With