diff --git a/rules/android_binary_internal/impl.bzl b/rules/android_binary_internal/impl.bzl index e65502321..9d2d568a0 100644 --- a/rules/android_binary_internal/impl.bzl +++ b/rules/android_binary_internal/impl.bzl @@ -333,6 +333,21 @@ def _process_deploy_jar(ctx, stamp_ctx, packaged_resources_ctx, jvm_ctx, build_i deploy_manifest_lines = build_info_ctx.deploy_manifest_lines, ) + if _is_instrumentation(ctx): + filtered_deploy_jar = ctx.actions.declare_file(ctx.label.name + "_migrated_filtered.jar") + filter_jar = ctx.attr.instruments[AndroidPreDexJarInfo].pre_dex_jar + common.filter_zip_exclude( + ctx, + output = filtered_deploy_jar, + input = deploy_jar, + filter_zips = [filter_jar], + filter_types = [".class"], + # These files are generated by databinding in both the target and the instrumentation + # app with different contents. We want to keep the one from the target app. + filters = ["/BR\\.class$", "/databinding/[^/]+Binding\\.class$"], + ) + deploy_jar = filtered_deploy_jar + return ProviderInfo( name = "deploy_ctx", value = struct( @@ -378,7 +393,19 @@ def _is_test_binary(ctx): Returns: Boolean indicating whether the target is a test target. """ - return ctx.attr.testonly or ctx.attr.instruments or str(ctx.label).find("/javatests/") >= 0 + return ctx.attr.testonly or _is_instrumentation(ctx) or str(ctx.label).find("/javatests/") >= 0 + +def _is_instrumentation(ctx): + """Whether this android_binary target is an instrumentation binary. + + Args: + ctx: The context. + + Returns: + Boolean indicating whether the target is an instrumentation target. + + """ + return bool(ctx.attr.instruments) def _process_baseline_profiles(ctx, dex_ctx, **_unused_ctxs): providers = []