Subproject with both Java and C++

plugins
native

(Kyle) #1

I’m currently making a Java library that interfaces with a native C++ library. It uses the original C++ library, and it uses swig to generate Java bindings, given a interface template file and an implementation file that makes use of the library.

I have been able to modify the project’s build script that does a similar thing with Python, to work with Java, here. As shown there, the build process consists of running swig, compiling the generated C++ source code with the library, copying the resulting shared object to the Java source tree, and (not seen here) compiles the Java code. In this environment, with the Bash script, I was able to get a working Java demo running with the new Java library.

In migrating this setup to Gradle, so far, I have been able to compile the Java code fine (This is why this is not in the aforementioned Bash script, because this I already did with Gradle.). Additionally, I’ve made a build task that successfully runs swig to generate the Java and C++ code. However, I’m stuck as to what the best approach to compiling the C++ code before the Java code would be. When adding the cpp-library plugin to this build script (Keeping in mind that it already has the java-library plugin, as specified by the parent project.), this exception occurs:

An exception occurred applying plugin request [id: 'cpp-library']
> Failed to apply plugin [id 'org.gradle.cpp-library']
   > Could not create an instance of type org.gradle.language.cpp.internal.DefaultCppLibrary.
      > Could not create an instance of type org.gradle.language.internal.DefaultLibraryDependencies.
         > Cannot add a configuration with name 'implementation' as a configuration with that name already exists.

Relevant Gradle project is available here, and I can provide any other necessary details.

In closing, what is the best way to have a Gradle project that builds both C++ code (namely a shared object) and Java code?


(Shahji) #2

Could you please add the “native” tag to the post.

Have you tried moving the apply plugin: "java-library" from the root build script into the sample/build.gradle file?

The problem is that java-library and cpp-library are mutually exclusive and cannot be applied to the same project. The issue has recently been reported here https://github.com/gradle/gradle-native/issues/352


(Kyle) #3

Done.

I tried this, but it doesn’t work I think because the library project should be a Java library (and so, by not having the java-library plugin applied this isn’t specified).

That’s an interesting issue. I tried keeping java-library out of the root projects, and putting it in both subprojects, but in the library, putting it after the C++ library, resulting in this different exception, pretty much saying that the library variants are ambiguous. When specifying either the apiElements or cppApiElements configurations like what is done here, this exception comes up.

Edit: I’m now kinda splitting the C++ and Java into two separate subprojects, and so far that does seem to be the way to go here.

Edit 2: I didn’t end up getting that working because I couldn’t understand how the C++ plugins work at all.