From 8d16f035b64ac177a52609d393d1fab2d626da99 Mon Sep 17 00:00:00 2001 From: Harpreet Kumar Date: Thu, 26 Nov 2026 17:15:28 +0100 Subject: [PATCH 1/3] fix(crashlytics): apply setCrashlyticsCollectionEnabled in the current run --- docs/crashlytics/usage/index.mdx | 1 + .../firebase/crashlytics/NativeRNFBTurboCrashlytics.java | 5 +++++ .../crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h | 2 ++ .../crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m | 4 ++++ .../crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm | 4 ++++ 5 files changed, 16 insertions(+) diff --git a/docs/crashlytics/usage/index.mdx b/docs/crashlytics/usage/index.mdx index 76f6100fa4..d529bcba09 100644 --- a/docs/crashlytics/usage/index.mdx +++ b/docs/crashlytics/usage/index.mdx @@ -204,6 +204,7 @@ This can be done throughout the app with a simple method call to `setCrashlytics - `setCrashlyticsCollectionEnabled(crashlytics, enabled)` throws if `enabled` is not a boolean and returns a `Promise`. - The module stores the value on the device, so it is used on later launches and takes priority over the `crashlytics_auto_collection_enabled` value in `firebase.json`. +- The native Crashlytics SDK applies the change at once, so a crash later in the same run is sent or kept on the device according to the new value. - `crashlytics.isCrashlyticsCollectionEnabled` is a read-only property that reflects the last value you set, or the startup value if you have not set one. - In a debug build without `crashlytics_debug_enabled`, native collection stays disabled even after you call `setCrashlyticsCollectionEnabled(crashlytics, true)`, although the property then reads `true`. diff --git a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java index 1068f9799f..3e3515ca0a 100644 --- a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java +++ b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java @@ -184,6 +184,11 @@ public void setUserId(String userId, Promise promise) { public void setCrashlyticsCollectionEnabled(boolean enabled, Promise promise) { ReactNativeFirebasePreferences.getSharedInstance() .setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); + // Apply the change in this run too; the preference alone is only read at the next start. + // The effective value still honours crashlytics_debug_enabled in debug builds. + FirebaseCrashlytics.getInstance() + .setCrashlyticsCollectionEnabled( + ReactNativeFirebaseCrashlyticsInitProvider.isCrashlyticsCollectionEnabled()); promise.resolve(null); } diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h index 134a851fb7..677b2e6efb 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h @@ -34,6 +34,8 @@ + (void)deleteUnsentReports; ++ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled; + + (BOOL)didCrashDuringPreviousExecution; + (void)log:(NSString *)message; diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m index d813facbff..381352c979 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m @@ -57,6 +57,10 @@ + (void)deleteUnsentReports { [[FIRCrashlytics crashlytics] deleteUnsentReports]; } ++ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled { + [[FIRCrashlytics crashlytics] setCrashlyticsCollectionEnabled:enabled]; +} + + (BOOL)didCrashDuringPreviousExecution { return [[FIRCrashlytics crashlytics] didCrashDuringPreviousExecution]; } diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm index 8e81bb9f2c..a675889740 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm @@ -194,6 +194,10 @@ - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled (void)reject; [[RNFBPreferences shared] setBooleanValue:@"crashlytics_auto_collection_enabled" boolValue:enabled]; + // Apply the change in this run too; the preference alone is only read at the next start. + // The effective value still honours crashlytics_debug_enabled in debug builds. + [RNFBCrashlyticsHelper + setCrashlyticsCollectionEnabled:[RNFBCrashlyticsInitProvider isCrashlyticsCollectionEnabled]]; resolve([NSNull null]); } From 0e6ae94e59591bc2d2e952a11a64fa82e3a7c365 Mon Sep 17 00:00:00 2001 From: Harpreet Kumar Date: Fri, 27 Nov 2026 09:33:52 +0100 Subject: [PATCH 2/3] docs(crashlytics): say the collection switch applies from the next run --- docs/crashlytics/usage/index.mdx | 2 +- .../firebase/crashlytics/NativeRNFBTurboCrashlytics.java | 2 +- .../crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm | 2 +- packages/crashlytics/lib/types/crashlytics.ts | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/crashlytics/usage/index.mdx b/docs/crashlytics/usage/index.mdx index d529bcba09..c707f39ba4 100644 --- a/docs/crashlytics/usage/index.mdx +++ b/docs/crashlytics/usage/index.mdx @@ -204,7 +204,7 @@ This can be done throughout the app with a simple method call to `setCrashlytics - `setCrashlyticsCollectionEnabled(crashlytics, enabled)` throws if `enabled` is not a boolean and returns a `Promise`. - The module stores the value on the device, so it is used on later launches and takes priority over the `crashlytics_auto_collection_enabled` value in `firebase.json`. -- The native Crashlytics SDK applies the change at once, so a crash later in the same run is sent or kept on the device according to the new value. +- The module also passes the value to the native Crashlytics SDK at once. The Firebase SDKs apply this switch from the next run of the app, so do not rely on it to stop collection in the current run. Because the SDK has the new value saved before the app closes, a withdrawal on iOS no longer needs an extra launch to take effect. - `crashlytics.isCrashlyticsCollectionEnabled` is a read-only property that reflects the last value you set, or the startup value if you have not set one. - In a debug build without `crashlytics_debug_enabled`, native collection stays disabled even after you call `setCrashlyticsCollectionEnabled(crashlytics, true)`, although the property then reads `true`. diff --git a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java index 3e3515ca0a..ba3cfc4195 100644 --- a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java +++ b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java @@ -184,7 +184,7 @@ public void setUserId(String userId, Promise promise) { public void setCrashlyticsCollectionEnabled(boolean enabled, Promise promise) { ReactNativeFirebasePreferences.getSharedInstance() .setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); - // Apply the change in this run too; the preference alone is only read at the next start. + // Also tell the SDK now: it saves the value, so the next start already has it. // The effective value still honours crashlytics_debug_enabled in debug builds. FirebaseCrashlytics.getInstance() .setCrashlyticsCollectionEnabled( diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm index a675889740..9b3bcbd717 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm @@ -194,7 +194,7 @@ - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled (void)reject; [[RNFBPreferences shared] setBooleanValue:@"crashlytics_auto_collection_enabled" boolValue:enabled]; - // Apply the change in this run too; the preference alone is only read at the next start. + // Also tell the SDK now: it saves the value, so the next start already has it. // The effective value still honours crashlytics_debug_enabled in debug builds. [RNFBCrashlyticsHelper setCrashlyticsCollectionEnabled:[RNFBCrashlyticsInitProvider isCrashlyticsCollectionEnabled]]; diff --git a/packages/crashlytics/lib/types/crashlytics.ts b/packages/crashlytics/lib/types/crashlytics.ts index e7c18ff0c8..d053c507bf 100644 --- a/packages/crashlytics/lib/types/crashlytics.ts +++ b/packages/crashlytics/lib/types/crashlytics.ts @@ -108,6 +108,9 @@ export interface Crashlytics extends ReactNativeFirebase.FirebaseModule { * * Use this for opt-in first user data collection flows combined with `firebase.json` settings to disable auto collection. * + * The value is saved on the device and passed to the native Crashlytics SDK at once. The SDK applies it from the + * next run of the app, so it does not stop collection in the current run. + * * @param enabled A boolean value representing whether to enable Crashlytics error collection. */ setCrashlyticsCollectionEnabled(enabled: boolean): Promise; From 17db694ccbf3bfc50122ed95e304e7493a6c9925 Mon Sep 17 00:00:00 2001 From: Harpreet Kumar Date: Fri, 27 Nov 2026 09:51:16 +0100 Subject: [PATCH 3/3] test(crashlytics): cover the collection switch passed to the Android SDK --- docs/crashlytics/usage/index.mdx | 2 +- .../NativeRNFBTurboCrashlytics.java | 13 +++- ...TurboCrashlyticsCollectionEnabledTest.java | 78 +++++++++++++++++++ .../RNFBCrashlytics/RNFBCrashlyticsModule.mm | 2 +- packages/crashlytics/lib/types/crashlytics.ts | 4 +- 5 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java diff --git a/docs/crashlytics/usage/index.mdx b/docs/crashlytics/usage/index.mdx index c707f39ba4..a4ca2ac16c 100644 --- a/docs/crashlytics/usage/index.mdx +++ b/docs/crashlytics/usage/index.mdx @@ -204,7 +204,7 @@ This can be done throughout the app with a simple method call to `setCrashlytics - `setCrashlyticsCollectionEnabled(crashlytics, enabled)` throws if `enabled` is not a boolean and returns a `Promise`. - The module stores the value on the device, so it is used on later launches and takes priority over the `crashlytics_auto_collection_enabled` value in `firebase.json`. -- The module also passes the value to the native Crashlytics SDK at once. The Firebase SDKs apply this switch from the next run of the app, so do not rely on it to stop collection in the current run. Because the SDK has the new value saved before the app closes, a withdrawal on iOS no longer needs an extra launch to take effect. +- The value is also passed to the native Crashlytics SDK straight away. Firebase guarantees the change from the next app launch, so don't rely on it for crashes later in the same run. - `crashlytics.isCrashlyticsCollectionEnabled` is a read-only property that reflects the last value you set, or the startup value if you have not set one. - In a debug build without `crashlytics_debug_enabled`, native collection stays disabled even after you call `setCrashlyticsCollectionEnabled(crashlytics, true)`, although the property then reads `true`. diff --git a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java index ba3cfc4195..94472b4f5e 100644 --- a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java +++ b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java @@ -182,14 +182,21 @@ public void setUserId(String userId, Promise promise) { @Override public void setCrashlyticsCollectionEnabled(boolean enabled, Promise promise) { + saveAndApplyCollectionEnabled(enabled); + promise.resolve(null); + } + + /** + * Saves the value for later launches and passes the native SDK the effective value straight away. + * The effective value still honours crashlytics_debug_enabled in debug builds. Firebase + * guarantees the change from the next app launch. + */ + static void saveAndApplyCollectionEnabled(boolean enabled) { ReactNativeFirebasePreferences.getSharedInstance() .setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); - // Also tell the SDK now: it saves the value, so the next start already has it. - // The effective value still honours crashlytics_debug_enabled in debug builds. FirebaseCrashlytics.getInstance() .setCrashlyticsCollectionEnabled( ReactNativeFirebaseCrashlyticsInitProvider.isCrashlyticsCollectionEnabled()); - promise.resolve(null); } @Override diff --git a/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java b/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java new file mode 100644 index 0000000000..351dccab8d --- /dev/null +++ b/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java @@ -0,0 +1,78 @@ +package io.invertase.firebase.crashlytics; + +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; + +import com.google.firebase.crashlytics.FirebaseCrashlytics; +import io.invertase.firebase.common.ReactNativeFirebasePreferences; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedStatic; +import org.robolectric.RobolectricTestRunner; + +/** + * JVM coverage for {@link NativeRNFBTurboCrashlytics#saveAndApplyCollectionEnabled(boolean)}. The + * SDK must receive the init provider's effective value, not the raw argument, so a debug build + * without crashlytics_debug_enabled stays disabled after setCrashlyticsCollectionEnabled(true). + */ +@RunWith(RobolectricTestRunner.class) +public class NativeRNFBTurboCrashlyticsCollectionEnabledTest { + + /** Runs the helper with the SDK, preferences and init provider stubbed; returns the SDK mock. */ + private static FirebaseCrashlytics saveAndApply(boolean enabled, boolean effective) { + FirebaseCrashlytics crashlytics = mock(FirebaseCrashlytics.class); + ReactNativeFirebasePreferences prefs = mock(ReactNativeFirebasePreferences.class); + + try (MockedStatic firebaseCrashlytics = + mockStatic(FirebaseCrashlytics.class); + MockedStatic preferences = + mockStatic(ReactNativeFirebasePreferences.class); + MockedStatic initProvider = + mockStatic(ReactNativeFirebaseCrashlyticsInitProvider.class)) { + firebaseCrashlytics.when(FirebaseCrashlytics::getInstance).thenReturn(crashlytics); + preferences.when(ReactNativeFirebasePreferences::getSharedInstance).thenReturn(prefs); + initProvider + .when(ReactNativeFirebaseCrashlyticsInitProvider::isCrashlyticsCollectionEnabled) + .thenReturn(effective); + + NativeRNFBTurboCrashlytics.saveAndApplyCollectionEnabled(enabled); + + verify(prefs).setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); + } + return crashlytics; + } + + @Test + public void passesTheEffectiveValueNotTheArgument() { + // A debug build without crashlytics_debug_enabled: the provider computes false. + verify(saveAndApply(true, false)).setCrashlyticsCollectionEnabled(false); + } + + @Test + public void passesTrueWhenCollectionIsEffectivelyEnabled() { + verify(saveAndApply(true, true)).setCrashlyticsCollectionEnabled(true); + } + + @Test + public void passesFalseOnWithdrawal() { + verify(saveAndApply(false, false)).setCrashlyticsCollectionEnabled(false); + } +} diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm index 9b3bcbd717..76849b146a 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm @@ -194,7 +194,7 @@ - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled (void)reject; [[RNFBPreferences shared] setBooleanValue:@"crashlytics_auto_collection_enabled" boolValue:enabled]; - // Also tell the SDK now: it saves the value, so the next start already has it. + // Also pass the SDK the value straight away. Firebase guarantees it from the next app launch. // The effective value still honours crashlytics_debug_enabled in debug builds. [RNFBCrashlyticsHelper setCrashlyticsCollectionEnabled:[RNFBCrashlyticsInitProvider isCrashlyticsCollectionEnabled]]; diff --git a/packages/crashlytics/lib/types/crashlytics.ts b/packages/crashlytics/lib/types/crashlytics.ts index d053c507bf..5a40d12cf2 100644 --- a/packages/crashlytics/lib/types/crashlytics.ts +++ b/packages/crashlytics/lib/types/crashlytics.ts @@ -108,8 +108,8 @@ export interface Crashlytics extends ReactNativeFirebase.FirebaseModule { * * Use this for opt-in first user data collection flows combined with `firebase.json` settings to disable auto collection. * - * The value is saved on the device and passed to the native Crashlytics SDK at once. The SDK applies it from the - * next run of the app, so it does not stop collection in the current run. + * The value is also passed to the native Crashlytics SDK straight away. Firebase guarantees the change from the + * next app launch, so don't rely on it for crashes later in the same run. * * @param enabled A boolean value representing whether to enable Crashlytics error collection. */