lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/bin/bash
#
# Copyright (C) 2008 The Android Open Source Project
#
# 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.
 
# Stop if something fails.
set -e
 
function fail() {
  echo "$*" >&2
  exit 1
}
 
if [[ $# -le 0 ]]; then
  echo 'Error:' '$0 should have the parameters from the "build" script forwarded to it' >&2
  fail 'Error: An example of how do it correctly is ./default-build "$@"'
  exit 1
fi
 
# Set default values for directories.
if [ -d smali ]; then
  HAS_SMALI=true
else
  HAS_SMALI=false
fi
 
# .j files in jasmin get compiled into classes.jar
if [ -d jasmin ]; then
  HAS_JASMIN=true
else
  HAS_JASMIN=false
fi
 
if [ -d src ]; then
  HAS_SRC=true
else
  HAS_SRC=false
fi
 
# .java files in src-art get compiled with libcore on the bootclasspath
if [ -d src-art ]; then
  HAS_SRC_ART=true
else
  HAS_SRC_ART=false
fi
 
if [ -d src2 ]; then
  HAS_SRC2=true
else
  HAS_SRC2=false
fi
 
if [ -d src-multidex ]; then
  HAS_SRC_MULTIDEX=true
else
  HAS_SRC_MULTIDEX=false
fi
 
if [ -d smali-multidex ]; then
  HAS_SMALI_MULTIDEX=true
else
  HAS_SMALI_MULTIDEX=false
fi
 
# .j files in jasmin-multidex get compiled into classes2.jar
if [ -d jasmin-multidex ]; then
  HAS_JASMIN_MULTIDEX=true
else
  HAS_JASMIN_MULTIDEX=false
fi
 
if [ -d smali-ex ]; then
  HAS_SMALI_EX=true
else
  HAS_SMALI_EX=false
fi
 
if [ -d src-ex ]; then
  HAS_SRC_EX=true
else
  HAS_SRC_EX=false
fi
 
if [ -d src-dex2oat-unresolved ]; then
  HAS_SRC_DEX2OAT_UNRESOLVED=true
else
  HAS_SRC_DEX2OAT_UNRESOLVED=false
fi
 
if [ -f hiddenapi-flags.csv ]; then
  HAS_HIDDENAPI_SPEC=true
else
  HAS_HIDDENAPI_SPEC=false
fi
 
# USE_HIDDENAPI=false run-test... will disable hiddenapi.
if [ -z "${USE_HIDDENAPI}" ]; then
  USE_HIDDENAPI=true
fi
 
# DESUGAR=false run-test... will disable desugaring.
if [[ "$DESUGAR" == false ]]; then
  USE_DESUGAR=false
fi
 
# Allow overriding ZIP_COMPRESSION_METHOD with e.g. 'store'
ZIP_COMPRESSION_METHOD="deflate"
# Align every ZIP file made by calling $ZIPALIGN command?
WITH_ZIP_ALIGN=false
ZIP_ALIGN_BYTES="-1"
 
BUILD_MODE="target"
DEV_MODE="no"
 
DEFAULT_EXPERIMENT="no-experiment"
 
# The key for default arguments if no experimental things are enabled.
EXPERIMENTAL=$DEFAULT_EXPERIMENT
 
# Setup experimental API level mappings in a bash associative array.
declare -A EXPERIMENTAL_API_LEVEL
EXPERIMENTAL_API_LEVEL[${DEFAULT_EXPERIMENT}]="26"
EXPERIMENTAL_API_LEVEL["default-methods"]="24"
EXPERIMENTAL_API_LEVEL["parameter-annotations"]="25"
EXPERIMENTAL_API_LEVEL["agents"]="26"
EXPERIMENTAL_API_LEVEL["method-handles"]="26"
EXPERIMENTAL_API_LEVEL["var-handles"]="28"
 
while true; do
  if [ "x$1" = "x--no-src" ]; then
    HAS_SRC=false
    shift
  elif [ "x$1" = "x--no-src2" ]; then
    HAS_SRC2=false
    shift
  elif [ "x$1" = "x--no-src-multidex" ]; then
    HAS_SRC_MULTIDEX=false
    shift
  elif [ "x$1" = "x--no-smali-multidex" ]; then
    HAS_SMALI_MULTIDEX=false
    shift
  elif [ "x$1" = "x--no-src-ex" ]; then
    HAS_SRC_EX=false
    shift
  elif [ "x$1" = "x--no-smali" ]; then
    HAS_SMALI=false
    shift
  elif [ "x$1" = "x--no-jasmin" ]; then
    HAS_JASMIN=false
    shift
  elif [ "x$1" = "x--api-level" ]; then
    shift
    EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]=$1
    shift
  elif [ "x$1" = "x--experimental" ]; then
    shift
    # We have a specific experimental configuration so don't use the default.
    EXPERIMENTAL="$1"
    shift
  elif [ "x$1" = "x--zip-compression-method" ]; then
    # Allow using different zip compression method, e.g. 'store'
    shift
    ZIP_COMPRESSION_METHOD="$1"
    shift
  elif [ "x$1" = "x--zip-align" ]; then
    # Align ZIP entries to some # of bytes.
    shift
    WITH_ZIP_ALIGN=true
    ZIP_ALIGN_BYTES="$1"
    shift
  elif [ "x$1" = "x--host" ]; then
    BUILD_MODE="host"
    shift
  elif [ "x$1" = "x--target" ]; then
    BUILD_MODE="target"
    shift
  elif [ "x$1" = "x--jvm" ]; then
    BUILD_MODE="jvm"
    shift
  elif [ "x$1" = "x--dev" ]; then
    DEV_MODE="yes"
    shift
  elif expr "x$1" : "x--" >/dev/null 2>&1; then
    fail "unknown $0 option: $1"
  else
    break
  fi
done
 
if [[ $BUILD_MODE == jvm ]]; then
  # Does not need desugaring on jvm because it supports the latest functionality.
  USE_DESUGAR=false
  # Do not attempt to build src-art directories on jvm, it would fail without libcore.
  HAS_SRC_ART=false
fi
 
# Set API level for smali and d8.
API_LEVEL="${EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]}"
 
# Add API level arguments to smali and dx
SMALI_ARGS="${SMALI_ARGS} --api $API_LEVEL"
D8_FLAGS="${D8_FLAGS} --min-api $API_LEVEL"
 
#########################################
 
# Catch all commands to 'ZIP' and prepend extra flags.
# Optionally, zipalign results to some alignment.
function zip() {
  local zip_target="$1"
  local entry_src="$2"
  shift 2
 
  command zip --compression-method "$ZIP_COMPRESSION_METHOD" "$zip_target" "$entry_src" "$@"
 
  if "$WITH_ZIP_ALIGN"; then
    # zipalign does not operate in-place, so write results to a temp file.
    local tmp_file="$(mktemp)"
    "$ZIPALIGN" -f "$ZIP_ALIGN_BYTES" "$zip_target" "$tmp_file"
    # replace original zip target with our temp file.
    mv "$tmp_file" "$zip_target"
  fi
}
 
function make_jasmin() {
  local out_directory="$1"
  shift
  local jasmin_sources=("$@")
 
  mkdir -p "$out_directory"
 
  if [[ $DEV_MODE == yes ]]; then
    echo ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}"
    ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}"
  else
    ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}" >/dev/null
  fi
}
 
