aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/android/android_sdk_repository_template.bzl
blob: ec6de6d605413e8081eea04d8d93d36f7761e1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""Template for the build file used in android_sdk_repository."""
# Copyright 2016 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def create_android_sdk_rules(
    name,
    build_tools_version,
    build_tools_directory,
    api_levels,
    default_api_level):
  """Generate the contents of the android_sdk_repository.

  Args:
    name: string, the name of the repository being generated.
    build_tools_version: string, the version of Android's build tools to use.
    build_tools_directory: string, the directory name of the build tools in
        sdk's build-tools directory.
    api_levels: list of ints, the API levels from which to get android.jar
        et al. and create android_sdk rules.
    default_api_level: int, the API level to alias the default sdk to if
        --android_sdk is not specified on the command line.
  """

  # This filegroup is used to pass the contents of the SDK to the Android
  # integration tests. We need to glob because not all of these folders ship
  # with the Android SDK, some need to be installed through the SDK Manager.
  # Since android_sdk_repository function generates BUILD files, we do not want
  # to include those because the integration test would fail when it tries to
  # regenerate those BUILD files because the Bazel Sandbox does not allow
  # overwriting existing files.
  native.filegroup(
      name = "files",
      srcs = native.glob([
          "add-ons",
          "build-tools",
          "extras",
          "platforms",
          "platform-tools",
          "sources",
          "system-images",
          "tools",
      ], exclude_directories = 0),
  )

  for api_level in api_levels:
    if api_level >= 23:
      # Android 23 removed most of org.apache.http from android.jar and moved it
      # to a separate jar.
      native.java_import(
          name = "org_apache_http_legacy-%d" % api_level,
          jars = ["platforms/android-%d/optional/org.apache.http.legacy.jar" % api_level]
      )

    native.android_sdk(
        name = "sdk-%d" % api_level,
        build_tools_version = build_tools_version,
        proguard = ":proguard_binary",
        aapt = ":aapt_binary",
        dx = ":dx_binary",
        main_dex_list_creator = ":main_dex_list_creator",
        adb = "platform-tools/adb",
        framework_aidl = "platforms/android-%d/framework.aidl" % api_level,
        aidl = ":aidl_binary",
        android_jar = "platforms/android-%d/android.jar" % api_level,
        shrinked_android_jar = "platforms/android-%d/android.jar" % api_level,
        annotations_jar = "tools/support/annotations.jar",
        main_dex_classes = "build-tools/%s/mainDexClasses.rules" % build_tools_directory,
        apkbuilder = "@bazel_tools//third_party/java/apkbuilder:embedded_apkbuilder",
        apksigner = ":apksigner",
        zipalign = ":zipalign_binary",
        jack = ":fail",
        jill = ":fail",
        resource_extractor = "@bazel_tools//tools/android:resource_extractor",
    )

  native.alias(
      name = "org_apache_http_legacy",
      actual = ":org_apache_http_legacy-%d" % default_api_level,
  )

  native.alias(
      name = "sdk",
      actual = ":sdk-%d" % default_api_level,
  )

  native.java_import(
      name = "proguard_import",
      jars = ["tools/proguard/lib/proguard.jar"]
  )

  native.java_binary(
      name = "proguard_binary",
      main_class = "proguard.ProGuard",
      runtime_deps = [":proguard_import"]
  )

  native.java_binary(
      name = "apksigner",
      main_class = "com.android.apksigner.ApkSignerTool",
      runtime_deps = ["build-tools/%s/lib/apksigner.jar" % build_tools_directory],
  )

  native.filegroup(
      name = "build_tools_libs",
      srcs = native.glob([
          "build-tools/%s/lib/**" % build_tools_directory,
          # Build tools version 24.0.0 added a lib64 folder.
          "build-tools/%s/lib64/**" % build_tools_directory,
      ])
  )

  for tool in ["aapt", "aidl", "zipalign"]:
    native.genrule(
        name = tool + "_runner",
        outs = [tool + "_runner.sh"],
        srcs = [],
        cmd  = "\n".join([
            "cat > $@ << 'EOF'",
            "#!/bin/bash",
            "set -eu",
            # The tools under build-tools/VERSION require the libraries under build-tools/VERSION/lib,
            # so we can't simply depend on them as a file like we do with aapt.
            "SDK=$${0}.runfiles/%s" % name,
            "exec $${SDK}/build-tools/%s/%s $$*" % (build_tools_directory, tool),
            "EOF\n"]),
    )

    native.sh_binary(
        name = tool + "_binary",
        srcs = [tool + "_runner.sh"],
        data = [
            ":build_tools_libs",
            "build-tools/%s/%s" % (build_tools_directory, tool)
        ],
    )

  native.sh_binary(
      name = "fail",
      srcs = ["fail.sh"])

  native.genrule(
      name = "generate_fail_sh",
      srcs = [],
      outs = ["fail.sh"],
      cmd = "echo -e '#!/bin/bash\\nexit 1' >> $@; chmod +x $@",
  )


  native.genrule(
      name = "main_dex_list_creator_source",
      srcs = [],
      outs = ["main_dex_list_creator.sh"],
      cmd = "\n".join(["cat > $@ <<'EOF'",
            "#!/bin/bash",
            "",
            "MAIN_DEX_LIST=$$1",
            "STRIPPED_JAR=$$2",
            "JAR=$$3",
            "" +
            "DIRNAME=$$(dirname $$0)",
            "JAVA_BINARY=TBD/main_dex_list_creator_java",  # Proper runfiles path comes here
            "$$JAVA_BINARY $$STRIPPED_JAR $$JAR > $$MAIN_DEX_LIST",
            "exit $$?",
            "",
            "EOF\n"]),
  )

  native.sh_binary(
      name = "main_dex_list_creator",
      srcs = ["main_dex_list_creator.sh"],
      data = [":main_dex_list_creator_java"],
  )

  native.java_binary(
      name = "main_dex_list_creator_java",
      main_class = "com.android.multidex.ClassReferenceListBuilder",
      runtime_deps = [":dx_jar_import"],
  )

  native.java_binary(
      name = "dx_binary",
      main_class = "com.android.dx.command.Main",
      runtime_deps = [":dx_jar_import"],
  )

  native.filegroup(
      name = "dx_jar",
      srcs = ["build-tools/%s/lib/dx.jar" % build_tools_directory],
  )

  native.java_import(
      name = "dx_jar_import",
      jars = [":dx_jar"],
  )