I’m Trying to Intercept Some Methods Using the Byte-Buddy Android Gradle Plugin
I want to intercept some methods using the Byte-Buddy Android Gradle plugin. Here are the versions I am using:
• Java version: 17 • Gradle version: 8.2.2 • ASM version: 9.6 • Kotlin version: 1.9.21 • byte-buddy version: 1.14.18 I’ve tried many methods, but to keep it simple, I’ll share the example below.
public class TestClass { @Override public String toString() { return "TestClass{2}"; } } I integrated the Gradle plugin, and it runs during compilation.
import net.bytebuddy.asm.AsmVisitorWrapper import net.bytebuddy.build.Plugin import net.bytebuddy.description.field.FieldDescription import net.bytebuddy.description.field.FieldList import net.bytebuddy.description.method.MethodList import net.bytebuddy.description.type.TypeDescription import net.bytebuddy.dynamic.ClassFileLocator import net.bytebuddy.dynamic.DynamicType import net.bytebuddy.implementation.Implementation import net.bytebuddy.jar.asm.ClassVisitor import net.bytebuddy.pool.TypePool import org.objectweb.asm.Opcodes class AnalyticsPlugin : Plugin { override fun matches(target: TypeDescription): Boolean { return target.canonicalName?.contains("TestClass") == true } override fun close() {} override fun apply( builder: DynamicType.Builder<*>, typeDescription: TypeDescription, classFileLocator: ClassFileLocator ): DynamicType.Builder<*> { // Intercept the toString method return builder.withToString() } } As a result of these operations, I get the following exception:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:stageDebugBytebuddyTransform'. > Failed to transform class com.app.ui.TestClass using com.app.analytics.compiler.AnalyticsPlugin@3260d700 * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:stageDebugBytebuddyTransform'. at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:148) ... Caused by: java.lang.IllegalStateException: Failed to transform class com.app.ui.TestClass using com.app.analytics.compiler.AnalyticsPlugin@3260d700 at net.bytebuddy.build.Plugin$Engine$ErrorHandler$Failing$1.onError(Plugin.java:1179) ... Caused by: java.lang.UnsupportedOperationException: Cannot intercept method for decorated type: class com.app.ui.TestClass at net.bytebuddy.dynamic.scaffold.inline.DecoratingDynamicTypeBuilder.invokable(DecoratingDynamicTypeBuilder.java:445) at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.invokable(DynamicType.java:3673) at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.method(DynamicType.java:3659) at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.withToString(DynamicType.java:3690) at com.app.analytics.compiler.AnalyticsPlugin.apply(AnalyticsPlugin.kt:44) ... When I use the following code, I encounter the same issue:
builder.method(isAnnotatedWith(CallToAction::class.java)) .intercept(MethodDelegation.to(Interceptor::class.java)) Is there any solution to this problem that i might be missing?