Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass specific arguments (example: opencv_dir) to CMake through gradle externaNativeTool?

I am trying to execute a CMake script in Android studio as an externalNativeBuildTool which it has OpenCV dependency.

I know that I can define statically in the script where the OpenCV and I wouldn't like to use OpenCV manager or plugin to the android studio. I want jus to pass Opencv_dir parameter to avoid to copy all headers and libraries to the project.

So, in conclusion, I have this error:

 Could not find a package configuration file provided by "OpenCV" with   any  of the following names:
    OpenCVConfig.cmake
    opencv-config.cmake

I' ve looking for a solution like:

https://developer.android.com/ndk/guides/cmake.html#build-command.

But these arguments are not satisfied, And I tried to put the Opencv_dir like this:

externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fno-rtti -fexceptions "
                arguments "-DANDROID_ARM_NEON=ON -DANDROID_PLATFORM=18 
               -DOpenCV_DIR="+getOpenCVDir().toString()

            }
        }
def getOpenCVDir() {
    Properties properties = new Properties()
    properties.load(new File(rootDir.absolutePath + "/local.properties").newDataInputStream())
    def externalModuleDir = properties.getProperty('opencv.dir', null)
    if (externalModuleDir == null) {
        throw new GradleException(
                "OpenCV location not found. Define location with opencv.dir in the local.properties file!")
    }
    return externalModuleDir
}

where the opencvdir is defined on local properties

opencv.dir={my_sdk_path}/OpenCV-android-sdk/sdk/native/jni

So is there any way to pass custom arguments to cmake through gradle?

Thanks in advance.

like image 785
uelordi Avatar asked Mar 10 '17 09:03

uelordi


1 Answers

I found the solution, there was a typo mistake:

It is necessary to put CMake flags as a list.

Before:

 externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fno-rtti -fexceptions "
                arguments "-DANDROID_ARM_NEON=ON -DANDROID_PLATFORM=18 
               -DOpenCV_DIR="+getOpenCVDir().toString()

            }
        }

After:

 externalNativeBuild {
     cmake {
            arguments "-DANDROID_ARM_NEON=TRUE",   
                      "-DOpenCV_DIR="+getOpenCVDir()

           cppFlags "-std=c++11 -fno-rtti -fno-exceptions -fpermissive"
            }
        }

Now it works perfect and it detects the opencv_dir.

like image 110
uelordi Avatar answered Sep 19 '22 07:09

uelordi