7

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.

Janusz
  • 187,060
  • 113
  • 301
  • 369
mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
  • Note syntax changes in Gradle 2.5 cFlags becomes CFlags and ldLibs "log" becomes ldLibs += "log" – Lorne K Oct 03 '15 at 00:02

1 Answers1

8

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).

  • Hello @orthopteroid, this is a fantastic answer. Thank you. I was wondering if you know how I can add libraries. I tried to continue in `cFlags` but it doesn't work. My post is this one http://stackoverflow.com/questions/34593133/gradle-cant-find-opencv-libraries-for-debugging-with-ndk – Rafael Ruiz Muñoz Jan 04 '16 at 15:39
  • 1
    It's only a slight variation on your answer, so I don't want to post it separately, but `CFlags.add("-I${file("src/main/jni/inc")}".toString())` serves the same purpose and Android Studio is happy with it. Note that `cFlags` changed to `CFlags` at some point. Also in my actual usage case it wasn't `CFlags` that was relevant, but `cppFlags`. – Rob Pridham Mar 18 '16 at 08:31