Java Native Interface (JNI) bindings (and generator) for Crystal

Java Native Interface (JNI) bindings (and generator) for Crystal

Generate Java bindings through the Java Native Interface (JNI) automatically from Java classes using the javap tool distributed with the JDK.

The main goal is to generate bindings for the Android SDK. This project is thus for the most usable widget and communication classes (e.g., android.os.Handler or android.widget.TextView). More complex classes, mostly those relying on generics and wildcards will probably fail, or generate invalid Crystal code.


Add the shard to your shard.yml dependencies:

    github: ysbaddaden/


Let's generate some bindings for an Android project. First we must add the target platform android.jar to CLASSPATH. For example. Let's also add the project local folder where the Android SDK will store the compiled classes.

export CLASSPATH=bin/classes:/opt/android-sdk/platforms/android-23/android.jar

Let's generate some bindings, this will load the definition of the specified classes, potentially following classes found, then generate the Crystal bindings into namespace separated folders. Pass --help for the full list of options.

lib/java/bin/generator --output src --no-follow \ \
    android.os.Bundle \
    android.os.Message \

You may now just use it:

require "android/os/bundle"
require "android/os/message"

bundle =
bundle.put_char_sequence("action", "setText")
bundle.put_char_sequence("text", "hello world")

message =


  • fix bugs
  • write tests


  • Julien Portalier (creator, maintainer)
Github statistic:
  • 20
  • 2
  • 1
  • 5
  • 1
  • 2 months ago