Ask Your Question

Revision history [back]

Where to find header files when building with android-gradle

I have successfully built OpenCV 3.4.3 from source using Gradle and the Android NDK (16b - 18b also seemed to work).

I've used the ExternalNativeBuild plugin to do this - which seems to be the current standard/recommended way to build Android native libraries.

The only problem is that the header files that should be generated and output to CMAKE_INSTALL_PREFIX, are not produced. I suspect that this is because when Gradle builds the library, it is not actually calling "ninja install" or "make install". I'm not completely sure though, because it's hard for me to tell exactly what it's doing under the hood.

I need the generated header files because I want to link openCV with some other android native projects. Has anyone else gotten this to work? Did you have to make a separate call to ninja/make? I'd like to avoid that if possible.

This is what my build.gradle looks like (the applicable part at least):

    apply plugin: 'com.android.library'

...

        externalNativeBuild {
        cmake {
            arguments '-G', 'Ninja',
                    //'G', 'Unix Makefiles',
                    //'-DCMAKE_MAKE_PROGRAM=make',
                    '-DANDROID_TOOLCHAIN=clang',
                    '-DANDROID_STL=c++_shared',
                    '-DANDROID_ARM_NEON=TRUE',
                    "-DCMAKE_INSTALL_PREFIX=${projectDir}/modules", <- headers should be here
                    '-DANDROID_NATIVE_API_LEVEL=android-18',
                    '-DENABLE_PRECOMPILED_HEADERS=OFF',
                    //'-DCMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD', <-tried that but didn't work
                    '-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON',
                    '-DBUILD_DOCS=OFF',
                    '-DBUILD_TESTS=OFF',
                    '-DBUILD_PERF_TESTS=OFF',
                    '-DBUILD_ANDROID_EXAMPLES=OFF',
                    '-DBUILD_EXAMPLES=OFF',
                    "-H${rootProject.sourcesDir}"
        }

        ndk {
            abiFilters = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
        }
    }
}

Other Notes:

• Had to set ANDROID_SDK_ROOT environment variable

• Replace tools at $ANDROID_SDK_ROOT/tools with tools_r25.2.5 (See 'https://github.com/opencv/opencv/issues/8460')

• clang and libc++ are used instead of gcc and gnustl (See 'https://developer.android.com/ndk/guides/cpp-support')

PS: I know that building with Gradle isn't officially supported by OpenCV, but I think it would be really great if it could be. Gradle seems to be the preferred and most widely-used tool for building Android libraries.