My new plugin - Android OpenCV Gradle Plugin - Discussion/Thoughts

Hey Everyone, I’ve got myself into developing a Gradle Plugin to do all the tedious stuff for including Android OpenCV libs (both java and native libs/sources) into an Android Application Gradle Project easily. I had some previous attempts of doing this other than using a Gradle Plugin but all of which we’re not good, not easy enough or even had some limitations. The major problem I wanted to solve was dependency version management and linking of the OpenCV libs by only using a simple configuration inside the build.gradle file of the project.

The coming two paragraphs is a little bit of history I went through.

In the beginning, when the Android Gradle Plugin started supporting CMake builds, I went out to import the Android OpenCV in to the project the proper way for it to support writing C++ code integrated the with native binaries of the OpenCV in an Android application. It was a better alternative to using the Java Bindings of OpenCV for most of the projects I did. I came up with a manual solution and posted it here. It really did inspire me to do more with OpenCV on Android but was tedious to handle changing or updating versions, the IDEs might not fully integrate the changes or the build might fail for some unexpected reasons.

Next comes the idea of using Git as a dependency management solution by using Git Submodules and a Git Repository, called Android-OpenCV, I hosted on GitHub. It introduced version management using the tags and felt similar to adding a dependency in build.gradle but instead the dependency is within the project Git repository instead of on the Gradle Build level alone. The configuration was simple, but after some time an issue appeared and was difficult to solve (thoughts of hosting the binaries publicly and financing was difficult to handle). Basically, GitHub has a quota or limits on how big of the files inside a repository could be or how many times they could be downloaded (Git LFS quota) and, in this case, the compiled static binaries of OpenCV we’re easily crossing the quota and eventually causing the repository to introduce errors and causing some people compilation issues with their projects that tried using this method of integration.

Fast forward now, I thought of developing a plugin that can take care of downloading the needed binaries and also properly configuring the buildscript for the project that has the Android Gradle Plugin applied. It’ll the relieve the need of an online server (other than a public server/website hosted by OpenCV to download the binaries straight from it) and do most of the work locally on the machine.

One of the interesting things that I came across was deciding when to do the necessary configurations/compilation and how to do it. I ended up using the afterEvaluate block of the project so that the plugin could do more of the automated configuration there and after the build was partially configured by the Android Gradle Plugin (but before any executions were started), and also used the Gradle Tooling API to compile the Java Bindings of OpenCV to introduce them as dependencies into the Android Application Project.

Finally, I sure hope I nailed it that way! :smiley: Best of all of this is I used the plugin in an actual build and it worked! I’ve uploaded the code here: AndroidOpenCVGradlePlugin and appreciate if anyone could have a look at it and share some thoughts about it (how I used the Tooling API or the design of the logic, the functional test cases, etc…). It still has lots of room for improvements, I’d consider that it’s still in the early stages so I haven’t yet published it on the Plugins Portal but looking forward to doing it soon, and I hope that it helps people with simplifying the integrations of their projects with OpenCV and in any other uses possible.

Thanks!