tree: 507ad460e53949bcf4246e076883896e67033ae8
  1. android/
  2. example/
  3. lib/
  4. .metadata
  5. analysis_options.yaml
  6. jnigen.yaml
  7. pubspec.yaml
  8. README.md
pkgs/jnigen/example/kotlin_plugin/README.md

kotlin_plugin

This example generates bindings for a Kotlin-based library. It showcases the conversion of suspend fun in Kotlin to async functions in Dart.

The command to regenerate JNI bindings is:

dart run jnigen --config jnigen.yaml # run from kotlin_plugin project root 

The example/ app must have its dependencies resolved (eg flutter pub get) before running JNIgen.

Note that jnigen.yaml of this example contains the option suspend_fun_to_async: true. This will generate async method bindings from Kotlin's suspend funs.

Creating a new Kotlin plugin

Running flutter create --template=plugin_ffi --platform=android kotlin_plugin creates the skeleton of an Android plugin.

In order to use Kotlin, build.gradle must be modified in the following way:

apply plugin: 'com.android.library'
// Add the line below:
apply plugin: 'kotlin-android'
android {
    // Add the following block
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    // ...
}

Now the Kotlin code in src/main/kotlin is included in the plugin.