Skip to content

Commit 8bd95a5

Browse files
authored
[pigeon] Replace full JNI bindings with stubs for API classes that aren't needed (flutter#13173)
The generated JNIgen config bound both the Kotlin interface and the `Registrar` for each HostApi, but the generated Dart only calls the `Registrar`, so the interface bindings were dead code. HostApis now list only the `Registrar`. FlutterApi interfaces are still bound, since Dart implements them. The test plugin's `.jni.dart` drops from 40,978 to 25,520 lines (−37.7%), and the `Registrar` bindings are byte-identical. Also removes the HostApi entry from the FFIgen config. The Swift protocol isn't `@objc`, so FFIgen never saw it, and the generated `.ffi.dart` is unchanged. Fixes flutter#193882 ## 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.
1 parent 36d0427 commit 8bd95a5

12 files changed

Lines changed: 279 additions & 16530 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.9
2+
3+
* [kotlin] Stops generating unused JNI bindings for host API interfaces, significantly
4+
reducing the size of the generated `.jni.dart` file.
5+
16
## 29.0.8
27

38
* [swift] Adds validation errors for code that Swift FFI doesn't support:

‎packages/pigeon/example/native_interop_app/lib/src/native_interop_example.g.jni.dart‎

Lines changed: 18 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -221,131 +221,6 @@ final class $FlutterError$Type$ extends jni$_.JType<FlutterError> {
221221
String get signature => r'Ldev/flutter/pigeonnativeinteropapp/FlutterError;';
222222
}
223223

224-
/// from: `dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApi`
225-
extension type NativeInteropExampleApi._(jni$_.JObject _$this) implements jni$_.JObject {
226-
static final _class = jni$_.JClass.forName(
227-
r'dev/flutter/pigeonnativeinteropapp/NativeInteropExampleApi',
228-
);
229-
230-
/// The type which includes information such as the signature of this class.
231-
static const jni$_.JType<NativeInteropExampleApi> type = $NativeInteropExampleApi$Type$();
232-
233-
/// Maps a specific port to the implemented interface.
234-
static final core$_.Map<core$_.int, $NativeInteropExampleApi> _$impls = {};
235-
static jni$_.JObjectPtr _$invoke(
236-
core$_.int port,
237-
jni$_.JObjectPtr descriptor,
238-
jni$_.JObjectPtr args,
239-
) {
240-
return _$invokeMethod(
241-
port,
242-
jni$_.MethodInvocation.fromAddresses(0, descriptor.address, args.address),
243-
);
244-
}
245-
246-
static final jni$_.Pointer<
247-
jni$_.NativeFunction<jni$_.JObjectPtr Function(jni$_.Int64, jni$_.JObjectPtr, jni$_.JObjectPtr)>
248-
>
249-
_$invokePointer = jni$_.Pointer.fromFunction(_$invoke);
250-
251-
static jni$_.Pointer<jni$_.Void> _$invokeMethod(core$_.int $p, jni$_.MethodInvocation $i) {
252-
try {
253-
final $d = $i.methodDescriptor.toDartString(releaseOriginal: true);
254-
final $a = $i.args;
255-
if ($d == r'doSomething()V') {
256-
_$impls[$p]!.doSomething();
257-
return jni$_.nullptr;
258-
}
259-
} catch (e) {
260-
return jni$_.ProtectedJniExtensions.newDartException(e);
261-
}
262-
return jni$_.nullptr;
263-
}
264-
265-
static void implementIn(jni$_.JImplementer implementer, $NativeInteropExampleApi $impl) {
266-
late final jni$_.RawReceivePort $p;
267-
$p = jni$_.RawReceivePort(($m) {
268-
if ($m == null) {
269-
_$impls.remove($p.sendPort.nativePort);
270-
$p.close();
271-
return;
272-
}
273-
final $i = jni$_.MethodInvocation.fromMessage($m);
274-
final $r = _$invokeMethod($p.sendPort.nativePort, $i);
275-
$i.args?.release();
276-
jni$_.ProtectedJniExtensions.returnResult($i.result, $r);
277-
});
278-
implementer.add(
279-
r'dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApi',
280-
$p,
281-
_$invokePointer,
282-
[if ($impl.doSomething$async) r'doSomething()V'],
283-
);
284-
final $a = $p.sendPort.nativePort;
285-
_$impls[$a] = $impl;
286-
}
287-
288-
factory NativeInteropExampleApi.implement($NativeInteropExampleApi $impl) {
289-
final $i = jni$_.JImplementer();
290-
implementIn($i, $impl);
291-
return $i.implement<NativeInteropExampleApi>();
292-
}
293-
}
294-
295-
extension NativeInteropExampleApi$$Methods on NativeInteropExampleApi {
296-
static final _id_doSomething = NativeInteropExampleApi._class.instanceMethodId(
297-
r'doSomething',
298-
r'()V',
299-
);
300-
301-
static final _doSomething =
302-
jni$_.ProtectedJniExtensions.lookup<
303-
jni$_.NativeFunction<
304-
jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>, jni$_.JMethodIDPtr)
305-
>
306-
>('globalEnv_CallVoidMethod')
307-
.asFunction<
308-
jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>, jni$_.JMethodIDPtr)
309-
>();
310-
311-
/// from: `public fun doSomething(): kotlin.Unit`
312-
void doSomething() {
313-
final _$$selfRef = reference;
314-
_doSomething(_$$selfRef.pointer, _id_doSomething.pointer).check();
315-
}
316-
}
317-
318-
abstract base mixin class $NativeInteropExampleApi {
319-
factory $NativeInteropExampleApi({
320-
required void Function() doSomething,
321-
core$_.bool doSomething$async,
322-
}) = _$NativeInteropExampleApi;
323-
324-
void doSomething();
325-
core$_.bool get doSomething$async => false;
326-
}
327-
328-
final class _$NativeInteropExampleApi with $NativeInteropExampleApi {
329-
_$NativeInteropExampleApi({required void Function() doSomething, this.doSomething$async = false})
330-
: _doSomething = doSomething;
331-
332-
final void Function() _doSomething;
333-
final core$_.bool doSomething$async;
334-
335-
void doSomething() {
336-
return _doSomething();
337-
}
338-
}
339-
340-
final class $NativeInteropExampleApi$Type$ extends jni$_.JType<NativeInteropExampleApi> {
341-
@jni$_.internal
342-
const $NativeInteropExampleApi$Type$();
343-
344-
@jni$_.internal
345-
@core$_.override
346-
String get signature => r'Ldev/flutter/pigeonnativeinteropapp/NativeInteropExampleApi;';
347-
}
348-
349224
/// from: `dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApiRegistrar`
350225
extension type NativeInteropExampleApiRegistrar._(jni$_.JObject _$this)
351226
implements jni$_.JObject, NativeInteropExampleApi {
@@ -518,3 +393,21 @@ final class $DefaultConstructorMarker$Type$ extends jni$_.JType<DefaultConstruct
518393
@core$_.override
519394
String get signature => r'Lkotlin/jvm/internal/DefaultConstructorMarker;';
520395
}
396+
397+
/// from: `dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApi`
398+
///
399+
/// WARNING: NativeInteropExampleApi is a stub. To generate bindings for this class, include
400+
/// dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApi in your config's classes list.
401+
///
402+
extension type NativeInteropExampleApi._(jni$_.JObject _$this) implements jni$_.JObject {
403+
static const jni$_.JType<NativeInteropExampleApi> type = $NativeInteropExampleApi$Type$();
404+
}
405+
406+
final class $NativeInteropExampleApi$Type$ extends jni$_.JType<NativeInteropExampleApi> {
407+
@jni$_.internal
408+
const $NativeInteropExampleApi$Type$();
409+
410+
@jni$_.internal
411+
@core$_.override
412+
String get signature => r'Ldev/flutter/pigeonnativeinteropapp/NativeInteropExampleApi;';
413+
}

