[英]Using JNI with Gradle (and lombok)
I'm trying to connect my Java app to come c++ code I've written.我正在尝试将我的 Java 应用程序连接到我编写的 c++ 代码。
What I've gathered from tutorials online is:我从网上教程中收集到的是:
javac
with the -h
flag to generate c/c++ headers for classes with native methods-h
标志的javac
为具有本机方法的类生成 c/c++ 头文件System.loadLibrary
call, to load in the built library, so now you can use the native functions - implemented by your library.System.loadLibrary
调用,以加载构建的库,因此现在您可以使用本机函数 - 由您的库实现。 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:我遇到麻烦的第一步是第 1 步——我遇到了很多关于依赖项的编译错误,所以我在网上搜索了一些关于如何将 JNI 与 gradle 一起使用——而不仅仅是纯 java 编译器,我发现了这个任务:
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;这项任务似乎有效,但现在我遇到的问题是它无法通过 lombok 注释进行编译; for example, I'm getting an unfound symbol for a getter function that's generated by lombok.
例如,我得到了一个由 lombok 生成的 getter function 的未找到符号。
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.我已经正确设置了 lombok - 我的正常 gradle 构建工作正常 - 但我认为 lombok 代码生成没有针对此任务完成。 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现在将处理 lombok 注释,可以正确生成 header 文件
You can try forcing lombok to run by adding compiler arg -processorpath path/to/lombok.jar
.您可以尝试通过添加编译器 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.如果这个特定任务的目的只是生成 header 文件而不是别的,另一个选择是首先让 lombok delombok 将所有源代码放入一个 tempdir,然后在其上运行 javac。 Bit drastic, perhaps.
有点激烈,也许。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.