# Like regular javac but may include libcore on the bootclasspath.
function javac_with_bootclasspath {
  local helper_args="--mode=$BUILD_MODE"
 
  if [[ $DEV_MODE == yes ]]; then
    helper_args="$helper_args --show-commands"
  fi
 
  # build with libcore for host and target, or openjdk for jvm
  "$ANDROID_BUILD_TOP/art/tools/javac-helper.sh" --core-only $helper_args ${JAVAC_ARGS} "$@"
}
 
# Make a "dex" file given a directory of classes in $1. This will be
# packaged in a jar file.
function make_dex() {
  local name="$1"
  local d8_inputs=$(find $name -name '*.class' -type f)
  local d8_output=${name}.jar
  local dex_output=${name}.dex
  local d8_local_flags=""
  if [[ "$USE_DESUGAR" = "true" ]]; then
    local boot_class_path_list=$($ANDROID_BUILD_TOP/art/tools/bootjars.sh --$BUILD_MODE --core --path)
    for boot_class_path_element in $boot_class_path_list; do
      d8_local_flags="$d8_local_flags --classpath $boot_class_path_element"
    done
  else
    d8_local_flags="$d8_local_flags --no-desugaring"
  fi
  if [ "$DEV_MODE" = "yes" ]; then
    echo ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
  fi
  ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
 
  # D8 outputs to JAR files today rather than DEX files as DX used
  # to. To compensate, we extract the DEX from d8's output to meet the
  # expectations of make_dex callers.
  if [ "$DEV_MODE" = "yes" ]; then
    echo unzip -p $d8_output classes.dex \> $dex_output
  fi
  unzip -p $d8_output classes.dex > $dex_output
}
 
