From 308aa701961ee9c780ccc14cb3639f458d0b44ac Mon Sep 17 00:00:00 2001 From: Karl Stenerud Date: Wed, 25 Jun 2025 13:55:14 +0200 Subject: [PATCH 1/2] Make the plugin configurable via DSL --- .../io/bitdrift/capture/AndroidComponentsConfig.kt | 2 +- .../capture/extension/BitdriftPluginExtension.kt | 8 +++----- .../capture/extension/InstrumentationExtension.kt | 11 ++++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt index 315684cbe..e45832466 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt @@ -54,7 +54,7 @@ fun AndroidComponentsExtension<*, *, *>.configure( tmpDir.mkdirs() onVariants { variant -> - if (extension.instrumentation.enabled.get()) { + if (extension.instrumentation.automaticOkHttpInstrumentation.get()) { variant.configureInstrumentation( SpanAddingClassVisitorFactory::class.java, InstrumentationScope.ALL, diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/BitdriftPluginExtension.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/BitdriftPluginExtension.kt index e61610db9..1c5edb84f 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/BitdriftPluginExtension.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/BitdriftPluginExtension.kt @@ -10,8 +10,6 @@ package io.bitdrift.capture.extension import org.gradle.api.Project import javax.inject.Inject -abstract class BitdriftPluginExtension @Inject constructor(project: Project) { - private val objects = project.objects - - val instrumentation: InstrumentationExtension = objects.newInstance(InstrumentationExtension::class.java) -} \ No newline at end of file +open class BitdriftPluginExtension @Inject constructor(project: Project) { + val instrumentation = project.extensions.create("instrumentation", InstrumentationExtension::class.java, project) +} diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt index e9b33db05..969c56b93 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt @@ -8,17 +8,18 @@ package io.bitdrift.capture.extension import javax.inject.Inject -import org.gradle.api.model.ObjectFactory +import org.gradle.api.Project import org.gradle.api.provider.Property -open class InstrumentationExtension @Inject constructor(objects: ObjectFactory) { +open class InstrumentationExtension @Inject constructor(project: Project) { + private val objects = project.objects - val enabled: Property = objects.property(Boolean::class.java) - .convention(true) + val automaticOkHttpInstrumentation: Property = objects.property(Boolean::class.java) + .convention(false) val debug: Property = objects.property(Boolean::class.java).convention( false ) val proxyOkHttpEventListener: Property = objects.property(Boolean::class.java).convention(false) -} \ No newline at end of file +} From c24690b27445342e5a2e90e0608a8bc6803c781d Mon Sep 17 00:00:00 2001 From: Karl Stenerud Date: Wed, 25 Jun 2025 16:14:05 +0200 Subject: [PATCH 2/2] Change boolean property proxyOkHttpEventListener to an enum type okHttpInstrumentationType, with options PROXY and OVERWRITE. --- .../io/bitdrift/capture/AndroidComponentsConfig.kt | 2 +- .../capture/extension/InstrumentationExtension.kt | 11 ++++++++++- .../instrumentation/SpanAddingClassVisitorFactory.kt | 3 ++- .../instrumentation/okhttp/OkHttpEventListener.kt | 2 +- .../visitor/OkHttpEventListenerMethodVisitor.kt | 10 +++++----- .../instrumentation/fakes/TestSpanAddingParameters.kt | 7 ++++--- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt index e45832466..cd26f7f61 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/AndroidComponentsConfig.kt @@ -62,7 +62,7 @@ fun AndroidComponentsExtension<*, *, *>.configure( ) { params -> params.tmpDir.set(tmpDir) params.debug.set(extension.instrumentation.debug) - params.proxyOkHttpEventListener.set(extension.instrumentation.proxyOkHttpEventListener) + params.okHttpInstrumentationType.set(extension.instrumentation.okHttpInstrumentationType) } } } diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt index 969c56b93..ea6787cb3 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/extension/InstrumentationExtension.kt @@ -21,5 +21,14 @@ open class InstrumentationExtension @Inject constructor(project: Project) { false ) - val proxyOkHttpEventListener: Property = objects.property(Boolean::class.java).convention(false) + val okHttpInstrumentationType: Property = objects.property(OkHttpInstrumentationType::class.java).convention(OkHttpInstrumentationType.OVERWRITE) + + enum class OkHttpInstrumentationType { + PROXY, + OVERWRITE, + } + + // Helpers so that these values can be used directly in the DSL + val PROXY = OkHttpInstrumentationType.PROXY + val OVERWRITE = OkHttpInstrumentationType.OVERWRITE } diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/SpanAddingClassVisitorFactory.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/SpanAddingClassVisitorFactory.kt index ebffcbc3a..fc4367bf0 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/SpanAddingClassVisitorFactory.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/SpanAddingClassVisitorFactory.kt @@ -38,6 +38,7 @@ import com.android.build.api.instrumentation.ClassContext import com.android.build.api.instrumentation.ClassData import com.android.build.api.instrumentation.InstrumentationParameters import io.bitdrift.capture.CapturePlugin +import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType import io.bitdrift.capture.instrumentation.okhttp.OkHttpEventListener import io.bitdrift.capture.instrumentation.util.findClassReader import io.bitdrift.capture.instrumentation.util.findClassWriter @@ -55,7 +56,7 @@ abstract class SpanAddingClassVisitorFactory : AsmClassVisitorFactory @get:Input - val proxyOkHttpEventListener: Property + val okHttpInstrumentationType: Property @get:Internal val tmpDir: Property diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/OkHttpEventListener.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/OkHttpEventListener.kt index 7d78d229e..6ff75b497 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/OkHttpEventListener.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/OkHttpEventListener.kt @@ -77,7 +77,7 @@ class OkHttpEventListenerMethodInstrumentable( apiVersion = apiVersion, originalVisitor = originalVisitor, instrumentableContext = instrumentableContext, - proxyEventListener = parameters.proxyOkHttpEventListener.get() + okHttpInstrumentationType = parameters.okHttpInstrumentationType.get() ) override fun isInstrumentable(data: MethodContext): Boolean { diff --git a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/visitor/OkHttpEventListenerMethodVisitor.kt b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/visitor/OkHttpEventListenerMethodVisitor.kt index bb98bce4d..1854b64cc 100644 --- a/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/visitor/OkHttpEventListenerMethodVisitor.kt +++ b/platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/instrumentation/okhttp/visitor/OkHttpEventListenerMethodVisitor.kt @@ -33,6 +33,7 @@ package io.bitdrift.capture.instrumentation.okhttp.visitor +import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType import io.bitdrift.capture.instrumentation.MethodContext import org.objectweb.asm.MethodVisitor import org.objectweb.asm.Opcodes @@ -42,7 +43,7 @@ class OkHttpEventListenerMethodVisitor( apiVersion: Int, originalVisitor: MethodVisitor, instrumentableContext: MethodContext, - val proxyEventListener: Boolean + val okHttpInstrumentationType: OkHttpInstrumentationType ) : AdviceAdapter( apiVersion, originalVisitor, @@ -57,10 +58,9 @@ class OkHttpEventListenerMethodVisitor( override fun onMethodEnter() { super.onMethodEnter() - if (proxyEventListener) { - addProxyingEventListener() - } else { - addOverwritingEventListener() + when (okHttpInstrumentationType) { + OkHttpInstrumentationType.PROXY -> addProxyingEventListener() + OkHttpInstrumentationType.OVERWRITE -> addOverwritingEventListener() } } diff --git a/platform/jvm/capture-plugin/src/test/kotlin/io/bitdrift/capture/instrumentation/fakes/TestSpanAddingParameters.kt b/platform/jvm/capture-plugin/src/test/kotlin/io/bitdrift/capture/instrumentation/fakes/TestSpanAddingParameters.kt index 5541dd3bb..0b3755038 100644 --- a/platform/jvm/capture-plugin/src/test/kotlin/io/bitdrift/capture/instrumentation/fakes/TestSpanAddingParameters.kt +++ b/platform/jvm/capture-plugin/src/test/kotlin/io/bitdrift/capture/instrumentation/fakes/TestSpanAddingParameters.kt @@ -33,6 +33,7 @@ package io.bitdrift.capture.instrumentation.fakes +import io.bitdrift.capture.extension.InstrumentationExtension.OkHttpInstrumentationType import io.bitdrift.capture.instrumentation.ClassInstrumentable import io.bitdrift.capture.instrumentation.SpanAddingClassVisitorFactory import java.io.File @@ -47,9 +48,9 @@ class TestSpanAddingParameters( override val debug: Property get() = DefaultProperty(PropertyHost.NO_OP, Boolean::class.javaObjectType) .convention(debugOutput) - override val proxyOkHttpEventListener: Property - get() = DefaultProperty(PropertyHost.NO_OP, Boolean::class.javaObjectType) - .convention(true) + override val okHttpInstrumentationType: Property + get() = DefaultProperty(PropertyHost.NO_OP, OkHttpInstrumentationType::class.javaObjectType) + .convention(OkHttpInstrumentationType.PROXY) override val tmpDir: Property get() = DefaultProperty(PropertyHost.NO_OP, File::class.java).convention(inMemoryDir)