I'm trying to connect my Java app to come c++ code I've written.
What I've gathered from tutorials online is:
javac
with the -h
flag to generate c/c++ headers for classes with native methodsSystem.loadLibrary
call, to load in the built library, so now you can use the native functions - implemented by your library.The first step I had trouble at was step 1 - I got loads of compile errors regarding dependencies, so I did some searching online about how to use JNI with gradle - as opposed to just the pure java compiler, and I found this task:
task generateJniHeaders(type: JavaCompile) {
classpath = sourceSets.main.compileClasspath
destinationDir file("${buildDir}/generated/jni")
source = sourceSets.main.java
options.compilerArgs += [
'-h', file("${buildDir}/generated/jni"),
'-d', file("${buildDir}/generated/jni-tmp")
]
// options.verbose = true
doLast {
delete file("${buildDir}/generated/jni-tmp")
}
}
This task seems to work, but now the problem I'm having is it can't compile through the lombok annotations; for example, I'm getting an unfound symbol for a getter function that's generated by lombok.
I have lombok correctly setup - my normal gradle build works fine - but I assume that the lombok code generation is not being done for this task. Is it possible to do this?
Configure the annotation processor for the compilation like so:
dependencies {
annotationProcessor 'org.projectlombok:lombok'
// ...
}
task generateJniHeaders(type: JavaCompile) {
classpath = sourceSets.main.compileClasspath
destinationDir file("${buildDir}/generated/jni")
source = sourceSets.main.java
options.compilerArgs += [
'-h', file("${buildDir}/generated/jni"),
'-d', file("${buildDir}/generated/jni-tmp")
]
options.annotationProcessorPath = configurations.annotationProcessor
// ^^^^ use the configured annotation processor ^^^^
doLast {
delete file("${buildDir}/generated/jni-tmp")
}
}
Now the lombok annotations will be processed and the header files can be generated properly
You can try forcing lombok to run by adding compiler arg -processorpath path/to/lombok.jar
.
If the point of this particular task is just to generate header files and nothing else, another option is to first let lombok delombok all your sources into a tempdir, and then run javac on that. Bit drastic, perhaps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.