xref: /aosp_15_r20/external/bazelbuild-rules_android/rules/android_binary.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2019 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Bazel rule for building an APK."""
16
17load(":common.bzl", "common")
18load(":migration_tag_DONOTUSE.bzl", "add_migration_tag")
19load("//rules/android_binary_internal:rule.bzl", "android_binary_internal_macro")
20load("//rules:acls.bzl", "acls")
21
22def android_binary(**attrs):
23    """Bazel android_binary rule.
24
25    https://docs.bazel.build/versions/master/be/android.html#android_binary
26
27    Args:
28      **attrs: Rule attributes
29    """
30    android_binary_internal_name = ":" + attrs["name"] + common.PACKAGED_RESOURCES_SUFFIX
31    android_binary_internal_macro(
32        **dict(
33            attrs,
34            name = android_binary_internal_name[1:],
35            visibility = ["//visibility:private"],
36        )
37    )
38
39    attrs.pop("$enable_manifest_merging", None)
40
41    # dex_shards is deprecated and unused. This only existed for mobile-install classic which has
42    # been replaced by mobile-install v2
43    attrs.pop("dex_shards", None)
44
45    # resource_apks is not used by the native android_binary
46    attrs.pop("resource_apks", None)
47
48    fqn = "//%s:%s" % (native.package_name(), attrs["name"])
49    if acls.use_r8(fqn):
50        # Do not pass proguard specs to the native android_binary so that it does
51        # not try to use proguard and instead uses the dex files from the
52        # AndroidDexInfo provider from android_binary_internal.
53        # This also disables resource shrinking from native android_binary (reguardless of the
54        # shrink_resources attr).
55        attrs["proguard_specs"] = []
56
57    native.android_binary(
58        application_resources = android_binary_internal_name,
59        **add_migration_tag(attrs)
60    )
61