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?