简体   繁体   中英

Using JNI with Gradle (and lombok)

I'm trying to connect my Java app to come c++ code I've written.

What I've gathered from tutorials online is:

  1. You use javac with the -h flag to generate c/c++ headers for classes with native methods
  2. You then import the generated headers into your c/c++ app and implement them
  3. You build the implemented app to a dynamic library file
  4. You place the built library file in the java libs path (where ever it is)
  5. You then add a static System.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM