Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the apk size of Android app which need openCv lib( only for Image processing)

Tags:

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.

enter image description here

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?

like image 760
Shakeeb Ayaz Avatar asked Jul 07 '17 04:07

Shakeeb Ayaz


People also ask

Can we use OpenCV for Android app?

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.

How use OpenCV image processing in Android?

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.


1 Answers

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.

step 1

  1. get the source of opencv
  2. create directory opencv_build
  3. change to root directory of opencv you just cloned
  4. invoke the build script platforms/android/build_sdk.py --ndk_path $ANDROID_NDK_ROOT --sdk_path $ANDROID_SDK_ROOT ../opencv_build .

Note

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.

step 2

Assume the step 1 above run properly. Here I take armeabi-v7a as an example.

  1. Go to directory opencv_build/o4a/lib/armeabi-v7a
  2. You will find many *.a for different opencv module, the library libopencv_java3.so also in this directory
  3. Create 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.

like image 131
alijandro Avatar answered Oct 11 '22 14:10

alijandro