You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit bbc5975
Browse filesBrowse the repository at this point in the historyBrowse files
Flutter minifies Android release builds with R8, which removes or renames classes that it doesn't see being used. JNI looks up classes and members by name at runtime, from Dart code that R8 can't see. As a result, native interop calls that work in debug builds fail in release builds with `ClassNotFoundException` or `NoSuchMethodError`.
This PR:
- **Kotlin generator:** When `useJni` is set, adds `@Keep` to every generated class that JNI reaches by name:
- data classes, including sealed classes;
- enums and their companion objects;
- the FlutterApi interface and its Registrar;
- the error class.
The HostApi interface and Registrar already had `@Keep`. The test plugin and example app are regenerated.
- **Docs:** Documents keep rules for six Kotlin standard library classes. `package:jni` and the generated Dart code look these up by name to call and implement `suspend` (`@async`) methods. Library classes can't be annotated, so these rules are needed until `package:jni` ships them (dart-lang/native#3732).
- Updated `native_interop_guide.md`, `native_interop_migration_guide.md` and the migration skill.
- Each spot has a TODO linking that issue.
- **CI:** Adds an `android_kotlin_native_interop_release_tests` suite to the `Linux_android custom_package_tests` shard. It runs the native interop integration tests in a minified release build.
- `flutter test` and `flutter drive` don't support release builds on Android, so the suite uses `flutter run --release` and reads the test summary from its output.
- The test plugin adds the six Kotlin rules in `consumer-rules.pro`, with a TODO to delete them once dart-lang/native#3732 is fixed.
- It deliberately has no rule for its own package, so a missing `@Keep` on any generated class still fails the suite.
Results for the native interop suite (339 tests) in a minified release build on an arm64 emulator:
| | Passed | Failed | Didn't run |
|---|---|---|---|
| Before (old generated code, with the Kotlin rules) | 106 | 70 | 163 |
| This PR, without the Kotlin rules | 210 | 129 | 0 |
| This PR | 339 | 0 | 0 |
- **Before:** 163 tests didn't run. R8 had removed the FlutterApi interface, so registering the FlutterApi threw `ClassNotFoundException` during test setup.
- **Without the Kotlin rules:** every failure is a call through a `suspend` function. Each one fails with `NoSuchMethodError`, because R8 renames `kotlin.coroutines.Continuation`.
Fixesflutter/flutter#193880
Temporary fix for dart-lang/native#3732
## Pre-Review Checklist
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.
[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Copy file name to clipboardExpand all lines: packages/pigeon/example/native_interop_app/android/app/src/main/kotlin/dev/flutter/pigeonnativeinteropapp/NativeInteropExample.g.kt
Copy file name to clipboardExpand all lines: packages/pigeon/native_interop_guide.md
+40-1Lines changed: 40 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Using Native Interop in pigeon follows the standard pigeon workflow, with a few
36
36
3.**Configure Options**: Configure `kotlinOptions.useJni` or `swiftOptions.useFfi` in your `PigeonOptions` (see [Step 2: Configure Pigeon Options](#step-2-configure-pigeon-options) below).
37
37
4.**Prerequisites**: Ensure your local environment meets the toolchain prerequisites for `jnigen` and `ffigen` (see [Section 3: Prerequisites](#3-prerequisites) below).
38
38
5.**Run Code Generation**: Run the `pigeon` tool to generate the native bridge code and interop bindings (see [Step 3: Run Code Generation](#step-3-run-code-generation) below).
39
-
6.**Configure Build Systems**: For Swift FFI, configure CocoaPods or Swift Package Manager (SwiftPM) to compile the intermediate Objective-C bridge files (see [Step 4: iOS/macOS Build System Configuration (FFI)](#step-4-iosmacos-build-system-configuration-ffi) below).
39
+
6.**Configure Build Systems**: For Swift FFI, configure CocoaPods or Swift Package Manager (SwiftPM) to compile the intermediate Objective-C bridge files (see [Step 4: iOS/macOS Build System Configuration (FFI)](#step-4-iosmacos-build-system-configuration-ffi) below). For Kotlin JNI, add keep rules so that Android release builds work (see [Step 5: Android Release Build Configuration (JNI)](#step-5-android-release-build-configuration-jni) below). <!-- TODO(tarrinneal): Remove this JNI sentence once package:jni ships these keep rules: https://github.com/dart-lang/native/issues/3732 -->
40
40
7.**Implement and Call**: Implement the generated protocol/class interface in your native codebase and call the generated Dart methods from your Flutter application.
41
41
42
42
---
@@ -201,6 +201,36 @@ When implementing Native Interop host APIs directly in an application target rat
201
201
```
202
202
Calling `register(api:)` in native code also prevents the Xcode linker (`-dead_strip`) from stripping the setup class from the compiled binary.
<!-- TODO(tarrinneal): Remove this section once package:jni ships these keep rules: https://github.com/dart-lang/native/issues/3732 -->
207
+
208
+
Flutter enables R8 for Android release builds by default. R8 removes, renames, and merges classes and members that it doesn't see being used. JNI looks up classes and members by name at runtime, so everything that JNI reaches must keep its original name.
209
+
210
+
Pigeon adds `@Keep` to every generated Kotlin class that the generated Dart code reaches through JNI, so the generated code needs no extra rules. However, `package:jni` and the generated Dart code also look up several Kotlin standard library classes by name to call Kotlin `suspend` functions (`@async` methods) and to implement them from Dart. Library classes can't be annotated, so until `package:jni` includes rules for these classes, you must add them yourself:
211
+
212
+
```text
213
+
# Kotlin classes that package:jni and the generated Dart code look up by name.
214
+
-keep class kotlin.Unit { *; }
215
+
-keep class kotlin.Result { *; }
216
+
-keep class kotlin.Result$Failure { *; }
217
+
-keep class kotlin.coroutines.Continuation { *; }
218
+
-keep class kotlin.coroutines.intrinsics.CoroutineSingletons { *; }
219
+
-keep class kotlin.coroutines.intrinsics.IntrinsicsKt { *; }
220
+
```
221
+
222
+
***Plugins**: Add the rules to a file in the plugin's `android/` directory, such as `consumer-rules.pro`, and list it in `consumerProguardFiles` in the plugin's `android/build.gradle` or `android/build.gradle.kts`. R8 then applies the rules to every application that uses the plugin:
223
+
```gradle
224
+
android {
225
+
defaultConfig {
226
+
consumerProguardFiles("consumer-rules.pro")
227
+
}
228
+
}
229
+
```
230
+
***Applications**: Add the rules to `android/app/proguard-rules.pro`. Flutter applies this file to release builds when it exists.
231
+
232
+
Debug and profile builds are not minified, so test a release build (for example, with `flutter run --release`) to verify the configuration.
233
+
204
234
---
205
235
206
236
## 5. Troubleshooting Automated Generation
@@ -263,6 +293,15 @@ If an iOS or macOS SwiftPM build fails because `<plugin_name>_objc_gen.o` cannot
263
293
-**Cause**: `Package.swift` defines a `<plugin_name>_objc_gen` target, but `ffigen` did not generate a `.m` implementation file in `Sources/<plugin_name>_objc_gen/` (because the Pigeon schema has no async callbacks, closures, or `@FlutterApi` methods requiring Objective-C trampolines). Without a `.m` source file, SwiftPM does not produce an object file for the target. (Note: any `.o` file produced inside `Sources/<plugin_name>_objc_gen/` during `ffigen` execution is a temporary `swiftc` artifact and must not be committed or used.)
264
294
-**Solution**: Remove the `<plugin_name>_objc_gen` target and its dependency entry from `Package.swift`.
265
295
296
+
### 5.7 `ClassNotFoundException` or `NoSuchMethodError` in Android Release Builds (JNI)
297
+
298
+
If native interop calls work in debug builds but fail in release builds with `java.lang.ClassNotFoundException` or `java.lang.NoSuchMethodError`:
299
+
-**Cause**: R8 removed or renamed a class or member that JNI looks up by name.
300
+
-**Solution**:
301
+
- If the error names a class generated by Pigeon, regenerate your Pigeon output with Pigeon 29.0.7 or later, which adds `@Keep` to every generated class that JNI reaches.
302
+
- If the error names a class in the `kotlin` package (for example, `kotlin/coroutines/Continuation` in the signature of a `suspend` method), add the keep rules from [Step 5](#step-5-android-release-build-configuration-jni). <!-- TODO(tarrinneal): Remove this bullet once package:jni ships these keep rules: https://github.com/dart-lang/native/issues/3732 -->
303
+
- To see what R8 removed or renamed, check `build/app/outputs/mapping/release/mapping.txt` in the application directory.
<!-- TODO(tarrinneal): Remove this section, and renumber the next one, once package:jni ships these keep rules: https://github.com/dart-lang/native/issues/3732 -->
192
+
193
+
Flutter enables R8 for Android release builds by default, and R8 removes or renames classes that JNI looks up by name. Pigeon adds `@Keep` to the generated Kotlin classes that JNI reaches, but `package:jni` and the generated Dart code also look up Kotlin standard library classes by name to call and implement `suspend` functions. Until `package:jni` includes rules for these classes, add them to a `consumer-rules.pro` file in your plugin's `android/` directory:
194
+
195
+
```text
196
+
# Kotlin classes that package:jni and the generated Dart code look up by name.
197
+
-keep class kotlin.Unit { *; }
198
+
-keep class kotlin.Result { *; }
199
+
-keep class kotlin.Result$Failure { *; }
200
+
-keep class kotlin.coroutines.Continuation { *; }
201
+
-keep class kotlin.coroutines.intrinsics.CoroutineSingletons { *; }
202
+
-keep class kotlin.coroutines.intrinsics.IntrinsicsKt { *; }
203
+
```
204
+
205
+
Then add `consumerProguardFiles("consumer-rules.pro")` to `android.defaultConfig` in the plugin's `android/build.gradle` or `android/build.gradle.kts`, so that every app using the plugin applies the rules. For a standalone application, add the rules to `android/app/proguard-rules.pro` instead.
206
+
207
+
Debug builds are not minified, so test a release build (for example, by running the example app with `flutter run --release`) after migrating. See [Step 5 of the Native Interop Guide](./native_interop_guide.md#step-5-android-release-build-configuration-jni) for details.
To use Native Interop and its external code generators:
192
214
-**`jnigen` 1.0.0 or later**: Earlier versions of JNIgen cannot parse metadata from newer Kotlin compilers and fail with `IllegalArgumentException: Provided Metadata instance has version ... while maximum supported version is ...`. `jnigen` 1.0.0 supports Kotlin metadata up to 2.4, and uses the JDK bundled with Flutter by default.
Copy file name to clipboardExpand all lines: packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/NativeInteropTests.gen.kt
0 commit comments