facebookincubator/fbjni

Passing a Java interface to C++

Closed this issue · 2 comments

Issue description

Hi! I'm trying to pass an instance of a Java interface to C++.

The interface I'm trying to use is the ImageProxy interface from AndroidX/CameraX, my C++ declaration for the binding/interface looks like this:

struct JImageProxy : public facebook::jni::JavaClass<JImageProxy> {
  static constexpr auto kJavaDescriptor = "Landroidx.camera.core/ImageProxy;";
};

and my C++ method looks like this:

void callback(jni::alias_ref<JImageProxy::javaobject> image);

and the Java (Kotlin) native func decl looks like this:

  private external fun callback(image: ImageProxy)

but my app crashes with the following exception:

2021-06-22 14:32:17.108 31506-31506/? E/.camera.exampl: Failed to register native method com.mrousavy.camera.CameraView.frameProcessorCallback(Landroidx.camera.core/ImageProxy;)V in /data/app/com.mrousavy.camera.example-22C3je6eP10VxqF5O-UTMw==/base.apk!classes4.dex
2021-06-22 14:32:17.109 31506-31506/? E/log: error java.lang.NoSuchMethodError: no static or non-static method "Lcom/mrousavy/camera/CameraView;.frameProcessorCallback(Landroidx.camera.core/ImageProxy;)V"

Does anyone know why this keeps happening? Did I declare something wrong? Does fbjni not support Java interfaces?

System Info

MacOS Big Sur 11.4, latest fbjni

If I use jobject / Any instead of the typed JImageProxy / ImageProxy way and do this:

if (boxedImage->isInstanceOf(JImageProxy::javaClassLocal())) {
   __android_log_write(ANDROID_LOG_INFO, TAG, "It's a JImageProxy.");
}

this gets logged. Not sure what I'm doing wrong in the code above...

ah stupid me 🤦‍♂️ forgot to use the / in the java descriptor. it works now!