You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
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
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.
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.
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.
The wrapper library handles error checking, global reference generation, and method signature generation (e.g. (I)J for isPrime), and makes function calls typesafe.
Activity
bleege commentedon Feb 22, 2016
We'll also want to investigate how using JavaCPP could make this better or worse.
https://github.com/bytedeco/javacpp
/cc @zugaldia @tmpsantos
zugaldia commentedon Feb 22, 2016
I fully support this one and I'm happy to help with the effort.
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:
tmpsantos commentedon Feb 22, 2016
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.
bleege commentedon Feb 22, 2016
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.
jfirebaugh commentedon Feb 22, 2016
I don't think we should try to generate the bindings.
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:
The wrapper library handles error checking, global reference generation, and method signature generation (e.g.
(I)JforisPrime), and makes function calls typesafe.