gjavah icon indicating copy to clipboard operation
gjavah copied to clipboard

Applied @greg2001 changes as made the program friendlier for an automated build environment

Open briandilley opened this issue 5 years ago • 1 comments

  • Applied pull request #2 from @greg2001
  • Added support for passing class files in to have headers generated (this made it easier to integrate with a gradle build process)
  • Made a few more modifications to make the header generate identical to the javac -h ... command
  • Added caching for ClassReader
  • Made it possible to send classes in that don't have native methods, these are just ignored (this made it easier to integrate with a gradle build process)

briandilley avatar Nov 27 '19 19:11 briandilley

I'm using this with gradle to create header files for my Kotlin code, here's what i'm using in my gradle file:

String NATIVE_SRC_DIR = "src/main/jni"
String NATIVE_SRC_JNI_INCLUDE_DIR = "src/main/jni/src/jni/generated"
String NATIVE_OUTPUT_DIR = "src/main/resources/native/lib"

...

/**
 * Generates header files for native methods.
 */
ext.javah = { String classpath, String outputDir, String classes ->
	exec {
		commandLine "sh", "tools/gjavah.sh", "-d", outputDir, "-classpath", classpath, classes
	}
}

...

tasks.withType(JavaCompile) {
	inputs.files(fileTree(NATIVE_SRC_DIR))
	inputs.files(fileTree(NATIVE_SRC_JNI_INCLUDE_DIR))
	outputs.dirs(NATIVE_OUTPUT_DIR)
	doLast {
		javah((sourceSets.main.runtimeClasspath + sourceSets.main.output).asPath,
				NATIVE_SRC_JNI_INCLUDE_DIR,
				sourceSets.main.output.asFileTree.matching { include "**/*.class" }.join(" "))
	}
}

Where tools/gjavah.sh is simply:

#!/bin/sh -e
MY_DIR=$(realpath $(dirname "$0"))
java -jar "$MY_DIR"/gjavah.jar $@

briandilley avatar Nov 27 '19 19:11 briandilley