gradle experimental include file directories via srcDir srcFile directive

1,147 views
Skip to first unread message

gui...@prettysimplegames.com

unread,
Dec 15, 2015, 10:12:56 AM12/15/15
to android-ndk

Does anyone knows how the includes of srcDir works in gradle experimental files (cpp AND h)? This is kind of a "3-fold" question:

1°) how do the srcDir work?

1.a°) Does it include recursively all subdirs? Does it only include files at their deep level? Does it they include all cpp/c/cc/cxx files?

for example this command:

android.sources {
    main {
        jni {
            source {
                srcDir "../../../../some/path/src"
            }
        }
    }
}

Does it include all cpp files under src? all files under src? all cpp files recursively into subdirs? all files recursively into subdirs?

The google documentation is very vague:

http://tools.android.com/tech-docs/new-build-system/gradle-experimental

and the gradle one is not clear either:

https://docs.gradle.org/current/userguide/nativeBinaries.html

It says it only includes src/${name}/cpp ? what does it mean? Do I have to create a

 ../../../../some/path/src/cpp 

folder?

1.b°) What about the headers:

android.sources {
    main {
        jni {
            exportedHeder {
                srcDir "../../../../some/path/src"
            }
        }
    }
}

I have the feeling that the srcDir directive applied on headers works differently than the srcDir for source (it includes only the headers of its current depth)

2°) What if I want a mix between file and dir?

android.sources {
    main {
        jni {
            source {
                srcDir "../../../../some/path/src"
                srcFile "../../../../some/path/src/myFile.cpp" 
            }
        }
    }
}

doesn't seem to work

3°) how does the include/ exclude directive works?

What about the include/exclude directive, does they apply only on the previous srcDir statement? Or do they apply on all the statement of the "source" block?

doing:

 android.sources {
        main {
            jni {
                source {
                    srcDir "../../../../some/path/src"
                    include "*.cpp"
                }
            }
        }
    }

doesn't seem to include any cpp file. I thought it would include all cpp files of this folder hierarchy, or even all cpp files under src, but it looks like it doesn't.

I want to point that I am using gradle 2.9, which is required by the latest gradle-experimental-0.6.0-alpha3 plugin.

Raymond Chiu

unread,
Jan 6, 2016, 1:10:35 PM1/6/16
to android-ndk
The jni source set is created by the Android plugin and used to generate Gradle's CSourceSet and CppSourceSet for the native binaries.

andorid.ndk {
    moduleName
= "foo"

}
android
.sources {
    main
{
        jni
{
            source
{
                srcDir
"../../../../some/path/src"
       
}
   
}
}

Defining the source set as above essentially generates native library similar to the following:

libraries {
    foo
}
sources
{
    foo
{
        c
{
            srcDir
"../../../../some/path/src"
            include
"**/*.c"
       
}
        cpp
{
            srcDir
"../../../../some/path/src"
            include
"**/*.C"
            include
"**/*.CPP"
            include
"**/*.c++"
            include
"**/*.cc"
            include
"**/*.cp"
            include
"**/*.cpp"
            include
"**/*.cxx"
       
}
   
}
}

So, to answer your questions more directly

> 1.a°) Does it include recursively all subdirs? Does it only include files at their deep level? Does it they include all cpp/c/cc/cxx files?

Yes, it recursively include all subdir.  You can see the file extensions we expect in the code above.

> 1.b°) What about the headers:

exportedHeader will be add as an include path during compilation.  It will also affects include paths if you have dependencies.  It is not recursive.

> 2°) What if I want a mix between file and dir?

There is no srcFile method.

> 3°) how does the include/ exclude directive works?

exclude is copied to the generated C/CppSourceSet.  So it should work as expected.
include is essentially ignored.
Typical native development can have a mix of C and C++ sources.  So we want to make it work by default.  If the default include pattern doesn't work for you, please let me know about your use case and we will consider how to resolve it.

XiaoMing Zhang

unread,
Apr 13, 2016, 2:34:18 PM4/13/16
to android-ndk
@Raymond Chiu 
exclude seems not work. I used gradle-experimental:0.7.0-alpha4. And is there a way to exclude a directory?

Raymond Chiu

unread,
Apr 14, 2016, 1:35:18 PM4/14/16
to android-ndk
Hi XiaoMing,

exclude should work.  The path is relative the the srcDir, which is src/main/jni by default.  You can just exclude the folder.  So if you want to exclude src/main/jni/ignore, you can just add

android {
    sources
{
        main
{
            jni
{
                source
{
                    exclude
"ignore"
               
}
           
}
     
  }
   
}
}

dmytro....@gmail.com

unread,
Apr 26, 2016, 11:32:43 AM4/26/16
to android-ndk
@Raymond Chiu 

Have you plans to support srcFile ?

It would be much easier to convert existing Android.mk/Application.mk build files to Gradle.

Adrian Lis

unread,
May 20, 2016, 8:47:40 PM5/20/16
to android-ndk
I am in a need of defining my own specific set of include files. How can I achieve that? The problem is that a library consists of many source files, some of which should not be compiled for android thus not be included while building the lib variant for android yet I also use gradle native cpp plugin to build library artifacts for other non-android platforms that include another set of the files from the same directory.

Any ideas? I was really hoping that include would work as expected like in default native C/CPP gradle plugins.

Raymond Chiu

unread,
May 23, 2016, 1:49:12 PM5/23/16
to android-ndk
We support include patterns now in 0.8.0-alpha1.  This be effectively the same as srcFile.

pookie

unread,
Jun 7, 2016, 10:59:21 AM6/7/16
to android-ndk
I have a project which most of its c(pp) files are for that project only. But some source file belong to other parts and modules of an larger project. I want to include those (and only those) from the parent project.
the will change time to time so i don't want to copy them to the jni folder and not copy them in a task before each (test)build.


Will that be possible with include like:

         srcDir "main/jni"
         include "../../../opt/file.cpp"
srcFile looks to me as a more logical name for adding a file. Include looks more as a filter in the source dir like only use files starting with main like mainframe.cpp maininto.c

Btw:
i can't seem to test 0.8.0-alphax because even when i only use srcDir i get build errors (gcc, windows) no input file:null



Op maandag 23 mei 2016 19:49:12 UTC+2 schreef Raymond Chiu:

Qizhi Yu

unread,
Jul 16, 2016, 8:16:35 PM7/16/16
to android-ndk

Where can I find some documents for the syntax of ndk/gradle stuff you discussed above ?
Reply all
Reply to author
Forward
0 new messages