Table of contents
Guessing the cause of the error:
Today, when using android asm, an error that the AndroidComponentsExtension cannot be found occurs
project.extensions.getByType Extension of type 'AndroidComponentsExtension' does not exist
Guessing the cause of the error:
This error is reported when the project registers the transform. Depending on the situation, the project probably has sub-modules, and the sub-modules do not have AndroidComponentsExtension. (It's just a guess, because I don't have deep knowledge of asm)
Solution:
Use Project.pluginManager.withPlugin("com.android.application") to filter modules, obtain ApplicationPlugin, and then register transform. Reference: Bytecode transformations: The Android Gradle Plugin | Product Blog • Sentry
target.pluginManager.withPlugin("com.android.application") {
val androidComponentsExtension =
target.extensions.getByType(AndroidComponentsExtension::class.java)
androidComponentsExtension.onVariants { variant ->
log("variant: ${variant.name}")
variant.instrumentation.transformClassesWith(MethodTraceTransform::class.java, InstrumentationScope.PROJECT){}
variant.instrumentation.setAsmFramesComputationMode(FramesComputationMode.COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS)
}
log("======== end apply ========")
}