{"id":17216884,"url":"https://github.com/markusjx/jni-bindgen","last_synced_at":"2025-03-25T13:44:19.343Z","repository":{"id":206667304,"uuid":"711291708","full_name":"MarkusJx/jni-bindgen","owner":"MarkusJx","description":"Automatically generate JNI bindings for Rust code","archived":false,"fork":false,"pushed_at":"2024-03-16T17:27:19.000Z","size":261,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T12:46:46.756Z","etag":null,"topics":["bindgen","code-generation","jni","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarkusJx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-28T19:37:43.000Z","updated_at":"2023-11-11T11:55:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"a545fb6d-a42d-4af6-8784-78f881f14f9a","html_url":"https://github.com/MarkusJx/jni-bindgen","commit_stats":null,"previous_names":["markusjx/jni-bindgen"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fjni-bindgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fjni-bindgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fjni-bindgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkusJx%2Fjni-bindgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarkusJx","download_url":"https://codeload.github.com/MarkusJx/jni-bindgen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245474687,"owners_count":20621452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bindgen","code-generation","jni","rust"],"created_at":"2024-10-15T03:42:35.994Z","updated_at":"2025-03-25T13:44:19.312Z","avatar_url":"https://github.com/MarkusJx.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jni-bindgen\n\nAutomatically generate JNI bindings for Rust code.\n\n## Usage\n\nIn order to use this crate, you need to add the following to your `Cargo.toml`:\n\n```toml\n[dependencies]\njni-bindgen = { git = \"https://github.com/MarkusJx/jni-bindgen\" }\n```\n\nIf you want to generate Java bindings for your Rust code, you must set the\n`JNI_BINDGEN_OUT_DIR` environment variable to the directory where the generated\ncode should be placed. Alternatively, you can use the cli tool to generate the\ncode.\n\nFor the Java bindings to work, you need to add the `JNIBindgen` dependency to\nyour Java project. The dependency is currently not available on Maven Central,\nbut you can retrieve it using [JitPack](https://jitpack.io/#MarkusJx/jni-bindgen).\nFor Gradle, you can add the following to your `build.gradle`:\n\n```groovy\nrepositories {\n    mavenCentral()\n    maven { url 'https://jitpack.io' }\n}\n\ndependencies {\n    implementation 'com.github.MarkusJx:jni-bindgen:master-SNAPSHOT'\n}\n```\n\n### Export a struct to Java\n\n```rust\nuse jni_bindgen::objects::traits::{FromJNI, ObjectFromJNI};\nuse jni_bindgen::jni;\n\nstruct MyClass {\n    field: i32,\n}\n\n#[jni(package = \"com.example\")]\nimpl MyClass {\n    #[jni(constructor)]\n    fn ctor(value: i32) -\u003e Self {\n        Self { field: value }\n    }\n    \n    #[jni]\n    fn get_field(\u0026self) -\u003e i32 {\n        self.field\n    }\n}\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eGenerated code\u003c/summary\u003e\n\n#### Rust code\n\n```rust\n#[no_mangle]\npub extern \"system\" fn Java_com_example_MyClass_00024MyClassNative_getField\u003c'local\u003e(\n    mut env: jni::JNIEnv\u003c'local\u003e,\n    object: jni::objects::JObject\u003c'local\u003e,\n) -\u003e jni::sys::jint {\n    use jni_bindgen::objects::traits::IntoJNIResult;\n    // Omitted: Retrieve this\n    let res = this.get_field();\n    res as jni::sys::jint\n}\n\n#[no_mangle]\npub extern \"system\" fn Java_com_example_MyClass_00024MyClassNative_ctor\u003c'local\u003e(\n    mut env: jni::JNIEnv\u003c'local\u003e,\n    class: jni::objects::JClass\u003c'local\u003e,\n    j_arg_0: jni::sys::jint,\n) -\u003e jni::sys::jlong {\n    use jni_bindgen::objects::traits::IntoJNIResult;\n    let arg_0 = j_arg_0 as i32;\n    let res = MyClass::ctor(arg_0);\n    Box::into_raw(Box::new(res)) as jni::sys::jlong\n}\n```\n\n#### Java code\n\n```java\npackage com.example;\n\nimport com.github.markusjx.jnibindgen.NativeClass;\nimport com.github.markusjx.jnibindgen.NativeClassImpl;\n\npublic class MyClass implements NativeClassImpl\u003cMyClass.MyClassNative\u003e {\n    private final MyClassNative inner;\n\n    public MyClass(int value) {\n        inner = new MyClassNative(value, this);\n    }\n\n    public int getField() {\n        return inner.getField();\n    }\n\n    public static long getTypeHash() {\n        return MyClassNative.getTypeHash();\n    }\n\n    @Override\n    public MyClassNative getInner() {\n        return inner;\n    }\n\n    public static class MyClassNative extends NativeClass {\n        private MyClassNative(int value, Object referent) {\n            super(ctor(value), referent);\n        }\n\n        private native int getField();\n\n        private static native void drop(long self);\n\n        private static native long getTypeHash();\n\n        private static native long ctor(int value);\n\n        @Override\n        protected void destruct() {\n            drop(this.ptr);\n        }\n    }\n}\n```\n\n\u003c/details\u003e\n\n### Import an interface from Java\n\n```rust\nuse jni_bindgen::objects::traits::{FromJNI, ObjectFromJNI};\nuse jni_bindgen::jni;\nuse jni::JNIEnv;\n\n#[jni(package = \"com.example\")]\ntrait MyInterface {\n    fn do_something(\u0026self, env: \u0026mut JNIEnv, value: i32) -\u003e jni_bindgen::Result\u003ci32\u003e;\n}\n\nstruct MyStruct;\n\n#[jni(package = \"com.example\")]\nimpl MyStruct {\n    #[jni]\n    fn use_do_something\u003c'a\u003e(\n        env: \u0026mut JNIEnv,\n        my_interface: Box\u003cdyn MyInterface + 'a\u003e,\n        value: i32,\n    ) -\u003e jni_bindgen::Result\u003ci32\u003e {\n        my_interface.do_something(env, value)\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusjx%2Fjni-bindgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkusjx%2Fjni-bindgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusjx%2Fjni-bindgen/lists"}