Native - Can't specify 'dependsOn' tasks for CPP compilation tasks

Hi. Like the title says.

I’m using Gradle 2.3 to generate and compile SWIG wrappers for an application. When typing gradle tasks on my project, I see some tasks for each library configured in the project, like so:

compileMyExampleLibSharedLibraryMyExampleLibCpp
linkMyExampleLibSharedLibrary

Now, if I have a dependent task swig, that generates Java wrappers for the C++ code, I can’t use this:

compileMyExampleLibSharedLibraryMyExampleLibCpp.dependsOn swig

Error:

* What went wrong:
A problem occurred evaluating project ':jni-native'.
> Could not find property 'compileMyExampleLibSharedLibraryMyExampleLibCpp' on project ':jni-native'.

It seems as if the task above gets defined at runtime.

Also, I can’t apply the cpp and java plugins at the same time to compile mixed C++/Java sources.

Any help/guidance is appreciated. Thanks!

This configuration needs to go in a model { } block. Please see the Gradle 2.2 release notes for details.

model {
    tasks.compileMyExampleLibSharedLibraryMyExampleLibCpp {
        dependsOn swig
    }
}
2 Likes

This should be possible. Are you receiving an error?

Hi. Yeah, as soon as I say apply plugin: 'java', I get:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/jzazueta/code/octave-interop/octjni-native/build.gradle' line: 25

* What went wrong:
A problem occurred evaluating project ':octjni-native'.
> Could not find property 'cppCompiler' on classes 'main'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

My best guess is that some object instance might be getting replaced by whatever the ‘java’ plugin does internally. Thanks for the help!

Your suggestion for the first problem above worked. Yay!

BTW that note should probably be referenced in the native binaries documentation section. Thanks again!

Can you share your build script so we can get a better idea of what’s going on?

Sure man. I’ll upload a slimmed down version of it to my github repo later today.

Thanks for the help!

If I had to guess, I bet you’re doing something like this:

binaries.all {
   cppCompiler.define 'SOMETHING'
}

Under the covers, the Java plug-in puts things in the binaries container too, so you’re trying to configure the cppCompiler on a collection of classes. In a mixed project, you’re better off being a little more explicit and selecting a particular type of binary:

binaries.withType(NativeBinary) {
   cppCompiler.define 'SOMETHING'
}

HTH

1 Like

Yup, that did the trick man. Restricting the binaries predicate made the java plugin stop complaining. Now I’ll refactor my code to see if I can export a JNI jar inside a single project in one fell swoop.

Maybe this could make a nice contribution to the Gradle user manual right? Perhaps a section for those looking to build SWIG wrappers? I’ll let you guys know if I stumble upon any other roadblocks.

Cheers!

We love contributions.

I think a single project mixed Java/C++ sample would be pretty cool. It wouldn’t have to use something elaborate (i.e., use SWIG) to just demonstrate that it’s possible to do that and maybe outline some gotchas. If you’d like to discuss it more, please hit the gradle-dev list.

1 Like

Will do. Thanks again yo!