In my last blog, I described how to set up Android Studio (AS) to work with OpenCV4Android. Initially Android supported ADT plugin in Eclipse. If you are an Eclipse user in Android development, build OpenCV java wrapper for java or simply build OpenCV for C++ API following the respective blogs. You will need standalone SDK for Android in this case. However recently Android announced that their support for ADT plugin in Eclipse has ended and requested all the developments in Eclipse to shift to Android Studio environment.
OpenCV4Android has two forms as follows.
- Using OpenCV’s native interface. This would be a little hectic task for a fresh OpenCV starter. You will have to learn NDK stuffs but promising when it comes to performance optimization. Isn’t it nice to code in C++ and make work on a mobile, just forgetting the interconnections?
- Using Java-API. This is easier than the native interface. You just need to import OpenCV classes and move on. Not to forget, computations are performed at native level and hence their is a cost in comparably high JNI calls. On the other hand, Java has its own advantage and so does this method.
Both the methods need OpenCV4Android distribution either pre-built one or building from the source. OpenCV4Android pre-built libraries has only modules in OpenCV main repository and if you need more than that, you need a pure building from the source. Though you can build java wrapper for OpenCV using this, the ‘opencv-xxx.jar’ file does not contain android module. Even after you include android in the jar file, you will have to manually configure the dependency setup for AS. Therefore it is again advised to go for a building from the source for Android. Let’s start!
- Download OpenCV and unzip (say at <opencv-source>)
- Download OpenCV extra modules and unzip (say at <opencv-contrib-source>)
- Download Android NDK (say at <ndk-dir>)
- Download CMake (to say <cmake-dir>) and MinGW (to say <mingw-dir>)
- Install Android Studio
Go to ‘<opencv-source>/platforms’ and create a folder named ‘android_arm’
Run ‘<cmake-dir>/bin/cmake-gui’ set paths as follows.
Press ‘Add Entry’ button in the cmake-gui, add ‘ANDROID_NDK’ as a cmake ‘path’ variable and provide the value as <ndk-dir> (in my case path is ‘C:\android-ndk-r10e’).
Add ‘ANDROID_NDK_HOST_X64’ too and check that.
Set ‘CMAKE_TOOLCHAIN_FILE’ as ‘<opencv-source>/platforms/android/android.toolchain.cmake’.
Press ‘Configure’. Choose ‘MinGW Makefile’ as the compiler and press ‘Finish’. See whether above configurations are properly set as in the below figures. Keep other settings as it is.
Building From Source
Go to <mingw-dir>/msys/1.0 and run ‘msys’ bash file.
Navigate to ‘<opencv-source>/platforms/android_arm’ path and run ‘mingw32-make’ command. After completing this, run ‘mingw32-make install’.
Now you have successfully built OpenCV4Android in ‘<opencv-source>/platforms/android_arm/install/sdk’
Then, import built modules as follows (detailed step-by-step explanation is given in the previous blog)
- Launch Android Studio and create a new project selecting File -> New -> New Project…
- Go to File -> New -> Import Module… and provide ‘<opencv-source>/platforms/android_arm/install/sdk/java’. Click ‘Next’ and ‘Finish’.
- If there is a Gradle synchronizing error, change the ‘compileSdkVersion’ and others as follows in build.gradle file in openCVLibraryXXX (imported module) folder.
- Then add the imported library as a dependency to the ‘app’ module in File->’Project Structure’.
- Copy libraries of OpenCV from ‘<opencv-source>/platforms/android_arm/install/sdk/native/libs’ into a newly created folder (jniLibs) in side the ‘app\src\main’ folder.
Additionally; in AS; after you import the module you might experience a gradle build error as follows.
unspecified on project app resolves to an APK archive which is not supported as a compilation dependency.
Therefore modify lines in build.gradle file of imported module as follows.
- Replace ‘apply plugin: com.android.application’ as, ‘apply plugin: com.android.library’
- Remove the line ‘applicationId “org.opencv”‘
OpenCV Extra Modules for Android Studio
Below are the steps to build OpenCV’s extra modules for Android Studio.
Go to each of the extra modules you need (say ‘<opencv-contrib-source>/modules/extra_1’) and open ‘CMakeLists.txt’ file. Find the line containing ‘ocv_define_module’ option.
ocv_define_module(extra_1 opencv some other modules)
Add ‘java’ modules at the end of the parenthesis.
ocv_define_module(extra_1 opencv some other modules java)
If there is no ‘WRAP’ found in the list, add ‘WRAP’ before ‘java’
ocv_define_module(extra_1 opencv some other modules WRAP java)
Open ‘cmake-gui’ and select ‘OPENCV_EXTRA_MODULES_PATH’ under ‘OPENCV’ category and insert ‘<opencv-contrib-source>/modules’
And keep the above ‘cmake-gui’ settings as it is. At run, extra module ‘opencv_bioinspired’ might fail and therefore uncheck that before the configuration in cmake-gui.
Again follow the steps given under sub topic ‘Building From Source’ explained earlier in this blog.
Even after all, in your OpenCV 3+; though all the settings, modules and dependencies are set and imported; you might feel something missing after seeing applications launch on the Android device stops unexpectedly as follows.
Package not found
OpenCV Manager package was not found! Try to install it?
This is because initial OpenCV module call points to ‘opencv_java’ instead of ‘opencv_java3’.
To change this, go to openCVLibraryXXX (imported module) -> src -> main -> java -> org -> opencv -> android -> StaticHelper.java.
Find the line,
result &= loadLibrary(“opencv_java”);
and change this as,
result &= loadLibrary(“opencv_java3”);
This is the end of this blog and hope you found this useful. Why not you comment below if you think some necessary parts are missing or on something I have done wrong. Good day!