# Merge all the dex files in $1..$N into $1. Skip non-existing files, but at least 1 file must exist.
function make_dexmerge() {
  # Dex file that acts as the destination.
  local dst_file="$1"
 
  # Dex files that act as the source.
  local dex_files_to_merge=()
 
  # Skip any non-existing files.
  while [[ $# -gt 0 ]]; do
    if [[ -e "$1" ]]; then
      dex_files_to_merge+=("$1")
    fi
    shift
  done
 
  # Skip merge if we are not merging anything. IE: input = output.
  if [[ "${#dex_files_to_merge[@]}" -eq "1" ]]; then
    local single_input=${dex_files_to_merge[0]}
    if [[ "$dst_file" != "$single_input" ]]; then
     mv "$single_input" "$dst_file";
     return
    fi
  fi
 
  # We assume the dexer did all the API level checks and just merge away.
  mkdir d8_merge_out
  ${DEXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}"
 
  if [[ -e "./d8_merge_out/classes2.dex" ]]; then
    fail "Cannot merge all dex files into a single dex"
  fi
 
  mv ./d8_merge_out/classes.dex "$dst_file";
  rmdir d8_merge_out
}
 
function make_hiddenapi() {
  local args=( "encode" )
  while [[ $# -gt 0 ]]; do
    args+=("--input-dex=$1")
    args+=("--output-dex=$1")
    shift
  done
  args+=("--api-flags=hiddenapi-flags.csv")
  args+=("--no-force-assign-all")
  ${HIDDENAPI} "${args[@]}"
}
 
# Print the directory name only if it exists.
function maybe_dir() {
  local dirname="$1"
  if [[ -d "$dirname" ]]; then
    echo "$dirname"
  fi
}
 
if [ -e classes.dex ]; then
  zip $TEST_NAME.jar classes.dex
  exit 0
fi
 
# Helper function for a common test. Evaluate with $(has_mutlidex).
function has_multidex() {
  echo [ ${HAS_SRC_MULTIDEX} = "true" \
         -o ${HAS_JASMIN_MULTIDEX} = "true" \
         -o ${HAS_SMALI_MULTIDEX} = "true" ]
}
 
if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
  mkdir -p classes
  mkdir classes-ex
  javac_with_bootclasspath -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
  javac_with_bootclasspath -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
  if [ ${NEED_DEX} = "true" ]; then
    make_dex classes-ex
    mv classes-ex.dex classes.dex   # rename it so it shows up as "classes.dex" in the zip file.
    zip ${TEST_NAME}-ex.jar classes.dex
    make_dex classes
  fi
else
  if [ "${HAS_SRC}" = "true" ]; then
    mkdir -p classes
    javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
  fi
 
  if [ "${HAS_SRC_ART}" = "true" ]; then
    mkdir -p classes
    javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src-art -name '*.java'`
  fi
 
  if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
    mkdir classes2
    javac_with_bootclasspath -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
    if [ ${NEED_DEX} = "true" ]; then
      make_dex classes2
    fi
  fi
 
  if [ "${HAS_SRC2}" = "true" ]; then
    mkdir -p classes
    javac_with_bootclasspath -classpath classes -d classes `find src2 -name '*.java'`
  fi
 
  # If the classes directory is not-empty, package classes in a DEX file. NB some
  # tests provide classes rather than java files.
  if [ "$(ls -A classes)" ]; then
    if [ ${NEED_DEX} = "true" ]; then
      make_dex classes
    fi
  fi
fi
 
if [[ "${HAS_JASMIN}" == true ]]; then
  # Compile Jasmin classes as if they were part of the classes.dex file.
  make_jasmin jasmin_classes $(find 'jasmin' -name '*.j')
  if [[ "${NEED_DEX}" == "true" ]]; then
    make_dex jasmin_classes
    make_dexmerge classes.dex jasmin_classes.dex
  else
    # Move jasmin classes into classes directory so that they are picked up with -cp classes.
    mkdir -p classes
    mv jasmin_classes/* classes
  fi
fi
 
if [ "${HAS_SMALI}" = "true" -a ${NEED_DEX} = "true" ]; then
  # Compile Smali classes
  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
  if [[ ! -s smali_classes.dex ]] ; then
    fail "${SMALI} produced no output."
  fi
  # Merge smali files into classes.dex, this takes priority over any jasmin files.
  make_dexmerge classes.dex smali_classes.dex
fi
 
# Compile Jasmin classes in jasmin-multidex as if they were part of the classes2.jar
if [[ "$HAS_JASMIN_MULTIDEX" == true ]]; then
  make_jasmin jasmin_classes2 $(find 'jasmin-multidex' -name '*.j')
 
  if [[ "${NEED_DEX}" == "true" ]]; then
    make_dex jasmin_classes2
    make_dexmerge classes2.dex jasmin_classes2.dex
  else
    # Move jasmin classes into classes2 directory so that they are picked up with -cp classes2.
    mkdir -p classes2
    mv jasmin_classes2/* classes2
  fi
fi
 
if [ "${HAS_SMALI_MULTIDEX}" = "true" -a ${NEED_DEX} = "true" ]; then
  # Compile Smali classes
  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
 
  # Merge smali_classes2.dex into classes2.dex
  make_dexmerge classes2.dex smali_classes2.dex
fi
 
if [ ${HAS_SRC_EX} = "true" ]; then
  # Build src-ex into classes-ex.
  # Includes 'src', 'src-art' source when compiling classes-ex, but exclude their .class files.
  if [[ "${HAS_SRC}" == "true" ]]; then
    mkdir -p classes-tmp-for-ex
    javac_with_bootclasspath -d classes-tmp-for-ex `find src -name '*.java'`
    src_tmp_for_ex="-cp classes-tmp-for-ex"
  fi
  if [[ "${HAS_SRC_ART}" == "true" ]]; then
    mkdir -p classes-tmp-for-ex
    javac_with_bootclasspath -d classes-tmp-for-ex `find src-art -name '*.java'`
    src_tmp_for_ex="-cp classes-tmp-for-ex"
  fi
  mkdir -p classes-ex
  javac_with_bootclasspath -d classes-ex $src_tmp_for_ex `find src-ex -name '*.java'`
fi
 
if [[ -d classes-ex ]] && [ ${NEED_DEX} = "true" ]; then
  make_dex classes-ex
fi
 
if [ "${HAS_SMALI_EX}" = "true" -a ${NEED_DEX} = "true" ]; then
  # Compile Smali classes
  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes-ex.dex `find smali-ex -name '*.smali'`
  if [[ ! -s smali_classes-ex.dex ]] ; then
    fail "${SMALI} produced no output."
  fi
  # Merge smali files into classes-ex.dex.
  make_dexmerge classes-ex.dex smali_classes-ex.dex
fi
 
if [[ -f classes-ex.dex ]]; then
  # Apply hiddenapi on the dex files if the test has API list file(s).
  if [ ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then
    make_hiddenapi classes-ex.dex
  fi
 
  # quick shuffle so that the stored name is "classes.dex"
  mv classes.dex classes-1.dex
  mv classes-ex.dex classes.dex
  zip $TEST_NAME-ex.jar classes.dex
  mv classes.dex classes-ex.dex
  mv classes-1.dex classes.dex
fi
 
# Apply hiddenapi on the dex files if the test has API list file(s).
if [ ${NEED_DEX} = "true" -a ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then
  if $(has_multidex); then
    make_hiddenapi classes.dex classes2.dex
  else
    make_hiddenapi classes.dex
  fi
fi
 
# Create a single dex jar with two dex files for multidex.
if [ ${NEED_DEX} = "true" ]; then
  if [ -f classes2.dex ] ; then
    zip $TEST_NAME.jar classes.dex classes2.dex
  else
    zip $TEST_NAME.jar classes.dex
  fi
fi