Skip to content

Commit a320b4b

Browse files
authored
Merge eb6c917 into 91418a1
2 parents 91418a1 + eb6c917 commit a320b4b

File tree

6 files changed

+72
-23
lines changed

6 files changed

+72
-23
lines changed

firebase-crashlytics/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [fixed] Fixed inefficiency in the Kotlin `FirebaseCrashlytics.setCustomKeys` extension.
23
* [fixed] Execute failure listener outside the main thread [#6535]
34

45
# 19.2.1
@@ -634,3 +635,4 @@ The following release notes describe changes in the new SDK.
634635
from your `AndroidManifest.xml` file.
635636
* [removed] The `fabric.properties` and `crashlytics.properties` files are no
636637
longer supported. Remove them from your app.
638+

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ class CrashlyticsTests {
5959
Firebase.app.get(UserAgentPublisher::class.java)
6060
}
6161

62+
@Test
63+
fun keyValueBuilder() {
64+
val keyValueBuilder = KeyValueBuilder()
65+
keyValueBuilder.key("hello", "world")
66+
keyValueBuilder.key("hello2", 23)
67+
keyValueBuilder.key("hello3", 0.1)
68+
69+
val result: Map<String, String> = keyValueBuilder.build().keysAndValues
70+
71+
assertThat(result).containsExactly("hello", "world", "hello2", "23", "hello3", "0.1")
72+
}
73+
74+
@Test
75+
fun keyValueBuilder_withCrashlyticsInstance() {
76+
@Suppress("DEPRECATION") val keyValueBuilder = KeyValueBuilder(Firebase.crashlytics)
77+
keyValueBuilder.key("hello", "world")
78+
keyValueBuilder.key("hello2", 23)
79+
keyValueBuilder.key("hello3", 0.1)
80+
81+
val result: Map<String, String> = keyValueBuilder.build().keysAndValues
82+
83+
// The result is empty because it called crashlytics.setCustomKey for every key.
84+
assertThat(result).isEmpty()
85+
}
86+
6287
companion object {
6388
private const val APP_ID = "1:1:android:1a"
6489
private const val API_KEY = "API-KEY-API-KEY-API-KEY-API-KEY-API-KEY"

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/metadata/MetaDataStoreTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public void testWriteUserData_noFields() throws Exception {
118118
}
119119

120120
@Test
121-
// TODO(b/375437048): Let @daymxn know if this test fails. It's flaky and running away from me:(
122121
public void testWriteUserData_singleField() throws Exception {
123122
crashlyticsWorkers.diskWrite.submit(
124-
() -> {
125-
storeUnderTest.writeUserData(SESSION_ID_1, metadataWithUserId(SESSION_ID_1).getUserId());
126-
});
123+
() ->
124+
storeUnderTest.writeUserData(
125+
SESSION_ID_1, metadataWithUserId(SESSION_ID_1).getUserId()));
127126
crashlyticsWorkers.diskWrite.await();
127+
Thread.sleep(10);
128128
UserMetadata userData =
129129
UserMetadata.loadFromExistingSession(SESSION_ID_1, fileStore, crashlyticsWorkers);
130130
assertEquals(USER_ID, userData.getUserId());
@@ -168,17 +168,15 @@ public void testWriteUserData_unicode() throws Exception {
168168
}
169169

170170
@Test
171-
// TODO(b/375437048): Let @daymxn know if this test fails. It's flaky and running away from me:(
172171
public void testWriteUserData_escaped() throws Exception {
173172
crashlyticsWorkers.diskWrite.submit(
174-
() -> {
175-
storeUnderTest.writeUserData(
176-
SESSION_ID_1, metadataWithUserId(SESSION_ID_1, ESCAPED).getUserId());
177-
});
173+
() ->
174+
storeUnderTest.writeUserData(
175+
SESSION_ID_1, metadataWithUserId(SESSION_ID_1, ESCAPED).getUserId()));
178176
crashlyticsWorkers.diskWrite.await();
177+
Thread.sleep(10);
179178
UserMetadata userData =
180179
UserMetadata.loadFromExistingSession(SESSION_ID_1, fileStore, crashlyticsWorkers);
181-
Thread.sleep(10);
182180
assertEquals(ESCAPED.trim(), userData.getUserId());
183181
}
184182

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ val Firebase.crashlytics: FirebaseCrashlytics
2727
get() = FirebaseCrashlytics.getInstance()
2828

2929
/** Associates all key-value parameters with the reports */
30-
fun FirebaseCrashlytics.setCustomKeys(init: KeyValueBuilder.() -> Unit) {
31-
val builder = KeyValueBuilder(this)
32-
builder.init()
33-
}
30+
fun FirebaseCrashlytics.setCustomKeys(init: KeyValueBuilder.() -> Unit) =
31+
setCustomKeys(KeyValueBuilder().apply(init).build())
3432

3533
/** @suppress */
3634
@Keep

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,48 @@
1616

1717
package com.google.firebase.crashlytics
1818

19-
/** Helper class to enable fluent syntax in [setCustomKeys] */
20-
class KeyValueBuilder(private val crashlytics: FirebaseCrashlytics) {
19+
/** Helper class to enable convenient syntax in [setCustomKeys] */
20+
class KeyValueBuilder
21+
private constructor(
22+
private val crashlytics: FirebaseCrashlytics?,
23+
private val builder: CustomKeysAndValues.Builder,
24+
) {
25+
@Deprecated(
26+
"Do not construct this directly. Use `setCustomKeys` instead. To be removed in the next major release."
27+
)
28+
constructor(crashlytics: FirebaseCrashlytics) : this(crashlytics, CustomKeysAndValues.Builder())
29+
30+
internal constructor() : this(crashlytics = null, CustomKeysAndValues.Builder())
31+
32+
internal fun build(): CustomKeysAndValues = builder.build()
2133

2234
/** Sets a custom key and value that are associated with reports. */
23-
fun key(key: String, value: Boolean) = crashlytics.setCustomKey(key, value)
35+
fun key(key: String, value: Boolean) {
36+
crashlytics?.setCustomKey(key, value) ?: builder.putBoolean(key, value)
37+
}
2438

2539
/** Sets a custom key and value that are associated with reports. */
26-
fun key(key: String, value: Double) = crashlytics.setCustomKey(key, value)
40+
fun key(key: String, value: Double) {
41+
crashlytics?.setCustomKey(key, value) ?: builder.putDouble(key, value)
42+
}
2743

2844
/** Sets a custom key and value that are associated with reports. */
29-
fun key(key: String, value: Float) = crashlytics.setCustomKey(key, value)
45+
fun key(key: String, value: Float) {
46+
crashlytics?.setCustomKey(key, value) ?: builder.putFloat(key, value)
47+
}
3048

3149
/** Sets a custom key and value that are associated with reports. */
32-
fun key(key: String, value: Int) = crashlytics.setCustomKey(key, value)
50+
fun key(key: String, value: Int) {
51+
crashlytics?.setCustomKey(key, value) ?: builder.putInt(key, value)
52+
}
3353

3454
/** Sets a custom key and value that are associated with reports. */
35-
fun key(key: String, value: Long) = crashlytics.setCustomKey(key, value)
55+
fun key(key: String, value: Long) {
56+
crashlytics?.setCustomKey(key, value) ?: builder.putLong(key, value)
57+
}
3658

3759
/** Sets a custom key and value that are associated with reports. */
38-
fun key(key: String, value: String) = crashlytics.setCustomKey(key, value)
60+
fun key(key: String, value: String) {
61+
crashlytics?.setCustomKey(key, value) ?: builder.putString(key, value)
62+
}
3963
}

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)