Add `baseline_profiles_override` to ignore other baseline profiles

This will result in just the baseline profiles from `android_binary.baseline_profiles_override` and `android_binary.startup_profiles` being used as the resulting baseline profiles - ignoring profiles pulled in from libraries. This should only be used if teams have very representative generated baseline profiles.

PiperOrigin-RevId: 937446627
Change-Id: I85137ea713eb50828651a66117f56bd797e0f7a3
diff --git a/rules/android_binary/attrs.bzl b/rules/android_binary/attrs.bzl
index 134e4ba..b6cd67c 100644
--- a/rules/android_binary/attrs.bzl
+++ b/rules/android_binary/attrs.bzl
@@ -102,6 +102,18 @@
                 """,
                 cfg = android_platforms_transition,
             ),
+            baseline_profiles_override = attr.label_list(
+                allow_empty = True,
+                allow_files = [".txt"],
+                doc = """
+                The list of baseline profiles that override any baseline profiles from
+                dependencies. If specified, all baseline profiles coming in from
+                android_library.baseline_profiles are ignored. Only use this if your baseline
+                profiles cover a very representative set of app functions. These will also be used
+                as startup profiles if startup profiles are not specified.
+                """,
+                cfg = android_platforms_transition,
+            ),
             proguard_specs = attr.label_list(allow_empty = True, allow_files = True, cfg = android_platforms_transition),
             resource_apks = attr.label_list(
                 allow_rules = ["apk_import"],
diff --git a/rules/android_binary/impl.bzl b/rules/android_binary/impl.bzl
index 7e3fced..c9d1bb7 100644
--- a/rules/android_binary/impl.bzl
+++ b/rules/android_binary/impl.bzl
@@ -642,16 +642,20 @@
 
         # Include startup profiles if the optimizer is disabled since profiles won't be merged
         # in the optimizer.
-        transitive_profiles = depset(
-            ctx.files.startup_profiles if enable_optimizer_integration and not has_proguard_specs else [],
-            transitive = [
-                profile_provider.files
-                for profile_provider in utils.collect_providers(
-                    BaselineProfileProvider,
-                    ctx.attr.deps,
-                )
-            ],
-        )
+        startup_profiles = ctx.files.startup_profiles if enable_optimizer_integration and not has_proguard_specs else []
+        if ctx.attr.baseline_profiles_override:
+            transitive_profiles = depset(ctx.files.baseline_profiles_override + startup_profiles)
+        else:
+            transitive_profiles = depset(
+                startup_profiles,
+                transitive = [
+                    profile_provider.files
+                    for profile_provider in utils.collect_providers(
+                        BaselineProfileProvider,
+                        ctx.attr.deps,
+                    )
+                ],
+            )
         baseline_profile_output = _baseline_profiles.process(
             ctx,
             transitive_profiles = transitive_profiles,