# Subproject with both Java and C++

**URL:** https://discuss.gradle.org/t/subproject-with-both-java-and-c/30509
**Category:** Help/Discuss
**Tags:** plugins, native
**Created:** [February 9, 2019, 5:20am UTC](https://discuss.gradle.org/t/subproject-with-both-java-and-c/30509 "2019-02-09T05:20:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![CodingKoopa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/codingkoopa/32/7472_2.png) [@CodingKoopa](https://discuss.gradle.org/u/CodingKoopa)
#### Post date: [February 9, 2019, 5:20am UTC](https://discuss.gradle.org/t/subproject-with-both-java-and-c/30509/1 "2019-02-09T05:20:19Z")

</div>

I’m currently making a Java library that interfaces with a native C++ library. It uses the [original C++ library](https://github.com/charmedlabs/pixy2/tree/master/src/host/libpixyusb2), and it uses [`swig`](http://www.swig.org/) to generate Java bindings, given a [interface template file](https://github.com/CodingKoopa/pixy2/blob/java-demos/src/host/libpixyusb2_swig/pixy.i) and an [implementation file](https://github.com/CodingKoopa/pixy2/blob/java-demos/src/host/libpixyusb2_swig/pixy.cpp) 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](https://github.com/CodingKoopa/pixy2/blob/java-demos/scripts/build_java_demos.sh). 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](https://github.com/CodingKoopa/pixy2/blob/java-demos/src/host/libpixyusb2_java/libpixyusb2/build.gradle) 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:

```gradle
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](https://github.com/CodingKoopa/pixy2/tree/java-demos/src/host/libpixyusb2_java), 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?

---

<div class="post-metadata">

### Author: ![Shahji](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Shahji](https://discuss.gradle.org/u/Shahji)
#### Post date: [February 9, 2019, 7:52am UTC](https://discuss.gradle.org/t/subproject-with-both-java-and-c/30509/2 "2019-02-09T07:52:07Z")

</div>

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](https://github.com/gradle/gradle-native/issues/352)

---

<div class="post-metadata">

### Author: ![CodingKoopa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/codingkoopa/32/7472_2.png) [@CodingKoopa](https://discuss.gradle.org/u/CodingKoopa)
#### Post date: [February 9, 2019, 2:16pm UTC](https://discuss.gradle.org/t/subproject-with-both-java-and-c/30509/3 "2019-02-09T14:16:28Z")

</div>

> [@Shahji](#):
>
> Could you please add the “native” tag to the post.

Done.

> [@Shahji](#):
>
> Have you tried moving the `apply plugin: "java-library"` from the root build script into the sample/build.gradle file?

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).

> [@Shahji](#):
>
> 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 [configuration name clash · Issue #352 · gradle/gradle-native · GitHub](https://github.com/gradle/gradle-native/issues/352)

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](https://pastebin.com/ztfpFbd2) different exception, pretty much saying that the library variants are ambiguous. When specifying either the `apiElements` or `cppApiElements` configurations like what is done [here](https://github.com/dialogflow/dialogflow-android-client/issues/57#issuecomment-453332881), [this](https://pastebin.com/XGAVGH2t) 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.
