3

I have a build.gradle file which I'm trying to use it to build my JNI instance with, I have been using the Gradle documentation but there are a lot of C++ examples and not many with C. What I'm trying to achieve is a setup where I can add any number of .c and .h files into my application and use Gradle to compile these and create one or multiple .so library file(s).

I know my build.gradle is wrong but I'm lost for ideas how to go about this, I've found lots of stuff on the C++ side of JNI but not C.

My current Project structure looks like this:

project/
├──src/
   └── com/
       └── example/
           └── subProjectExample1
               └── HelloJNI.java
           └── subProjectExample2
               └── main.java
   └── main/
       ├── c
           └── HelloJNI.c
       ├── headers
           └── com.example_HelloJNI.h

build.gradle

allprojects {
    apply plugin "java"
    apply plugin "c"
    apply plugin "eclipse"
}
subprojects {
    apply plugin "java"
    repositories {
        jcenter()
    }
}

project(':example1') {
    dependencies {
    }
}   

project(':example2') {
    dependencies {
        compile project(':example1')
    }
}   

model {
    repositories {
        libs(PrebuiltLibraries) {
            jdk {
                headers.srcDirs "${System.properties['java.home']}/../include", 
                    "${System.properties['java.home']}/../include/linux"
            }
        }
    }
    platforms {
       linux64 {
        operatingSystem "linux"
           architecture "x86_64"
       }
    }
    toolchains {
        gcc(Gcc) {
            eachPlatform {
                cCompiler.executable = "/usr/bin/gcc"
            }
        }
    }
    components {
        main(NativeLibrarySpec) {
            sources {
                c {
                    source {
                        srcDir "src/main/c"
                        include "**/*.c"
                    }
                    exportedHeaders {
                        srcDir "src/main/headers"
                    }
                }
            }
        }
    }
}

I'm running this with the ./gradlew mainExecutable from these documents

The output.txt I'm getting is:

HelloJNI.c: fatal error: jni.h: No such file or directory

 #include "jni.h"
                 ^   
compilation terminated.

error: command 'gcc' failed with exit status 1

Just for reference I'm using Gradle version 5.6.2 with Eclipse.

EDIT:

I have added the following:

repositories {
        libs(PrebuiltLibraries) {
            jdk {
                headers.srcDirs "${System.properties['java.home']}/../include", 
                    "${System.properties['java.home']}/../include/linux"
            }
        }
    }

But I'm still getting the same error.

  • You will need to add the JNI headers to the include path of your gradle file (can typically be found in `$JAVA_HOME/include`) – Botje Sep 19 '19 at 08:14
  • Possible duplicate of [jni.h: no such file or directory](https://stackoverflow.com/questions/13466777/jni-h-no-such-file-or-directory) – Botje Sep 19 '19 at 08:15
  • Apologises @Botje, I uploaded a previous version of my `build.gradle` file. I did have it working with the `$JAVA_HOME/include` through the compiler but I want to be able to change the build script so I can deploy it through Docker later down the line or even include windows support so that fix is a temporary one. – Move_Regulator05 Sep 19 '19 at 13:57
  • 1
    You could do worse than take inspiration from [this example](https://github.com/avu/gcj/blob/master/build.gradle) linked from [IntelliJ documentation](https://www.jetbrains.com/help/idea/setting-up-jni-development-in-gradle-project.html) – Botje Sep 19 '19 at 14:03
  • @Botje Massive thanks for that example. I've now got it build `.o` files but do you have any examples of how to create the `.so` library file? – Move_Regulator05 Sep 20 '19 at 09:50

0 Answers0