tree: 3bca88e0457438ce5b52ae0c1d9a9cfe15e81de9
  1. android/
  2. lib/
  3. src/
  4. test/
  5. tool/
  6. .gitignore
  7. .metadata
  8. analysis_options.yaml
  9. jnigen.yaml
  10. pubspec.yaml
  11. README.md
pkgs/jni_gen/examples/in_app_java/README.md

In-App Java Example

This example shows how to write custom java code in android/app/src and call it using jni_gen generated bindings.

How to run this example:

  • Run flutter run to run the app.

  • To regenerate bindings after changing Java code, run flutter pub run jni_gen --config jnigen.yaml. This requires at least one APK build to have been run before, so that it's possible for jni_gen to obtain classpaths of Android Gradle libraries. Therefore, once run flutter build apk before generating bindings for the first time, or after a flutter clean.

General steps

These are general steps to integrate Java code into a flutter project using jni_gen.

  • Write Java code in suitable package folder, under android/ subproject of the flutter app.

  • Create A jnigen config like jnigen.yaml in this example.

  • Generate bindings using jnigen config.

  • Add an externalNativeBuild to gradle script (see android/app/build.gradle in this example).

  • Add proguard rules to exclude your custom classes from tree shaking, since they are always accessed reflectively in JNI.

  • Build and run the app.