Skip to content

Commit

Permalink
Merge pull request #1326 from embrace-io/add-moshi-rules
Browse files Browse the repository at this point in the history
Add moshi rules to prevent adapters failing in minified build
  • Loading branch information
fractalwrench authored Sep 6, 2024
2 parents 2967682 + 337be04 commit 5daee45
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.embrace.android.embracesdk.internal.clock.NormalizedIntervalClock
import io.embrace.android.embracesdk.internal.clock.SystemClock
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.EmbLoggerImpl
import io.embrace.android.embracesdk.internal.serialization.DecoratedSerializer
import io.embrace.android.embracesdk.internal.serialization.EmbraceSerializer
import io.embrace.android.embracesdk.internal.serialization.PlatformSerializer
import io.embrace.android.embracesdk.internal.telemetry.EmbraceTelemetryService
Expand All @@ -32,6 +33,6 @@ internal class InitModuleImpl(
}

override val jsonSerializer: PlatformSerializer by singleton {
EmbraceSerializer()
DecoratedSerializer(EmbraceSerializer(), logger)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.embrace.android.embracesdk.internal.serialization

import io.embrace.android.embracesdk.internal.logging.EmbLogger
import java.io.InputStream
import java.io.OutputStream
import java.lang.reflect.Type

internal class DecoratedSerializer(
private val impl: PlatformSerializer,
private val logger: EmbLogger
) : PlatformSerializer {

override fun <T> toJson(src: T): String {
return serializerAction { impl.toJson(src) }
}

override fun <T> toJson(src: T, clz: Class<T>): String {
return serializerAction { impl.toJson(src, clz) }
}

override fun <T> toJson(src: T, type: Type): String {
return serializerAction { impl.toJson(src, type) }
}

override fun <T> toJson(any: T, clazz: Class<T>, outputStream: OutputStream) {
return serializerAction { impl.toJson(any, clazz, outputStream) }
}

override fun <T> toJson(any: T, type: Type, outputStream: OutputStream) {
return serializerAction { impl.toJson(any, type, outputStream) }
}

override fun <T> fromJson(json: String, clz: Class<T>): T {
return serializerAction { impl.fromJson(json, clz) }
}

override fun <T> fromJson(json: String, type: Type): T {
return serializerAction { impl.fromJson(json, type) }
}

override fun <T> fromJson(inputStream: InputStream, clz: Class<T>): T {
return serializerAction { impl.fromJson(inputStream, clz) }
}

override fun <T> fromJson(inputStream: InputStream, type: Type): T {
return serializerAction { impl.fromJson(inputStream, type) }
}

private fun <T> serializerAction(action: () -> T): T {
try {
return action()
} catch (exc: Exception) {
logger.logError("JSON serializer failed", exc)
throw exc
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.embrace.android.embracesdk.internal.comms.api

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = false)
public enum class Endpoint(
public val path: String,
public val version: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.embrace.android.embracesdk.internal.config.remote

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = false)
public enum class Unwinder(public val code: Int) {
LIBUNWIND(0),
LIBUNWINDSTACK(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.JsonClass

/**
* Intervals during which the UI thread was blocked for more than 1 second, which
* determines that the application is not responding (ANR).
Expand Down Expand Up @@ -40,6 +42,7 @@ public data class AnrInterval @JvmOverloads constructor(
/**
* The type of thread not responding. Currently only the UI thread is monitored.
*/
@JsonClass(generateAdapter = false)
public enum class Type {
UI
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = false)
public enum class ApplicationState {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.JsonClass

public class PushNotificationBreadcrumb {

@JsonClass(generateAdapter = false)
public enum class NotificationType(public val type: String) {
NOTIFICATION("notif"),
DATA("data"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = false)
public enum class ThreadState(public val code: Int) {
NEW(0),
RUNNABLE(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.JsonClass

/**
* Web Core Vital type.
*
Expand All @@ -9,6 +11,7 @@ package io.embrace.android.embracesdk.internal.payload
* FCP = First Contentful Paint: Indicates the time it takes for the first content element to appear on the screen.
*
*/
@JsonClass(generateAdapter = false)
public enum class WebVitalType {
FID, LCP, CLS, FCP
}
10 changes: 9 additions & 1 deletion embrace-android-sdk/embrace-proguard.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
-keepattributes LineNumberTable, SourceFile

## Keep classes used by hosted SDKs
## Keep public API including enums
-keep class io.embrace.android.embracesdk.Embrace { *; }
-keep class io.embrace.android.embracesdk.network.http.HttpMethod { *; }
-keep class io.embrace.android.embracesdk.network.EmbraceNetworkRequest { *; }
-keep class io.embrace.android.embracesdk.Severity { *; }
-keep class io.embrace.android.embracesdk.LogType { *; }
-keep class io.embrace.android.embracesdk.LogExceptionType { *; }
-keep class io.embrace.android.embracesdk.spans.ErrorCode { *; }

## Keep classes used by hosted SDKs
-keep class io.embrace.android.embracesdk.Embrace$AppFramework { *; }
-keep class io.embrace.android.embracesdk.Embrace$LastRunEndState { *; }
-keep class io.embrace.android.embracesdk.Severity { *; }
Expand Down

0 comments on commit 5daee45

Please sign in to comment.