1

It's unclear how Dart VM loads a C/C++ library through FFI.

final dylib = ffi.DynamicLibrary.open(libraryPath);

Some C++ libraries require a java context to work on Android. For example, Juce library is initialized through

public class Java
{
    static
    {
        System.loadLibrary ("juce_jni");
    }

    public native static void initialiseJUCE (Context appContext);
}

However, if I want to call Juce from Dart using FFI, I cannot load it from Dart as I'd not be able to pass a java context. If I load the library from Java, then I cannot use Dart FFI.

Is there a way to load the same lib in Java and Dart, start it in Java but then interface it with Dart FFI?

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
PPP
  • 1,279
  • 1
  • 28
  • 71
  • You have to find a way to talk to juce directly. There seem to be multiple ways to do that, including just creating a shared library - see https://docs.juce.com/master/tutorial_choosing_projucer_template.html If you're working on iOS there's obviously no Java so you must be able to talk directly to it in C. It's understandable that the default Android/Juce project expects to be called form Java, but look around to see if there are other templates, e.g. for calling Juce directly from NDK-based code. – Richard Heap May 14 '21 at 22:06
  • @RichardHeap I'm already creating a shared library, using CMake. I've read the article but in every case it generates code that still needs calling `InitialiseJUCE` – PPP May 15 '21 at 01:55
  • Just an idea, you might be able to use `MethodChannel`, you could initialise the Native Library via the Java/Kotlin side & then handle the communication of methods via the same `MethodChannel`. But then this approach would make FFI useless here. – Darshan May 22 '21 at 13:14
  • Your shared library needs to expose a method to dart that calls initialiseJUCE, you then pass a structure back to dart that holds the context. Each call you make from dart passes the structure which you can then pass to the juce call. – Brett Sutton Jun 06 '21 at 22:43
  • @BrettSutton but initializeJUCE requires a java context in its argument – PPP Jun 10 '21 at 19:54

0 Answers0