Problem with native support on 2.1

I’m trying to use the native support with Gradle 2.1, but using the examples in the manual doesn’t seem to work.

apply plugin: 'c'
  sources {
 lib {
  c {
   source {
    srcDir "src/main/native"
    include "**/*.c"
   }
   exportedHeaders {
    srcDir "**/*.h"
   }
  }
 }
}
  libraries {
 myLib {
  source sources.lib
 }
}

With this code, I’m getting:

* What went wrong:
A problem occurred evaluating project ':native'.
> Cannot create a LanguageSourceSet named 'c' because this container does not support creating elements by name alone. Please specify which subtype of LanguageSourceSet to create. Known subtypes are: CSourceSet

I’m a Gradle newbie, so it will be very nice if someone can point me in the right direction (and explain to me why the instructions form the manual doesn’t seem to work, unless I made some sort of oversight :D).

Thanks in advance!

try the following snippet:

apply plugin: 'c'
  libraries {
    myLib {
    }
}
  sources {
    myLib {
        c {
            source {
                srcDir "src/main/native"
                include "**/*.c"
            }
            exportedHeaders {
                srcDir "**/*.h"
            }
        }
    }
}

explanation: atm the component definition needs to be done before the associated sources can be configured. The component is a library named myLib. For this component a sourceSet is created with the same name (‘myLib’) and a associated CSourceSet named ‘c’. If you declare a plain SourceSet, you must explicitly provide the type of the sourceSet. This means alternatively your snippet can look like this:

apply plugin: 'c'
  sources {
    lib {
        c(CSourceSet) {
            source {
                srcDir "src/main/native"
                include "**/*.c"
            }
            exportedHeaders {
                srcDir "**/*.h"
            }
        }
    }
}
  libraries {
    myLib {
        source sources.lib
    }
}

We changed this behaviour with the latest 2.1 release. Maybe the documentation is a bit misleading here. Expect improvements in this section as it is under active development.

cheers, René

Thank you!

Both methods worked (although the second one has the sources for “lib” added, but not replacing the standard ones, but that might be normal Gradle behavior, I need to read more :D)

Yeah, I assumed this was under active development, was just surprised that the example in the manual didn’t work (I assume it was working with a previous version)

Again, thanks!

If you’re using Gradle 2.1, then you should refer to the user guide for 2.1 as well (http://www.gradle.org/docs/2.1/userguide/userguide.html).

The included examples are tested to work with the corresponding version of Gradle: if you discover a matching sample that isn’t working, please let us know.

In 2.2.1, I ran into the above problem in the top-level build.gradle located in these examples ( https://github.com/k-mack/gradle-examples ). After correcting it, I seem to still have an issue related to JNI. The following line:

jniPlatformCExtractHeaders.dependsOn nativeHeaders

resulting in:

  • What went wrong: A problem occurred evaluating root project ‘gradle-examples’. > Could not find property ‘jniPlatformCExtractHeaders’ on project ‘:jvm:jni’.

I verified the build worked in the 2.0 gradle wrapper script. In 2.2.1, it appears that the headers are not building put in to $buildDir/nativerHeaders, which is where they should be going, so I assume that is what jniPlatformCExtractHeaders is doing (running javah). How to do this in 2.2.1?

Also, I can’t seem to find any “jni” examples in the existing repository. Having one or more maintained JNI examples in gradle might be a big help. Sorry if I’m looking in the wrong place!

I moved my part of the discussion to http://gsfn.us/t/4nrs8 since it is mostly a separate issue.

Fixed now; I removed the offending line and added tasks.withType(CCompile) {task -> task.dependsOn nativeHeaders}