Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one set a C/C++ include path with Gradle Experimental Plugin?

Android Studio cannot find my header files when they are in a location different to the main source folder of my module. This is noticeable by #include "SDL_config.h" statements being highlighted in red (other #include statements are fine).

I have tried modifying the cppFlags values below but I am starting to doubt that these cppFlags are even being passed to the compiler at all.

Has anyone managed to include files from folders other than their main source folder in this way?

Is there a problem with the way I have specified the cppFlags, or perhaps the moduleName or even sources?

I could go through the SDL2 source and modify all of the #include statements to relative #include paths but I don't really want to modify the SDL2 source. I should simply be able to specify a list of include paths somehow.

apply plugin: 'com.android.model.application'

model {
...
    android.ndk {
        moduleName = 'main'

        //W:\hello-sdl-android\android-project\app\src\main\jni\SDL2\include
        cppFlags += "-I${file("src/main/jni/SDL2/include")}".toString()
        cppFlags += "-I${file("src/main/jni/SDL2/src")}".toString()
    }

    android.sources {
        main.jni {
            source {
                srcDirs += ['src/main/jni/src']
                srcDirs += ['src/main/jni/SDL2/src']
            }
        }
    }
...
}

UPDATE: Here's more information on my current project layout:

app/src/main/jni app/src/main/jni/src/Main.cpp <- This is compiling app/src/main/jni/SDL2 <- All SDL2 source is under here app/src/main/jni/GLM <- All GLM source is under here

This layout is a direct result of using the example project which I was provided here: https://gitlab.com/scannerdarkly/hello-sdl-android

That project will build using ndk-build from the command line - I wanted to take things a step further by building within Android Studio. My project will attempt to draw a triangle on a GLES2 device.

Here is a link to my current project so far:

http://www.mediafire.com/download/92577p7vf123l72/hello-sdl-android.zip

like image 892
SparkyNZ Avatar asked Nov 11 '15 19:11

SparkyNZ


People also ask

How do I change the Gradle path in Android Studio?

To change its path go to this path File > Settings... > Build, Execution, Deployment > Gradle In the Global Gradle settings Change Service directory path to what you want. Save this answer.

How do I add plugins to build Gradle?

To add a Gradle plugin with dependencies, you can use code similar to the following: Plugin DSL GA versions. Plugin DSL non GA versions. Legacy Plugin Application.

Does Gradle support C?

Gradle is a build automation system that provides plugins to build C/C++ libraries and applications. In CLion, you can work with Gradle projects that are based on cpp-application and cpp-library plugins (for more details on C/C++ Gradle plugins, see this blog post: Introducing the new C++ plugins).


2 Answers

I use a slightly different approach:

cFlags += "-I" + file("src/main/jni/SDL2/include").absolutePath

.. and this works. The reason is probably that the compiler is launched with a different working directory, and absolutePath resolves any ambiguity here.

like image 105
Alex Cohn Avatar answered Sep 20 '22 11:09

Alex Cohn


Yes! I figured it out - SDL2 source files are .C files, so include paths need to be declared using CFlags, not cppFlags.

like image 23
SparkyNZ Avatar answered Sep 19 '22 11:09

SparkyNZ