Search code examples
androidkotlincapacitor

How can I call a suspend function in Kotlin from capacitor?


I have an issue where I have native code written in Kotlin that I need to call as a Capacitor Pugin. I can implement plugins to call normal functions just fine. However if I call a suspend function the app crashes and I get the following error: java.lang.RuntimeException: java.lang.IllegalArgumentException: Wrong number of arguments; expected 2, got 1

I tried calling a capacitor plugin function like so:

const {value} = await realmPlugin.initialize({ appId: 'myAppId' });

I expect to call my function and get the expected return, instead the app crashes and I get the error:

java.lang.RuntimeException: java.lang.IllegalArgumentException: Wrong number of arguments; expected 2, got 1


Solution

  • You can try to create a wrapper in Kotlin with the call of your suspend function inside a coroutine scope. Just for example, with the GlobalScope:

    fun initializePlugin(...) =
      GlobalScope.launch {
        realmPlugin.initialize(...)
      }