Skip to content

Commit

Permalink
R8 consumer rules (#685)
Browse files Browse the repository at this point in the history
* Ensuring demo app build fails due to R8 missing rules

* Fixing demo app serialization after obfuscation

* Adding code to check R8 rules around grpc exporters
  • Loading branch information
LikeTheSalad authored Nov 21, 2024
1 parent 91935e9 commit f8f1fa4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
12 changes: 12 additions & 0 deletions core/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-dontwarn com.google.auto.value.AutoValue$Builder
-dontwarn com.google.auto.value.AutoValue$CopyAnnotations
-dontwarn com.google.auto.value.AutoValue
-dontwarn com.google.auto.value.extension.memoized.Memoized
-dontwarn io.grpc.Channel
-dontwarn io.grpc.MethodDescriptor$Builder
-dontwarn io.grpc.MethodDescriptor$Marshaller
-dontwarn io.grpc.MethodDescriptor$MethodType
-dontwarn io.grpc.MethodDescriptor
-dontwarn io.grpc.stub.AbstractFutureStub
-dontwarn io.grpc.stub.AbstractStub$StubFactory
-dontwarn io.grpc.stub.AbstractStub
5 changes: 1 addition & 4 deletions demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ android {
}
release {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
signingConfig = signingConfigs["debug"]
}
}
buildFeatures {
Expand Down
6 changes: 0 additions & 6 deletions demo-app/proguard-rules.pro

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.app.Application
import android.util.Log
import io.opentelemetry.android.OpenTelemetryRum
import io.opentelemetry.android.OpenTelemetryRumBuilder
import io.opentelemetry.android.agent.setSlowRenderingDetectionPollInterval
import io.opentelemetry.android.config.OtelRumConfig
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfiguration
import io.opentelemetry.api.common.AttributeKey.stringKey
Expand All @@ -19,9 +18,9 @@ import io.opentelemetry.api.incubator.events.EventBuilder
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter
import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider
import java.time.Duration
import kotlin.math.log

const val TAG = "otel.demo"

Expand Down Expand Up @@ -62,6 +61,26 @@ class OtelDemoApplication : Application() {
} catch (e: Exception) {
Log.e(TAG, "Oh no!", e)
}

// This is needed to get R8 missing rules warnings.
initializeOtelWithGrpc()
}

// This is not used but it's needed to verify that our consumer proguard rules cover this use case.
private fun initializeOtelWithGrpc() {
val builder = OpenTelemetryRum.builder(this)
.addSpanExporterCustomizer {
OtlpGrpcSpanExporter.builder().build()
}
.addLogRecordExporterCustomizer {
OtlpGrpcLogRecordExporter.builder().build()
}

// This is an overly-cautious measure to prevent R8 from discarding away the whole method
// in case it identifies that it's actually not doing anything meaningful.
if (System.currentTimeMillis() < 0) {
print(builder)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package io.opentelemetry.android.demo.shop.model

import androidx.annotation.Keep

@Keep
data class Product(
val id: String,
val name: String,
val description: String,
val picture: String,
val priceUsd: PriceUsd,
val categories: List<String>) {
val categories: List<String>
) {
fun priceValue(): Double {
return priceUsd.units.toDouble() + priceUsd.nanos.toDouble()/1_000_000_000f
return priceUsd.units.toDouble() + priceUsd.nanos.toDouble() / 1_000_000_000f
}
}

// For deserialization
@Keep
data class ProductDeserializationWrapper(
val products: List<Product>
)

@Keep
data class PriceUsd(
val currencyCode: String,
val units: Long,
Expand Down

0 comments on commit f8f1fa4

Please sign in to comment.