Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firebase-crashlytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Unreleased

* [fixed] Fixed inefficiency in the Kotlin `FirebaseCrashlytics.setCustomKeys` extension.

# 19.2.1
* [changed] Updated protobuf dependency to `3.25.5` to fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ class CrashlyticsTests {
Firebase.app.get(UserAgentPublisher::class.java)
}

@Test
fun keyValueBuilder() {
val keyValueBuilder = KeyValueBuilder()
keyValueBuilder.key("hello", "world")
keyValueBuilder.key("hello2", 23)
keyValueBuilder.key("hello3", 0.1)

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

// The result is not empty because we need to pass the CustomKeysAndValues around.
assertThat(result).isNotEmpty()
}

@Test
fun keyValueBuilder_withCrashlyticsInstance() {
val keyValueBuilder = KeyValueBuilder(Firebase.crashlytics)
keyValueBuilder.key("hello", "world")
keyValueBuilder.key("hello2", 23)
keyValueBuilder.key("hello3", 0.1)

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

// The result is empty because it called crashlytics.setCustomKey for every key.
assertThat(result).isEmpty()
}

companion object {
private const val APP_ID = "1:1:android:1a"
private const val API_KEY = "API-KEY-API-KEY-API-KEY-API-KEY-API-KEY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ val Firebase.crashlytics: FirebaseCrashlytics
get() = FirebaseCrashlytics.getInstance()

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

/** @suppress */
@Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,43 @@
package com.google.firebase.crashlytics

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

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

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: Boolean) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: Boolean) {
crashlytics?.setCustomKey(key, value) ?: builder.putBoolean(key, value)
}

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: Double) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: Double) {
crashlytics?.setCustomKey(key, value) ?: builder.putDouble(key, value)
}

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: Float) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: Float) {
crashlytics?.setCustomKey(key, value) ?: builder.putFloat(key, value)
}

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: Int) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: Int) {
crashlytics?.setCustomKey(key, value) ?: builder.putInt(key, value)
}

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: Long) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: Long) {
crashlytics?.setCustomKey(key, value) ?: builder.putLong(key, value)
}

/** Sets a custom key and value that are associated with reports. */
fun key(key: String, value: String) = crashlytics.setCustomKey(key, value)
fun key(key: String, value: String) {
crashlytics?.setCustomKey(key, value) ?: builder.putString(key, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ public void setCustomKey(String key, String value) {
* @throws NullPointerException if any key in keysAndValues is null.
*/
public void setCustomKeys(Map<String, String> keysAndValues) {
crashlyticsWorkers.common.submit(() -> controller.setCustomKeys(keysAndValues));
if (!keysAndValues.isEmpty()) {
crashlyticsWorkers.common.submit(() -> controller.setCustomKeys(keysAndValues));
}
}

// endregion
Expand Down