Skip to content

Commit e5a1077

Browse files
committed
Deprecate crashlytics param to KeyValueBuilder
1 parent 0e44a52 commit e5a1077

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/CrashlyticsTests.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,23 @@ class CrashlyticsTests {
6868

6969
val result: Map<String, String> = keyValueBuilder.build().keysAndValues
7070

71-
// TODO(mrober): Make unit tests for this.
71+
// The result is not empty because we need to pass the CustomKeysAndValues around.
7272
assertThat(result).isNotEmpty()
7373
}
7474

75+
@Test
76+
fun keyValueBuilder_withCrashlyticsInstance() {
77+
val keyValueBuilder = KeyValueBuilder(Firebase.crashlytics)
78+
keyValueBuilder.key("hello", "world")
79+
keyValueBuilder.key("hello2", 23)
80+
keyValueBuilder.key("hello3", 0.1)
81+
82+
val result: Map<String, String> = keyValueBuilder.build().keysAndValues
83+
84+
// The result is empty because it called crashlytics.setCustomKey for every key.
85+
assertThat(result).isEmpty()
86+
}
87+
7588
companion object {
7689
private const val APP_ID = "1:1:android:1a"
7790
private const val API_KEY = "API-KEY-API-KEY-API-KEY-API-KEY-API-KEY"

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/KeyValueBuilder.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,43 @@
1717
package com.google.firebase.crashlytics
1818

1919
/** Helper class to enable fluent syntax in [setCustomKeys] */
20-
class KeyValueBuilder internal constructor() {
20+
@Suppress("DEPRECATION")
21+
class KeyValueBuilder(
22+
// TODO(mrober): Remove this param and make ctor internal in 2025.
23+
@Deprecated(message = "The crashlytics instance is no longer needed and will be removed in 2025.")
24+
private val crashlytics: FirebaseCrashlytics? = null
25+
) {
2126
private val builder = CustomKeysAndValues.Builder()
2227

2328
internal fun build(): CustomKeysAndValues = builder.build()
2429

2530
/** Sets a custom key and value that are associated with reports. */
2631
fun key(key: String, value: Boolean) {
27-
builder.putBoolean(key, value)
32+
crashlytics?.setCustomKey(key, value) ?: builder.putBoolean(key, value)
2833
}
2934

3035
/** Sets a custom key and value that are associated with reports. */
3136
fun key(key: String, value: Double) {
32-
builder.putDouble(key, value)
37+
crashlytics?.setCustomKey(key, value) ?: builder.putDouble(key, value)
3338
}
3439

3540
/** Sets a custom key and value that are associated with reports. */
3641
fun key(key: String, value: Float) {
37-
builder.putFloat(key, value)
42+
crashlytics?.setCustomKey(key, value) ?: builder.putFloat(key, value)
3843
}
3944

4045
/** Sets a custom key and value that are associated with reports. */
4146
fun key(key: String, value: Int) {
42-
builder.putInt(key, value)
47+
crashlytics?.setCustomKey(key, value) ?: builder.putInt(key, value)
4348
}
4449

4550
/** Sets a custom key and value that are associated with reports. */
4651
fun key(key: String, value: Long) {
47-
builder.putLong(key, value)
52+
crashlytics?.setCustomKey(key, value) ?: builder.putLong(key, value)
4853
}
4954

5055
/** Sets a custom key and value that are associated with reports. */
5156
fun key(key: String, value: String) {
52-
builder.putString(key, value)
57+
crashlytics?.setCustomKey(key, value) ?: builder.putString(key, value)
5358
}
5459
}

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCore.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ public void setCustomKey(String key, String value) {
368368
* @throws NullPointerException if any key in keysAndValues is null.
369369
*/
370370
public void setCustomKeys(Map<String, String> keysAndValues) {
371-
crashlyticsWorkers.common.submit(() -> controller.setCustomKeys(keysAndValues));
371+
if (!keysAndValues.isEmpty()) {
372+
crashlyticsWorkers.common.submit(() -> controller.setCustomKeys(keysAndValues));
373+
}
372374
}
373375

374376
// endregion

0 commit comments

Comments
 (0)