Gradle NDK to specify an 'include' directive in generated Android.mk

android, android-ndk, gradle, opencv

Solution

I've found that that the build process will pull everything in from below the ./src/main/jni folder. So, I've placed symlinks there to include and src folders elsewhere - the src files will be enumerated into the .mk file by the build process and the inc files will be scooped up by the compiler. Perhaps its a bit hacky:

android {
    defaultConfig {
        ndk {
            moduleName "yourlib"
            cFlags "-std=c99 -I${project.buildDir}/../src/main/jni/inc"
        }
        ...
    }
    ...
}

I also have different cFlags depending upon debug build. This seems to be valid gradle, but doesn't want to build with android-studio. It will build with the gradlew command tho:

android {
    defaultConfig {
        ndk {
            moduleName "yourlib"
            cFlags "-std=c99 -I${project.buildDir}/../src/main/jni/inc"
        }
        ...
    }
    ...
    buildTypes {
        release {
            debuggable false
            jniDebugBuild false
            ndk {
                moduleName "yourlib"
            }
        }
        debug {
            debuggable true
            jniDebugBuild true
            ndk {
                moduleName "yourlib"
                ldLibs "log"
                cFlags "-g -D_DEBUG"
            }
        }
    }
}

I hope it can help you (android-studio 0.8.6).

Problem

When you have ``` android { defaultConfig { ndk { moduleName "yourlib" stl "stlport_static" ldLibs "log", "z", "m" cFlags "-I/some/include/dir/" } ... } ... } ``` in your build.gradle then Gradle will compile the files in src/main/jni/ and it will generate an Android.mk in build/ndk/debug/Android.mk. However, in my case, I'm trying to compile some C++ files compiled against OpenCV. I have this working when I manually create the Android.mk file and run the ndk-build command. But I want to do it via Gradle / Android Studio automatically. When doing this manually, I include the libraries to link against. I do this, in the manually created Android.mk, with the line: ``` include /path/to/the/opencv/directory/sdk/native/jni/OpenCV.mk ``` However, in Android's Gradle plugin, I am unsure of how to add this 'include' directive in the generated Android.mk file. Can anyone point me in the right Gradle-directive direction to add this line to the generate file? Thanks.

Original source

Related problems