‎packages/pigeon/example/native_interop_app/tool/pigeon/native_interop_example_ffigen_config.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ Future<void> main(List<String> args) async {
3838
'NativeInteropExamplePigeonInternalNull',
3939
'NativeInteropExamplePigeonTypedData',
4040
'NativeInteropExampleNumberWrapper',
41-
'NativeInteropExampleApi',
4241
'NativeInteropExampleApiSetup',
4342
'PigeonError',
4443
};

‎packages/pigeon/example/native_interop_app/tool/pigeon/native_interop_example_jnigen_config.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void main() async {
2121
input: Input(
2222
classes: [
2323
'dev.flutter.pigeonnativeinteropapp.FlutterError',
24-
'dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApi',
2524
'dev.flutter.pigeonnativeinteropapp.NativeInteropExampleApiRegistrar',
2625
],
2726
androidSdk: AndroidSdk(addGradleDeps: true, androidExample: Uri.directory('.')),

‎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.8';
19+
const String pigeonVersion = '29.0.9';
2020

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

‎packages/pigeon/lib/src/kotlin/jnigen_config_generator.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ class JnigenConfigGenerator extends Generator<InternalJnigenConfigOptions> {
125125
"'$packagePrefix${generatorOptions.kotlinOptions.errorClassName ?? 'FlutterError'}',",
126126
);
127127
for (final Api api in root.apis) {
128-
if (api is AstHostApi || api is AstFlutterApi) {
128+
if (api is AstFlutterApi) {
129129
indent.writeln("'$packagePrefix${api.name}',");
130130
indent.writeln("'$packagePrefix${api.name}Registrar',");
131+
} else if (api is AstHostApi) {
132+
indent.writeln("'$packagePrefix${api.name}Registrar',");
131133
}
132134
}
133135
for (final Class dataClass in root.classes) {

‎packages/pigeon/lib/src/swift/ffigen_config_generator.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ import 'package:swiftgen/swiftgen.dart';
150150
}
151151
for (final Api api in root.apis) {
152152
if (api is AstHostApi) {
153-
indent.writeln("'${api.name}',");
154153
indent.writeln("'${api.name}Setup',");
155154
}
156155
if (api is AstFlutterApi) {

0 commit comments

Comments
 (0)