tree: 4b8ff61baaca575958e460054bc7327a9dc9d710 [path history] [tgz]
  1. android/
  2. example/
  3. lib/
  4. src/
  5. .gitignore
  6. .metadata
  7. analysis_options.yaml
  8. jnigen.yaml
  9. pubspec.yaml
  10. 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:

flutter pub run jnigen --config jnigen.yaml # run from kotlin_plugin project root 

The example/ app must be built at least once in release mode (eg flutter build apk) before running jnigen. This is the equivalent of Gradle Sync in Android Studio, and enables jnigen to run a Gradle stub and determine release build's classpath, which contains the paths to relevant dependencies. Therefore a build must have been run after cleaning build directories, or updating Java dependencies. This is a known complexity of the Gradle build system, and if you know a solution, please contribute to issue discussion at #33.

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.

For Kotlin coroutines to work, Jni.initDLApi() must be run first.

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.