Skip to content

Commit 2def459

Browse files
authored
[pigeon] update jni dependency to ^1.1.1 and remove workaround proguard rules (flutter#13464)
This should remove the need for manual proguard rules
1 parent 4dd0e3e commit 2def459

14 files changed

Lines changed: 54 additions & 132 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 29.0.12
2+
3+
* [kotlin] Warns after JNI code generation if the `jni` version constraint in `pubspec.yaml` allows versions below `1.1.1`.
4+
* Updates `jni` dependency to `^1.1.1` and removes manual Kotlin keep rules from native interop guides and migration skill now that `package:jni` includes them.
5+
16
## 29.0.11
27

38
* [swift] Warns after FFI code generation if the `objective_c` version constraint in `pubspec.yaml` does not match the generated `ObjCVersionCheck`.

‎packages/pigeon/example/native_interop_app/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
ffi: ^2.2.0
1111
flutter:
1212
sdk: flutter
13-
jni: ^1.1.0
13+
jni: ^1.1.1
1414
objective_c: ^9.6.2
1515

1616
dev_dependencies:

‎packages/pigeon/lib/src/generator_tools.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'generator.dart';
1616
/// The current version of pigeon.
1717
///
1818
/// This must match the version in pubspec.yaml.
19-
const String pigeonVersion = '29.0.11';
19+
const String pigeonVersion = '29.0.12';
2020

2121
/// Default plugin package name.
2222
const String defaultPluginPackageName = 'dev.flutter.pigeon';

‎packages/pigeon/lib/src/pigeon_lib_internal.dart‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,8 @@ String? checkObjectiveCPubspecVersion(InternalPigeonOptions options) {
27182718

27192719
/// Checks whether the `jni` dependency constraint in `pubspec.yaml`
27202720
/// matches the `JniVersionCheck(major, minor)` emitted by `jnigen` in the
2721-
/// generated `.jni.dart` file.
2721+
/// generated `.jni.dart` file (and is at least `1.1.1`, which includes the
2722+
/// required Android R8 keep rules).
27222723
///
27232724
/// Returns a warning message if the constraint allows incompatible versions, or
27242725
/// `null` if the constraint is compatible or cannot be checked.
@@ -2730,6 +2731,7 @@ String? checkJniPubspecVersion(InternalPigeonOptions options) {
27302731
versionCheckName: 'JniVersionCheck',
27312732
packageName: 'jni',
27322733
bindingTypeLabel: 'JNI',
2734+
minimumVersionFloor: Version(1, 1, 1),
27332735
);
27342736
}
27352737

@@ -2740,6 +2742,7 @@ String? _checkGeneratedBindingsPubspecVersion({
27402742
required String versionCheckName,
27412743
required String packageName,
27422744
required String bindingTypeLabel,
2745+
Version? minimumVersionFloor,
27432746
}) {
27442747
final String? dartOutPath = options.dartOptions?.dartOut;
27452748
if (dartOutPath == null || dartOutPath.isEmpty) {
@@ -2770,8 +2773,12 @@ String? _checkGeneratedBindingsPubspecVersion({
27702773

27712774
final int major = int.parse(match.group(1)!);
27722775
final int minor = int.parse(match.group(2)!);
2773-
final requiredMinVersion = Version(major, minor, 0);
2774-
final nextMajorVersion = Version(major + 1, 0, 0);
2776+
final versionCheckMinVersion = Version(major, minor, 0);
2777+
final Version requiredMinVersion =
2778+
(minimumVersionFloor != null && versionCheckMinVersion < minimumVersionFloor)
2779+
? minimumVersionFloor
2780+
: versionCheckMinVersion;
2781+
final nextMajorVersion = Version(requiredMinVersion.major + 1, 0, 0);
27752782

27762783
final String? pubspecPath = _findTargetPubspecPath(
27772784
appDirectory: appDirectory,
@@ -2833,9 +2840,9 @@ String? _checkGeneratedBindingsPubspecVersion({
28332840

28342841
if (!allowsValidVersion || allowsLowerVersion || allowsHigherMajorVersion) {
28352842
return 'Generated $bindingTypeLabel bindings in "${path.basename(bindingsDartFile.path)}" require '
2836-
'package:$packageName ^$major.$minor.0 (from $versionCheckName($major, $minor)), '
2843+
'package:$packageName ^$requiredMinVersion (from $versionCheckName($major, $minor)), '
28372844
'but "$pubspecPath" specifies "$packageName: $rawConstraint". '
2838-
'Consider updating pubspec.yaml to "$packageName: ^$major.$minor.0".';
2845+
'Consider updating pubspec.yaml to "$packageName: ^$requiredMinVersion".';
28392846
}
28402847
} catch (_) {
28412848
return null;

‎packages/pigeon/native_interop_guide.md‎

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Using Native Interop in pigeon follows the standard pigeon workflow, with a few
3636
3. **Configure Options**: Configure `kotlinOptions.useJni` or `swiftOptions.useFfi` in your `PigeonOptions` (see [Step 2: Configure Pigeon Options](#step-2-configure-pigeon-options) below).
3737
4. **Prerequisites**: Ensure your local environment meets the toolchain prerequisites for `jnigen` and `ffigen` (see [Section 3: Prerequisites](#3-prerequisites) below).
3838
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). 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 -->
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).
4040
7. **Implement and Call**: Implement the generated protocol/class interface in your native codebase and call the generated Dart methods from your Flutter application.
4141

4242
---
@@ -201,36 +201,6 @@ When implementing Native Interop host APIs directly in an application target rat
201201
```
202202
Calling `register(api:)` in native code also prevents the Xcode linker (`-dead_strip`) from stripping the setup class from the compiled binary.
203203

204-
### Step 5: Android Release Build Configuration (JNI)
205-
206-
<!-- 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-
234204
---
235205

236206
## 5. Troubleshooting Automated Generation
@@ -299,7 +269,6 @@ If native interop calls work in debug builds but fail in release builds with `ja
299269
- **Cause**: R8 removed or renamed a class or member that JNI looks up by name.
300270
- **Solution**:
301271
- 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 -->
303272
- To see what R8 removed or renamed, check `build/app/outputs/mapping/release/mapping.txt` in the application directory.
304273

305274
---

‎packages/pigeon/native_interop_migration_guide.md‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,7 @@ Ensure your build system is configured appropriately:
186186

187187
---
188188

189-
## 6. Android Release Build Configuration (JNI)
190-
191-
<!-- 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.
208-
209-
---
210-
211-
## 7. Environment Prerequisites & Tooling Versions
189+
## 6. Environment Prerequisites & Tooling Versions
212190

213191
To use Native Interop and its external code generators:
214192
- **`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.

‎packages/pigeon/platform_tests/shared_test_plugin_code/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies:
1919
sdk: flutter
2020
integration_test:
2121
sdk: flutter
22-
jni: ^1.1.0
22+
jni: ^1.1.1
2323
meta: ^1.17.0
2424
mockito: ^5.4.4
2525
objective_c: ^9.6.2

‎packages/pigeon/platform_tests/test_plugin/android/build.gradle.kts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ android {
4343

4444
defaultConfig {
4545
minSdk = 24
46-
// TODO(tarrinneal): Remove, along with consumer-rules.pro, once package:jni
47-
// ships the Kotlin keep rules: https://github.com/dart-lang/native/issues/3732
48-
consumerProguardFiles("consumer-rules.pro")
4946
}
5047

5148
testOptions {

‎packages/pigeon/platform_tests/test_plugin/android/consumer-rules.pro‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎packages/pigeon/platform_tests/test_plugin/example/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies:
99
ffi: ^2.2.0
1010
flutter:
1111
sdk: flutter
12-
jni: ^1.1.0
12+
jni: ^1.1.1
1313
objective_c: ^9.6.2
1414
pub_semver: ^2.2.0
1515
shared_test_plugin_code:

0 commit comments

Comments
 (0)