Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Update JNI bindings to use modern C++ practices #4043

Closed

Description

@jfirebaugh

The JNI bindings should:

  • Use RAII class wrappers for all JNI resources, e.g. GlobalRefs, thread attachments, ...?
  • Use method wrappers that do error checking and throw exceptions
  • (stretch goal) Use type-safe method wrappers for Call*Method invocations

These wrappers should be designed to avoid the items on this list, of which we are guilty of several:

http://android-developers.blogspot.com/2011/11/jni-local-reference-changes-in-ics.html

Activity

  1. added
    Android
    on Feb 19, 2016
  2. bleege commented on Feb 22, 2016

    @bleege
    Contributor

    We'll also want to investigate how using JavaCPP could make this better or worse.

    https://github.com/bytedeco/javacpp

    /cc @zugaldia @tmpsantos

  3. zugaldia commented on Feb 22, 2016

    @zugaldia
    Member

    I fully support this one and I'm happy to help with the effort.

    We'll also want to investigate how using JavaCPP could make this better or worse.

    This is the library used by OpenCV, FFmpeg, and Apple Music on Android, so it has a strong industry support.

    I know @jfirebaugh is looking into a few more options. Just adding two more that have been discussed in the past at some point:

    • Djinni, "a tool for generating cross-language type declarations and interface bindings", by Dropbox. It supports Java and Objective-C, and Python is experimental.
    • SWIG, this one mentioned by @tmpsantos supporting a larger number of languages, not only Java.
  4. tmpsantos commented on Feb 22, 2016

    @tmpsantos
    Contributor

    Regardless of what tool we use, we should definitely try to generate the bindings. I know that not everybody is a big fan of SWIG, but I just mentioned it because I have done some quick python bindings with it and it worked just fine. The benefit is we would get other languages for free.

    If we find something more specialized to Java that will deliver better interfaces and performance, I'm all for it too.

  5. bleege commented on Feb 22, 2016

    @bleege
    Contributor

    Agreed all around. The current setup of crafting artisanal JNI bindings is a huge PITA not to mention tends to lead to undesirable code.

    FWIW, @kkaefer and I looked at Djinni last summer and passed on it. Still, it'd be worth a second look to see what's changed since then.

  6. jfirebaugh commented on Feb 22, 2016

    @jfirebaugh
    ContributorAuthor

    I don't think we should try to generate the bindings.

    • Every binding generator I've seen produced reams of low-quality code, and resulted in bindings with bad ergonomics, bindings that don't follow the conventions of the target language, or bindings that leaked underlying abstraction details.
    • For all practical purposes, generation enforces a one-to-one mapping of source to target. If that's not what you want, you have to figure out how to bend the generator to your will, or (more likely) hand code another wrapping layer in the target language.

    We very much do not want to expose our C++ classes one-for-one in the Android SDK -- every class should be considered individually, and tailored to the general conventions of the Android world and the specific precedents of other Android mapping SDKs. We're better off writing a single binding layer by hand. But we should be doing it with a safe, modern C++ interface, not raw JNI calls. I'm working on enabling this, by writing a C++14 wrapper library for the JNI API.

    With it, writing bindings looks like this:

    /*
      package com.mapbox;
      public class Foo {
        public static void staticMethod();
        public void isPrime(int value);
      }
    */
    
    struct Foo { static constexpr auto path = "com/mapbox/Foo"; };
    jni::Class<Foo> fooClass { env };
    jni::Constructor<Foo> newFoo { foo };
    jni::StaticMethod<Foo, void ()> staticMethod { fooClass, "staticMethod" };
    jni::Method<Foo, bool (jint)> isPrime { fooClass, "isPrime" };
    
    // Call static method:
    staticMethod();
    
    // Create instance:
    jni::Object<Foo> foo = newFoo();
    
    // Call instance method:
    assert(isPrime(foo, 1234567) == false);
    

    The wrapper library handles error checking, global reference generation, and method signature generation (e.g. (I)J for isPrime), and makes function calls typesafe.

  7. self-assigned this
    on Mar 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Android

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions