blob: 34cb743206d74acbd6fe63d175de3701f628b8dd [file] [log] [blame] [view] [edit]
# In-App Java Example
This example shows how to write custom java code in `android/app/src` and call
it using bindings generated by `jnigen`.
### How to run this example:
- Run `flutter run` to run the app.
- To regenerate bindings after changing Java code, run
`dart run tool/jnigen.dart`. This requires at least one APK build to have
been run before, so that `jnigen` can obtain classpaths of Android Gradle
libraries. Therefore, run `flutter build apk` once 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
`jnigen`.
- Place Java code in a suitable package folder under the `android/` subproject
of the flutter app.
- Create a `jnigen` configuration script like `tool/jnigen.dart` in this
example.
- Generate bindings by running that script with `dart run`.
- To prevent tree shaking of your custom classes (which are always accessed
reflectively in JNI) use either the `@Keep` annotation
(`androidx.annotation.Keep`) for code written in the application itself, or
a [proguard-rules file](https://github.com/dart-lang/native/blob/main/pkgs/jnigen/example/in_app_java/android/app/proguard-rules.pro)
for external libraries.
- Build and run the app.