Skip to content

Commit a56c297

Browse files
authored
Feat: Allow manual init of the Native SDK (#765)
1 parent 7216a3f commit a56c297

9 files changed

+60
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix serialization of threads (#844)
6+
- Feat: Allow manual init of the Native SDK (#765)
67

78
## 6.6.0-alpha.1
89

@@ -67,7 +68,7 @@
6768
* Fix: Disable log by default in debug mode (#753)
6869
* [Dio] Ref: Replace FailedRequestAdapter with FailedRequestInterceptor (#728)
6970
* Fix: Add missing return values - dart analyzer (#742)
70-
* Feature: Add `DioEventProcessor` which improves DioError crash reports (#718)
71+
* Feat: Add `DioEventProcessor` which improves DioError crash reports (#718)
7172
* Fix: Do not report duplicated packages and integrations (#760)
7273
* Feat: Allow manual init of the Native SDK or no Native SDK at all (#765)
7374

dart/lib/src/enricher/io_enricher_event_processor.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class IoEnricherEventProcessor extends EventProcessor {
2424
FutureOr<SentryEvent> apply(SentryEvent event, {dynamic hint}) {
2525
// If there's a native integration available, it probably has better
2626
// information available than Flutter.
27+
2728
final os = _options.platformChecker.hasNativeIntegration
2829
? null
2930
: _getOperatingSystem(event.contexts.operatingSystem);
31+
3032
final device = _options.platformChecker.hasNativeIntegration
3133
? null
3234
: _getDevice(event.contexts.device);

dart/lib/src/sentry_options.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ class SentryOptions {
210210
/// breadcrumbs.
211211
bool enablePrintBreadcrumbs = true;
212212

213-
/// If [platformChecker] is provided, it is used get the envirnoment.
213+
/// If [platformChecker] is provided, it is used get the environment.
214214
/// This is useful in tests. Should be an implementation of [PlatformChecker].
215215
PlatformChecker platformChecker = PlatformChecker();
216216

217-
/// If [environmentVariables] is provided, it is used get the envirnoment
217+
/// If [environmentVariables] is provided, it is used get the environment
218218
/// variables. This is useful in tests.
219219
EnvironmentVariables environmentVariables = EnvironmentVariables.instance();
220220

flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import io.flutter.plugin.common.MethodChannel
1111
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1212
import io.flutter.plugin.common.MethodChannel.Result
1313
import io.sentry.HubAdapter
14-
import io.sentry.Sentry
1514
import io.sentry.SentryEvent
1615
import io.sentry.SentryLevel
1716
import io.sentry.android.core.ActivityFramesTracker
@@ -270,7 +269,7 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
270269
}
271270

272271
private fun closeNativeSdk(result: Result) {
273-
Sentry.close()
272+
HubAdapter.getInstance().close()
274273
framesTracker?.stop()
275274
framesTracker = null
276275

flutter/lib/src/default_integrations.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class _LoadContextsIntegrationEventProcessor extends EventProcessor {
325325
}
326326
}
327327

328-
/// Enables Sentry's native SDKs (Android and iOS)
328+
/// Enables Sentry's native SDKs (Android and iOS) with options.
329329
class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
330330
NativeSdkIntegration(this._channel);
331331

@@ -335,6 +335,9 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
335335
@override
336336
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
337337
_options = options;
338+
if (!options.autoInitializeNative) {
339+
return;
340+
}
338341
try {
339342
await _channel.invokeMethod<void>('initNativeSdk', <String, dynamic>{
340343
'dsn': options.dsn,
@@ -377,6 +380,10 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
377380

378381
@override
379382
FutureOr<void> close() async {
383+
final options = _options;
384+
if (options != null && !options.autoInitializeNative) {
385+
return;
386+
}
380387
try {
381388
await _channel.invokeMethod<void>('closeNativeSdk');
382389
} catch (exception, stackTrace) {

flutter/lib/src/sentry_flutter.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ typedef FlutterOptionsConfiguration = FutureOr<void> Function(
2727
mixin SentryFlutter {
2828
static const _channel = MethodChannel('sentry_flutter');
2929

30-
/// Initializes the SDK
3130
static Future<void> init(
3231
FlutterOptionsConfiguration optionsConfiguration, {
3332
AppRunner? appRunner,
@@ -115,13 +114,15 @@ mixin SentryFlutter {
115114
}
116115

117116
// Will enrich events with device context, native packages and integrations
118-
if (!options.platformChecker.isWeb &&
117+
if (options.platformChecker.hasNativeIntegration &&
118+
!options.platformChecker.isWeb &&
119119
(options.platformChecker.platform.isIOS ||
120120
options.platformChecker.platform.isMacOS)) {
121121
integrations.add(LoadContextsIntegration(channel));
122122
}
123123

124-
if (!options.platformChecker.isWeb &&
124+
if (options.platformChecker.hasNativeIntegration &&
125+
!options.platformChecker.isWeb &&
125126
options.platformChecker.platform.isAndroid) {
126127
integrations.add(LoadAndroidImageListIntegration(channel));
127128
}

flutter/lib/src/sentry_flutter_options.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class SentryFlutterOptions extends SentryOptions {
1111
enableBreadcrumbTrackingForCurrentPlatform();
1212
}
1313

14+
/// Initializes the Native SDKs on init.
15+
/// Set this to `false` if you have an existing native SDK and don't want to re-initialize.
16+
///
17+
/// NOTE: Be careful and only use this if you know what you are doing.
18+
/// If you use this flag, make sure a native SDK is running before the Flutter Engine initializes or events might not be captured.
19+
/// Defaults to `true`.
20+
bool autoInitializeNative = true;
21+
1422
/// Enable or disable reporting of used packages.
1523
bool reportPackages = true;
1624

flutter/test/default_integrations_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,37 @@ void main() {
256256
expect(closeCalled, true);
257257
});
258258

259+
test('nativeSdkIntegration does not call native sdk when auto init disabled',
260+
() async {
261+
var methodChannelCalled = false;
262+
_channel.setMockMethodCallHandler((MethodCall methodCall) async {
263+
methodChannelCalled = true;
264+
});
265+
fixture.options.autoInitializeNative = false;
266+
267+
final integration = NativeSdkIntegration(_channel);
268+
269+
await integration.call(fixture.hub, fixture.options);
270+
271+
expect(methodChannelCalled, false);
272+
});
273+
274+
test('nativeSdkIntegration does not close native when auto init disabled',
275+
() async {
276+
var methodChannelCalled = false;
277+
_channel.setMockMethodCallHandler((MethodCall methodCall) async {
278+
methodChannelCalled = true;
279+
});
280+
fixture.options.autoInitializeNative = false;
281+
282+
final integration = NativeSdkIntegration(_channel);
283+
284+
await integration(fixture.hub, fixture.options);
285+
await integration.close();
286+
287+
expect(methodChannelCalled, false);
288+
});
289+
259290
test('loadContextsIntegration adds integration', () async {
260291
_channel.setMockMethodCallHandler((MethodCall methodCall) async {});
261292

flutter/test/sentry_flutter_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ void main() {
154154
await Sentry.close();
155155
});
156156

157-
test('Web && (iOS)', () async {
158-
// Tests that iOS || macOS integrations aren't added on a browswer which
157+
test('Web && (iOS || macOS) ', () async {
158+
// Tests that iOS || macOS integrations aren't added on a browser which
159159
// runs on iOS or macOS
160160
await SentryFlutter.init(
161161
getConfigurationTester(

0 commit comments

Comments
 (0)