Regarding linking external native libs (opencv c++) in gradle

Reposting my stack overflow question from :

I am creating a project that uses opencv C++ code. As of this moment I am only able to find the java executables being hosted in maven central at :-
So I installed opencv on my system and started using that for my project. In order to compile my project with the opencv headers I introduced the following in my build.gradle script:-

// C++ specific build configurations

// Acquiring environment variables
def opencvhome = System.getenv(“OPENCV_HOME”)

def opencvinclude = opencvhome + “\include”

model {
components {
main(NativeLibrarySpec) {
sources {
cpp {
source {
srcDirs "src/main/cpp"
include “**/*.cpp”
exportedHeaders {
srcDirs “src/main/include”, opencvinclude
This was for including the necessary headers. Notice the opencvinclude part as picked up as an environment variable which I set to the install location before running the gradle build.

Now my question is how do I add additional linkage dependencies of opencv?
I consulted the above documentation. It only covers dependencies that are themselves gradle projects. But opencv C++ is not a gradle project. Can someone please help me out with this?

What you want is a prebuilt library. These allow you to define external libraries and create dependencies on them as if they were library components in your build. You can find a sample in the gradle distribution or at

1 Like

Thank you

This is precisely what I needed.

Is it all right if edit the documentation to add this part into

I see something like this in cunit test section, but I believe it will be better if a separate subsection be introduced for just this purpose. A lot of CPP projects are not on maven.