xref: /aosp_15_r20/external/bazelbuild-rules_android/mobile_install/adapters/android_library.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2018 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"""Rule adapter for android_library."""
15
16load(":adapters/base.bzl", "make_adapter")
17load(
18    ":providers.bzl",
19    "MIAndroidAarNativeLibsInfo",
20    "MIAndroidAssetsInfo",
21    "MIAndroidDexInfo",
22    "MIAndroidResourcesInfo",
23    "MIAndroidSdkInfo",
24    "MIJavaResourcesInfo",
25    "providers",
26)
27load(":resources.bzl", "get_assets_dir")
28load(":transform.bzl", "dex", "filter_jars")
29
30def _aspect_attrs():
31    """Attrs of the rule requiring traversal by the aspect."""
32    return [
33        "_android_sdk",
34        # Access the kt toolchain to get kotlin std and runtime libs.
35        "_toolchain",
36        "deps",
37        "exports",
38    ]
39
40def _adapt(target, ctx):
41    """Adapts the rule and target data.
42
43    Args:
44      target: The target.
45      ctx: The context.
46
47    Returns:
48      A list of providers.
49    """
50    kt_toolchain = [ctx.rule.attr._toolchain] if hasattr(ctx.rule.attr, "_toolchain") else []
51    if ctx.rule.attr.neverlink:
52        return []
53
54    if target[AndroidIdeInfo].idl_generated_java_files:
55        aidl_lib = [ctx.rule.attr._android_sdk[MIAndroidSdkInfo].aidl_lib]
56    else:
57        aidl_lib = []
58
59    return [
60        providers.make_mi_android_aar_native_libs_info(
61            deps = providers.collect(
62                MIAndroidAarNativeLibsInfo,
63                ctx.rule.attr.deps,
64                ctx.rule.attr.exports,
65            ),
66        ),
67        providers.make_mi_android_assets_info(
68            assets = depset(ctx.rule.files.assets),
69            assets_dir = get_assets_dir(
70                ctx.rule.files.assets[0],
71                ctx.rule.attr.assets_dir,
72            ) if ctx.rule.files.assets else None,
73            deps = providers.collect(
74                MIAndroidAssetsInfo,
75                ctx.rule.attr.deps,
76                ctx.rule.attr.exports,
77            ),
78        ),
79        providers.make_mi_android_dex_info(
80            dex_shards = dex(
81                ctx,
82                filter_jars(
83                    ctx.label.name + "_resources.jar",
84                    target[JavaInfo].runtime_output_jars,
85                ),
86                target[JavaInfo].transitive_compile_time_jars,
87            ),
88            deps = providers.collect(
89                MIAndroidDexInfo,
90                ctx.rule.attr.deps,
91                ctx.rule.attr.exports,
92                aidl_lib,
93                kt_toolchain,
94            ),
95        ),
96        providers.make_mi_android_resources_info(
97            package = target[AndroidIdeInfo].java_package,
98            deps = providers.collect(
99                MIAndroidResourcesInfo,
100                ctx.rule.attr.deps,
101                ctx.rule.attr.exports,
102            ),
103        ),
104        providers.make_mi_java_resources_info(
105            deps = providers.collect(
106                MIJavaResourcesInfo,
107                ctx.rule.attr.deps,
108                ctx.rule.attr.exports,
109                aidl_lib,
110                kt_toolchain,
111            ),
112        ),
113    ]
114
115android_library = make_adapter(_aspect_attrs, _adapt)
116