aboutsummaryrefslogtreecommitdiff
path: root/build/config/android/internal_rules.gni
blob: 885352749654455a4118cebb7b6b883309e6c0a6 (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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Do not add any imports to non-//build directories here.
# Some projects (e.g. V8) do not have non-build directories DEPS'ed in.
import("//build/config/android/channel.gni")
import("//build/config/android/config.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/compute_inputs_for_analyze.gni")
import("//build/config/coverage/coverage.gni")
import("//build/config/python.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/toolchain/goma.gni")
import("//build/toolchain/kythe.gni")
import("//build/util/generate_wrapper.gni")
import("//build_overrides/build.gni")
if (current_toolchain == default_toolchain) {
  import("//build/toolchain/concurrent_links.gni")
}
assert(is_android)

default_android_sdk_dep = "//third_party/android_sdk:android_sdk_java"
_kotlin_stdlib_dep = "//third_party/kotlin_stdlib:kotlin_stdlib_java"
_jacoco_dep = "//third_party/jacoco:jacocoagent_java"
_jacoco_host_jar =
    "$root_build_dir/lib.java/third_party/jacoco/jacocoagent_java.jar"
_robolectric_libs_dir =
    rebase_path(
        get_label_info("//:foo($robolectric_toolchain)", "root_out_dir"),
        root_build_dir)

# The following _java_*_types variables capture all the existing target types.
# If a new type is introduced, please add it to one of these categories,
# preferring the more specific resource/library types.
_java_resource_types = [
  "android_assets",
  "android_resources",
]

_java_library_types = [
  "java_library",
  "system_java_library",
  "android_app_bundle_module",
]

# These are leaf java target types. They cannot be passed as deps to other
# targets. Thus their naming schemes are not enforced.
_java_leaf_types = [
  "android_apk",
  "android_app_bundle",
  "dist_aar",
  "dist_jar",
  "java_annotation_processor",
  "java_binary",
  "robolectric_binary",
]

# All _java_resource_types targets must conform to these patterns.
java_resource_patterns = [
  "*_assets",
  "*_grd",
  "*_java_strings",
  "*locale_paks",
  "*_resources",
  "*strings_java",
  "*android*:assets",
  "*:*_apk_*resources",
  "*android*:resources",
]

# All _java_library_types targets must conform to these patterns. This includes
# all non-leaf targets that use java_library_impl.
java_library_patterns = [
  "*_java",
  "*_javalib",
  "*javatests",
  "*_bundle_module",
  "*:*_java_*",  # E.g. chrome_java_test_support
  "*:java",
  "*/java",  # to allow filtering without expanding labels //a/java ->
             # //a/java:java
  "*:junit",
  "*/junit",
  "*:junit_*",
  "*:*_junit_*",

  # TODO(agrieve): Rename to glue_java
  "//android_webview/glue",
  "//android_webview/glue:glue",
]

# These identify all non-leaf targets that have .build_config.json files. This is the
# set of patterns that other targets can use to filter out java targets.
java_target_patterns = java_library_patterns + java_resource_patterns

_r8_path = "//third_party/r8/lib/r8.jar"
_custom_r8_path = "//third_party/r8/custom_r8.jar"

# This duplication is intentional, so we avoid updating the r8.jar used by
# dexing unless necessary, since each update invalidates all incremental dexing
# and unnecessarily slows down all bots.
_d8_path = "//third_party/r8/d8/lib/r8.jar"
_custom_d8_path = "//third_party/r8/custom_d8.jar"
_default_lint_jar_path = "//third_party/android_build_tools/lint/lint.jar"
_custom_lint_jar_path = "//third_party/android_build_tools/lint/custom_lint.jar"
_manifest_merger_jar_path =
    "//third_party/android_build_tools/manifest_merger/manifest-merger.jar"

# Put the bug number in the target name so that false-positives have a hint in
# the error message about why non-existent dependencies are there.
build_config_target_suffix = "__build_config_crbug_908819"

# Write the target's .build_config.json file. This is a json file that contains a
# dictionary of information about how to build this target (things that
# require knowledge about this target's dependencies and cannot be calculated
# at gn-time). There is a special syntax to add a value in that dictionary to
# an action/action_foreachs args:
#   --python-arg=@FileArg($rebased_build_config_path:key0:key1)
# At runtime, such an arg will be replaced by the value in the build_config.
# See build/android/gyp/write_build_config.py and
# build/android/gyp/util/build_utils.py:ExpandFileArgs
template("write_build_config") {
  action_with_pydeps(target_name) {
    forward_variables_from(invoker, [ "testonly" ])
    _type = invoker.type
    _parent_invoker = invoker.invoker
    _target_label =
        get_label_info(":${_parent_invoker.target_name}", "label_no_toolchain")

    # Ensure targets match naming patterns so that __assetres, __header, __host,
    # and __validate targets work properly.
    if (filter_exclude([ _type ], _java_resource_types) == []) {
      if (filter_exclude([ _target_label ], java_resource_patterns) != []) {
        assert(false, "Invalid java resource target name: $_target_label")
      }
    } else if (filter_exclude([ _type ], _java_library_types) == []) {
      if (filter_exclude([ _target_label ], java_library_patterns) != [] ||
          filter_exclude([ _target_label ], java_resource_patterns) == []) {
        assert(false, "Invalid java library target name: $_target_label")
      }
    } else if (_type == "group") {
      if (filter_exclude([ _target_label ], java_target_patterns) != []) {
        assert(false, "Invalid java target name: $_target_label")
      }
    } else if (filter_exclude([ _type ], _java_leaf_types) != []) {
      assert(false, "This java type needs a category: $_type")
    }

    if (defined(invoker.public_target_label)) {
      _target_label = invoker.public_target_label
    }

    deps = []
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    if (defined(invoker.android_manifest_dep)) {
      deps += [ invoker.android_manifest_dep ]
    }

    script = "//build/android/gyp/write_build_config.py"
    depfile = "$target_gen_dir/$target_name.d"
    inputs = []
    outputs = [ invoker.build_config ]

    _deps_configs = []
    if (defined(invoker.possible_config_deps)) {
      foreach(_possible_dep, invoker.possible_config_deps) {
        _dep_label = get_label_info(_possible_dep, "label_no_toolchain")
        if (filter_exclude([ _dep_label ], java_target_patterns) == []) {
          deps += [ "$_dep_label$build_config_target_suffix" ]
          _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
          _dep_name = get_label_info(_possible_dep, "name")
          _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"

          _deps_configs += [ _dep_config ]
        }
      }
    }
    _public_deps_configs = []
    if (defined(invoker.possible_config_public_deps)) {
      foreach(_possible_dep, invoker.possible_config_public_deps) {
        _dep_label = get_label_info(_possible_dep, "label_no_toolchain")

        # E.g. Adding an action that generates a .java file that is then
        # consumed by a subsequent java_library() target would not work
        # because the libraries depend only on the nested targets of one
        # another. It is simplest to just ban non-java public_deps.
        assert(filter_exclude([ _dep_label ], java_target_patterns) == [],
               "Only java_library targets can be used as public_deps. " +
                   "Found:\n${_dep_label}\non Target:\n" +
                   get_label_info(":$target_name", "label_no_toolchain"))

        # Put the bug number in the target name so that false-positives
        # have a hint in the error message about non-existent dependencies.
        deps += [ "$_dep_label$build_config_target_suffix" ]
        _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
        _dep_name = get_label_info(_possible_dep, "name")
        _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"

        _public_deps_configs += [ _dep_config ]
      }
    }
    inputs += _deps_configs
    inputs += _public_deps_configs
    _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)
    _rebased_public_deps_configs =
        rebase_path(_public_deps_configs, root_build_dir)

    args = [
      "--type=$_type",
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--deps-configs=$_rebased_deps_configs",
      "--public-deps-configs=$_rebased_public_deps_configs",
      "--build-config",
      rebase_path(invoker.build_config, root_build_dir),
      "--gn-target",
      _target_label,
    ]

    if (defined(invoker.preferred_dep) && invoker.preferred_dep) {
      args += [ "--preferred-dep" ]
    }

    if (defined(invoker.aar_path)) {
      args += [
        "--aar-path",
        rebase_path(invoker.aar_path, root_build_dir),
      ]
    }

    if (defined(invoker.chromium_code) && !invoker.chromium_code) {
      # Default to chromium code if invoker did not pass anything.
      args += [ "--non-chromium-code" ]
    }

    if (defined(invoker.device_jar_path)) {
      args += [
        "--device-jar-path",
        rebase_path(invoker.device_jar_path, root_build_dir),
      ]
    }
    if (defined(invoker.host_jar_path)) {
      args += [
        "--host-jar-path",
        rebase_path(invoker.host_jar_path, root_build_dir),
      ]
    }
    if (defined(invoker.unprocessed_jar_path)) {
      args += [
        "--unprocessed-jar-path",
        rebase_path(invoker.unprocessed_jar_path, root_build_dir),
      ]
    }
    if (defined(invoker.ijar_path)) {
      args += [
        "--interface-jar-path",
        rebase_path(invoker.ijar_path, root_build_dir),
      ]
    }
    if (defined(invoker.kotlinc_jar_path)) {
      args += [
        "--kotlinc-jar-path",
        rebase_path(invoker.kotlinc_jar_path, root_build_dir),
      ]
    }
    if (defined(invoker.java_resources_jar)) {
      args += [
        "--java-resources-jar-path",
        rebase_path(invoker.java_resources_jar, root_build_dir),
      ]
    }
    if (defined(invoker.annotation_processor_deps) &&
        invoker.annotation_processor_deps != []) {
      _processor_configs = []
      foreach(_dep_label, invoker.annotation_processor_deps) {
        deps += [ "$_dep_label$build_config_target_suffix" ]
        _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
        _dep_name = get_label_info(_dep_label, "name")
        _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
        _processor_configs += [ _dep_config ]
      }
      _rebased_processor_configs =
          rebase_path(_processor_configs, root_build_dir)
      inputs += _processor_configs
      args += [ "--annotation-processor-configs=$_rebased_processor_configs" ]
    }

    # Dex path for library targets, or the the intermediate library for apks.
    if (defined(invoker.dex_path)) {
      args += [
        "--dex-path",
        rebase_path(invoker.dex_path, root_build_dir),
      ]
    }

    # Dex path for the final apk.
    if (defined(invoker.final_dex_path)) {
      args += [
        "--final-dex-path",
        rebase_path(invoker.final_dex_path, root_build_dir),
      ]
    }
    if (defined(invoker.supports_android) && invoker.supports_android) {
      args += [ "--supports-android" ]
    }
    if (defined(invoker.requires_android) && invoker.requires_android) {
      args += [ "--requires-android" ]
    }
    if (defined(invoker.is_prebuilt) && invoker.is_prebuilt) {
      args += [ "--is-prebuilt" ]
    }
    if (defined(invoker.bypass_platform_checks) &&
        invoker.bypass_platform_checks) {
      args += [ "--bypass-platform-checks" ]
    }
    if (defined(invoker.is_robolectric) && invoker.is_robolectric) {
      args += [ "--is-robolectric" ]
    }

    if (defined(invoker.apk_under_test)) {
      _dep_label = invoker.apk_under_test
      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
      _dep_name = get_label_info(_dep_label, "name")
      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
      inputs += [ _dep_config ]
      deps += [ "$_dep_label$build_config_target_suffix" ]
      args += [
        "--tested-apk-config",
        rebase_path(_dep_config, root_build_dir),
      ]
    }

    if (defined(invoker.asset_sources)) {
      _rebased_asset_sources =
          rebase_path(invoker.asset_sources, root_build_dir)
      args += [ "--asset-sources=$_rebased_asset_sources" ]
    }
    if (defined(invoker.asset_renaming_sources)) {
      _rebased_asset_renaming_sources =
          rebase_path(invoker.asset_renaming_sources, root_build_dir)
      args += [ "--asset-renaming-sources=$_rebased_asset_renaming_sources" ]

      # These are zip paths, so no need to rebase.
      args += [
        "--asset-renaming-destinations=${invoker.asset_renaming_destinations}",
      ]
    }
    if (defined(invoker.disable_compression) && invoker.disable_compression) {
      args += [ "--disable-asset-compression" ]
    }
    if (defined(invoker.treat_as_locale_paks) && invoker.treat_as_locale_paks) {
      args += [ "--treat-as-locale-paks" ]
    }

    if (defined(invoker.merged_android_manifest)) {
      args += [
        "--merged-android-manifest",
        rebase_path(invoker.merged_android_manifest, root_build_dir),
      ]
    }
    if (defined(invoker.android_manifest)) {
      inputs += [ invoker.android_manifest ]
      args += [
        "--android-manifest",
        rebase_path(invoker.android_manifest, root_build_dir),
      ]
    }
    if (defined(invoker.resources_zip)) {
      args += [
        "--resources-zip",
        rebase_path(invoker.resources_zip, root_build_dir),
      ]
    }

    if (defined(invoker.resource_overlay) && invoker.resource_overlay) {
      args += [ "--resource-overlay" ]
    }

    if (defined(invoker.custom_package)) {
      args += [
        "--package-name",
        invoker.custom_package,
      ]
    }
    if (defined(invoker.r_text)) {
      args += [
        "--r-text-path",
        rebase_path(invoker.r_text, root_build_dir),
      ]
    }
    if (defined(invoker.res_size_info_path)) {
      args += [
        "--res-size-info",
        rebase_path(invoker.res_size_info_path, root_build_dir),
      ]
    }
    if (defined(invoker.res_sources_path)) {
      _res_sources_path = rebase_path(invoker.res_sources_path, root_build_dir)
      args += [ "--res-sources-path=$_res_sources_path" ]
    }
    if (defined(invoker.proto_resources_path)) {
      _rebased_proto_resources =
          rebase_path(invoker.proto_resources_path, root_build_dir)
      args += [ "--apk-proto-resources=$_rebased_proto_resources" ]
    }
    if (defined(invoker.r_text_path)) {
      _rebased_rtxt_path = rebase_path(invoker.r_text_path, root_build_dir)
      args += [ "--r-text-path=$_rebased_rtxt_path" ]
    }
    if (defined(invoker.module_pathmap_path)) {
      _rebased_pathmap_path =
          rebase_path(invoker.module_pathmap_path, root_build_dir)
      args += [ "--module-pathmap-path=$_rebased_pathmap_path" ]
    }

    if (defined(invoker.shared_libraries_runtime_deps_file)) {
      # Don't list shared_libraries_runtime_deps_file as an input in order to
      # avoid having to depend on the runtime_deps target. See comment in
      # rules.gni for why we do this.
      args += [
        "--shared-libraries-runtime-deps",
        rebase_path(invoker.shared_libraries_runtime_deps_file, root_build_dir),
      ]
    }

    if (defined(invoker.base_allowlist_rtxt_path)) {
      args += [
        "--base-allowlist-rtxt-path",
        rebase_path(invoker.base_allowlist_rtxt_path, root_build_dir),
      ]
    }

    if (defined(invoker.loadable_modules)) {
      _rebased_loadable_modules =
          rebase_path(invoker.loadable_modules, root_build_dir)
      args += [ "--loadable-modules=$_rebased_loadable_modules" ]
    }

    if (defined(invoker.secondary_abi_shared_libraries_runtime_deps_file)) {
      # Don't list secondary_abi_shared_libraries_runtime_deps_file as an
      # input in order to avoid having to depend on the runtime_deps target.
      # See comment in rules.gni for why we do this.
      args += [
        "--secondary-abi-shared-libraries-runtime-deps",
        rebase_path(invoker.secondary_abi_shared_libraries_runtime_deps_file,
                    root_build_dir),
      ]
    }

    if (defined(invoker.secondary_abi_loadable_modules) &&
        invoker.secondary_abi_loadable_modules != []) {
      _rebased_secondary_abi_loadable_modules =
          rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir)
      args += [ "--secondary-abi-loadable-modules=$_rebased_secondary_abi_loadable_modules" ]
    }

    if (defined(invoker.native_lib_placeholders) &&
        invoker.native_lib_placeholders != []) {
      args += [ "--native-lib-placeholders=${invoker.native_lib_placeholders}" ]
    }

    if (defined(invoker.secondary_native_lib_placeholders) &&
        invoker.secondary_native_lib_placeholders != []) {
      args += [ "--secondary-native-lib-placeholders=${invoker.secondary_native_lib_placeholders}" ]
    }

    if (defined(invoker.library_always_compress)) {
      args += [ "--library-always-compress=${invoker.library_always_compress}" ]
    }

    if (defined(invoker.apk_path)) {
      # TODO(tiborg): Remove APK path from build config and use
      # install_artifacts from metadata instead.
      _rebased_apk_path = rebase_path(invoker.apk_path, root_build_dir)
      args += [ "--apk-path=$_rebased_apk_path" ]
      if (defined(invoker.incremental_apk_path)) {
        _rebased_incremental_apk_path =
            rebase_path(invoker.incremental_apk_path, root_build_dir)
        _rebased_incremental_install_json_path =
            rebase_path(invoker.incremental_install_json_path, root_build_dir)
        args += [
          "--incremental-install-json-path=$_rebased_incremental_install_json_path",
          "--incremental-apk-path=$_rebased_incremental_apk_path",
        ]
      }
    }

    if (defined(invoker.target_sources_file)) {
      args += [
        "--target-sources-file",
        rebase_path(invoker.target_sources_file, root_build_dir),
      ]
    }
    if (defined(invoker.srcjar)) {
      args += [
        "--srcjar",
        rebase_path(invoker.srcjar, root_build_dir),
      ]
    }
    if (defined(invoker.bundled_srcjars)) {
      _rebased_bundled_srcjars =
          rebase_path(invoker.bundled_srcjars, root_build_dir)
      args += [ "--bundled-srcjars=$_rebased_bundled_srcjars" ]
    }
    if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
      args += [ "--proguard-enabled" ]
    }
    if (defined(invoker.proguard_mapping_path)) {
      _rebased_proguard_mapping_path =
          rebase_path(invoker.proguard_mapping_path, root_build_dir)
      args += [ "--proguard-mapping-path=$_rebased_proguard_mapping_path" ]
    }
    if (defined(invoker.input_jars_paths)) {
      _rebased_input_jars_paths =
          rebase_path(invoker.input_jars_paths, root_build_dir)
      args += [ "--extra-classpath-jars=$_rebased_input_jars_paths" ]
    }
    if (defined(invoker.low_classpath_priority) &&
        invoker.low_classpath_priority) {
      args += [ "--low-classpath-priority" ]
    }
    if (defined(invoker.mergeable_android_manifests)) {
      _rebased_mergeable_android_manifests =
          rebase_path(invoker.mergeable_android_manifests, root_build_dir)
      args += [
        "--mergeable-android-manifests=$_rebased_mergeable_android_manifests",
      ]
    }
    if (defined(invoker.proguard_configs)) {
      _rebased_proguard_configs =
          rebase_path(invoker.proguard_configs, root_build_dir)
      args += [ "--proguard-configs=$_rebased_proguard_configs" ]
    }
    if (defined(invoker.gradle_treat_as_prebuilt) &&
        invoker.gradle_treat_as_prebuilt) {
      args += [ "--gradle-treat-as-prebuilt" ]
    }
    if (defined(invoker.main_class)) {
      args += [
        "--main-class",
        invoker.main_class,
      ]
    }
    if (defined(invoker.base_module_target)) {
      _dep_label = invoker.base_module_target
      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
      _dep_name = get_label_info(_dep_label, "name")
      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
      deps += [ "$_dep_label$build_config_target_suffix" ]
      inputs += [ _dep_config ]
      args += [
        "--base-module-build-config",
        rebase_path(_dep_config, root_build_dir),
      ]
    }
    if (defined(invoker.parent_module_target)) {
      _dep_label = invoker.parent_module_target
      _dep_gen_dir = get_label_info(_dep_label, "target_gen_dir")
      _dep_name = get_label_info(_dep_label, "name")
      _dep_config = "$_dep_gen_dir/$_dep_name.build_config.json"
      deps += [ "$_dep_label$build_config_target_suffix" ]
      inputs += [ _dep_config ]
      args += [
        "--parent-module-build-config",
        rebase_path(_dep_config, root_build_dir),
      ]
    }
    if (defined(invoker.module_name)) {
      args += [
        "--module-name",
        invoker.module_name,
      ]
    }
    if (defined(invoker.modules)) {
      foreach(_module, invoker.modules) {
        if (defined(_module.uses_split)) {
          args += [ "--uses-split=${_module.name}:${_module.uses_split}" ]
        }
      }
    }
    if (defined(invoker.module_build_configs)) {
      inputs += invoker.module_build_configs
      _rebased_configs =
          rebase_path(invoker.module_build_configs, root_build_dir)
      args += [ "--module-build-configs=$_rebased_configs" ]
    }
    if (defined(invoker.add_view_trace_events) &&
        invoker.add_view_trace_events) {
      # Adding trace events involves rewriting bytecode and generating a new set
      # of jar files. In order to avoid conflicts between bundles we save the
      # new jars in a bundle specific gen/ directory. The build config for the
      # bundle, and each one of its modules need a path to a bundle specific
      # gen/ directory in order to generate a list of rewritten jar paths.
      # We use the base module's target_gen_dir because non-base modules and the
      # app bundle targets have a reference to it (base_module_target).
      if (_type == "android_app_bundle") {
        _trace_events_target_name =
            get_label_info(_parent_invoker.base_module_target, "name")
      } else if (defined(invoker.base_module_target)) {
        _trace_events_target_name =
            get_label_info(invoker.base_module_target, "name")
      } else {
        _grandparent_invoker = _parent_invoker.invoker
        _trace_events_target_name = _grandparent_invoker.target_name
      }

      # FIXME: This should likely be using the base module's target_out_dir
      #     rather than the current target's.
      args += [
        "--trace-events-jar-dir",
        rebase_path("$target_out_dir/$_trace_events_target_name",
                    root_build_dir),
      ]
    }
    if (defined(invoker.version_name)) {
      args += [
        "--version-name",
        invoker.version_name,
      ]
    }
    if (defined(invoker.version_code)) {
      args += [
        "--version-code",
        invoker.version_code,
      ]
    }
    if (defined(invoker.recursive_resource_deps) &&
        invoker.recursive_resource_deps) {
      args += [ "--recursive-resource-deps" ]
    }
    if (current_toolchain != default_toolchain) {
      # This has to be a built-time error rather than a GN assert because many
      # packages have a mix of java and non-java targets. For example, the
      # following would fail even though nothing depends on :bar(//baz):
      #
      # shared_library("foo") {
      # }
      #
      # android_library("bar") {
      #   deps = [ ":foo(//baz)" ]
      #   assert(current_toolchain == default_toolchain)
      # }
      _msg = [
        "Tried to build an Android target in a non-default toolchain.",
        "target: $_target_label",
        "current_toolchain: $current_toolchain",
        "default_toolchain: $default_toolchain",
      ]
      args += [ "--fail=$_msg" ]
    }
  }
}

template("generate_android_wrapper") {
  generate_wrapper(target_name) {
    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    generator_script = "//build/android/gyp/generate_android_wrapper.py"
    sources = [
      "//build/android/gyp/util/build_utils.py",
      "//build/gn_helpers.py",
      "//build/util/generate_wrapper.py",
    ]
  }
}

template("generate_r_java") {
  action_with_pydeps(target_name) {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
    depfile = "$target_gen_dir/${invoker.target_name}.d"
    inputs = [ invoker.build_config ]
    outputs = [ invoker.srcjar_path ]
    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
    script = "//build/android/gyp/create_r_java.py"
    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--srcjar-out",
      rebase_path(invoker.srcjar_path, root_build_dir),
      "--deps-rtxts=@FileArg($_rebased_build_config:deps_info:dependency_r_txt_files)",
      "--r-package=${invoker.package}",
    ]
  }
}

# Generates a script in the build bin directory which runs the test
# target using the test runner script in build/android/test_runner.py.
template("test_runner_script") {
  testonly = true
  _test_name = invoker.test_name
  _test_type = invoker.test_type
  _is_unit_test = defined(invoker.is_unit_test) && invoker.is_unit_test
  _incremental_apk = defined(invoker.incremental_apk) && invoker.incremental_apk

  _runtime_deps =
      !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps

  if (_runtime_deps) {
    # This runtime_deps file is used at runtime and thus cannot go in
    # target_gen_dir.
    _target_dir_name = get_label_info(":$target_name", "dir")
    _runtime_deps_file =
        "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.runtime_deps"
    _runtime_deps_target = "${target_name}__write_deps"
    group(_runtime_deps_target) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "deps",
                               "public_deps",
                             ])
      data_deps = []
      if (defined(invoker.data_deps)) {
        data_deps += invoker.data_deps
      }
      if (defined(invoker.additional_apks)) {
        data_deps += invoker.additional_apks
      }
      write_runtime_deps = _runtime_deps_file
    }
  }

  if (defined(invoker.apk_under_test)) {
    _install_artifacts_json =
        "${target_gen_dir}/${target_name}.install_artifacts"
    _install_artifacts_target_name = "${target_name}__install_artifacts"
    generated_file(_install_artifacts_target_name) {
      deps = [ invoker.apk_under_test ]
      output_conversion = "json"
      outputs = [ _install_artifacts_json ]
      data_keys = [ "install_artifacts" ]
      walk_keys = [ "install_artifacts_barrier" ]
      rebase = root_build_dir
    }
  }

  generate_android_wrapper(target_name) {
    forward_variables_from(invoker,
                           [
                             "assert_no_deps",
                             "metadata",
                             "public_deps",
                             "visibility",
                           ])
    wrapper_script = "$root_build_dir/bin/run_${_test_name}"

    executable = "//testing/test_env.py"

    if (defined(invoker.android_test_runner_script)) {
      _runner_script = invoker.android_test_runner_script
    } else {
      _runner_script = "//build/android/test_runner.py"
    }

    deps = []
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
    data_deps = [
      "//build/android:test_runner_core_py",
      "//testing:test_scripts_shared",
    ]
    if (_test_type != "junit") {
      data_deps += [ "//build/android:test_runner_device_support" ]
    }
    if (defined(invoker.data_deps)) {
      data_deps += invoker.data_deps
    }
    data = []
    if (defined(invoker.data)) {
      data += invoker.data
    }

    executable_args = [
      "@WrappedPath(" + rebase_path(_runner_script, root_build_dir) + ")",
      _test_type,
      "--output-directory",
      "@WrappedPath(.)",
      "--wrapper-script-args",
    ]

    if (_is_unit_test) {
      executable_args += [ "--is-unit-test" ]
    }

    if (_runtime_deps) {
      deps += [ ":$_runtime_deps_target" ]
      data += [ _runtime_deps_file ]
      _rebased_runtime_deps_file =
          rebase_path(_runtime_deps_file, root_build_dir)
      executable_args += [
        "--runtime-deps-path",
        "@WrappedPath(${_rebased_runtime_deps_file})",
      ]
    }

    # apk_target is not used for native executable tests
    # (e.g. breakpad_unittests).
    if (defined(invoker.apk_target)) {
      assert(!defined(invoker.executable_dist_dir))
      deps += [ "${invoker.apk_target}$build_config_target_suffix" ]
      _apk_build_config =
          get_label_info(invoker.apk_target, "target_gen_dir") + "/" +
          get_label_info(invoker.apk_target, "name") + ".build_config.json"
      _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir)
      not_needed([ "_rebased_apk_build_config" ])
    } else if (_test_type == "gtest") {
      assert(
          defined(invoker.executable_dist_dir),
          "Must define either apk_target or executable_dist_dir for test_runner_script()")
      _rebased_executable_dist_dir =
          rebase_path(invoker.executable_dist_dir, root_build_dir)
      executable_args += [
        "--executable-dist-dir",
        "@WrappedPath(${_rebased_executable_dist_dir})",
      ]
    }

    if (use_jacoco_coverage) {
      # Keep in sync with recipe constant for recipe to find coverage data
      # files from local java tests: https://bit.ly/3Zul6do
      _jacoco_coverage_dir_name = "java_coverage"
    }

    _device_test = true
    if (_test_type == "gtest") {
      assert(defined(invoker.test_suite))
      executable_args += [
        "--suite",
        invoker.test_suite,
      ]
      if (use_clang_coverage) {
        # Set a default coverage output directory (can be overridden by user
        # passing the same flag).
        _rebased_coverage_dir =
            rebase_path("$root_out_dir/coverage", root_build_dir)
        executable_args += [
          "--coverage-dir",
          "@WrappedPath(${_rebased_coverage_dir})",
        ]
      }
    } else if (_test_type == "instrumentation") {
      _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))"
      if (_incremental_apk) {
        _test_apk = "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_apk_path))"
      }
      executable_args += [
        "--test-apk",
        _test_apk,
      ]
      if (defined(invoker.apk_under_test)) {
        if (_incremental_apk) {
          deps += [ "${invoker.apk_under_test}$build_config_target_suffix" ]
          _apk_under_test_build_config =
              get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" +
              get_label_info(invoker.apk_under_test, "name") +
              ".build_config.json"
          _rebased_apk_under_test_build_config =
              rebase_path(_apk_under_test_build_config, root_build_dir)
          _apk_under_test = "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path))"
        } else {
          deps += [ ":${_install_artifacts_target_name}" ]
          _rebased_install_artifacts_json =
              rebase_path(_install_artifacts_json, root_build_dir)
          _apk_under_test =
              "@WrappedPath(@FileArg($_rebased_install_artifacts_json[]))"
        }
        executable_args += [
          "--apk-under-test",
          _apk_under_test,
        ]
      }
      if (defined(invoker.use_webview_provider)) {
        deps += [ "${invoker.use_webview_provider}$build_config_target_suffix" ]
        _build_config =
            get_label_info(invoker.use_webview_provider, "target_gen_dir") +
            "/" + get_label_info(invoker.use_webview_provider, "name") +
            ".build_config.json"
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        executable_args += [
          "--use-webview-provider",
          "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))",
        ]
      }
      if (defined(invoker.proguard_mapping_path)) {
        if (_incremental_apk) {
          not_needed(invoker, [ "proguard_mapping_path" ])
        } else {
          data += [ invoker.proguard_mapping_path ]
          _rebased_mapping_path =
              rebase_path(invoker.proguard_mapping_path, root_build_dir)
          executable_args += [
            "--proguard-mapping-path",
            "@WrappedPath($_rebased_mapping_path)",
          ]
        }
      }
      if (use_jacoco_coverage) {
        # Set a default coverage output directory (can be overridden by user
        # passing the same flag).
        _rebased_coverage_dir =
            rebase_path("$root_out_dir/$_jacoco_coverage_dir_name",
                        root_build_dir)
        executable_args += [
          "--coverage-dir",
          "@WrappedPath(${_rebased_coverage_dir})",
        ]
      }
    } else if (_test_type == "junit") {
      assert(defined(invoker.test_suite))
      _device_test = false
      executable_args += [
        "--test-suite",
        invoker.test_suite,
        "--native-libs-dir",
        "@WrappedPath($_robolectric_libs_dir)",
      ]

      # Test runner uses this generated wrapper script.
      data += [ "$root_build_dir/bin/helper/${invoker.test_suite}" ]

      deps += [ ":${invoker.test_suite}$build_config_target_suffix" ]

      _rebased_robolectric_runtime_deps_dir =
          rebase_path("//third_party/robolectric/lib", root_build_dir)
      _rebased_resource_apk = rebase_path(invoker.resource_apk, root_build_dir)
      executable_args += [
        "--resource-apk",
        "@WrappedPath(${_rebased_resource_apk})",
        "--robolectric-runtime-deps-dir",
        "@WrappedPath(${_rebased_robolectric_runtime_deps_dir})",
      ]
      if (use_jacoco_coverage) {
        # Set a default coverage output directory (can be overridden by user
        # passing the same flag).
        _rebased_coverage_dir =
            rebase_path("$root_out_dir/$_jacoco_coverage_dir_name",
                        root_build_dir)
        executable_args += [
          "--coverage-dir",
          "@WrappedPath(${_rebased_coverage_dir})",
        ]
      }
    } else if (_test_type == "linker") {
      executable_args += [
        "--test-apk",
        "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:apk_path))",
      ]
    } else {
      assert(false, "Invalid test type: $_test_type.")
    }

    # Devil does not reliably work with component builds of its tools.
    # There's no benefit to them, so fall back to prebuilts for component builds.
    # https://crbug.com/1404180
    if (_test_type != "junit" && !is_component_build) {
      executable_args += [ "--use-local-devil-tools" ]
    }

    if (defined(invoker.additional_apks)) {
      foreach(additional_apk, invoker.additional_apks) {
        deps += [ "$additional_apk$build_config_target_suffix" ]
        _build_config =
            get_label_info(additional_apk, "target_gen_dir") + "/" +
            get_label_info(additional_apk, "name") + ".build_config.json"
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        executable_args += [
          "--additional-apk",
          "@WrappedPath(@FileArg($_rebased_build_config:deps_info:apk_path))",
        ]
      }
    }
    if (defined(invoker.shard_timeout)) {
      executable_args += [ "--shard-timeout=${invoker.shard_timeout}" ]
    }
    if (_incremental_apk) {
      executable_args += [
        "--test-apk-incremental-install-json",
        "@WrappedPath(@FileArg($_rebased_apk_build_config:deps_info:incremental_install_json_path))",
      ]
      if (defined(invoker.apk_under_test)) {
        executable_args += [
          "--apk-under-test-incremental-install-json",
          "@WrappedPath(@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_install_json_path))",
        ]
      }
      executable_args += [ "--fast-local-dev" ]
    }
    if (_device_test && is_asan) {
      executable_args += [ "--tool=asan" ]
    }

    if (defined(invoker.modules)) {
      foreach(module, invoker.modules) {
        executable_args += [
          "--module",
          module,
        ]
      }
    }

    if (defined(invoker.fake_modules)) {
      foreach(fake_module, invoker.fake_modules) {
        executable_args += [
          "--fake-module",
          fake_module,
        ]
      }
    }

    if (defined(invoker.additional_locales)) {
      foreach(locale, invoker.additional_locales) {
        executable_args += [
          "--additional-locale",
          locale,
        ]
      }
    }

    if (defined(invoker.extra_args)) {
      executable_args += invoker.extra_args
    }
  }
}

if (enable_java_templates) {
  template("android_lint") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

      # https://crbug.com/1098752 Fix for bot OOM (https://crbug.com/1098333).
      if (defined(java_cmd_pool_size)) {
        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
      } else {
        pool = "//build/toolchain:link_pool($default_toolchain)"
      }

      # Lint requires generated sources and generated resources from the build.
      # Turbine __header targets depend on all generated sources, and the
      # __assetres targets depend on all generated resources.
      deps = []
      if (defined(invoker.deps)) {
        _lib_deps =
            filter_exclude(filter_include(invoker.deps, java_library_patterns),
                           java_resource_patterns)
        foreach(_lib_dep, _lib_deps) {
          # Expand //foo/java -> //foo/java:java
          _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
          deps += [
            "${_lib_dep}__assetres",
            "${_lib_dep}__header",
          ]
        }

        # Keep non-java deps as they may generate files used only by lint.
        # e.g. generated suppressions.xml files.
        deps += filter_exclude(invoker.deps, _lib_deps)
      }

      if (defined(invoker.min_sdk_version)) {
        _min_sdk_version = invoker.min_sdk_version
      } else {
        _min_sdk_version = default_min_sdk_version
      }

      if (defined(invoker.lint_jar_path)) {
        _lint_jar_path = invoker.lint_jar_path
      } else {
        _lint_jar_path = _default_lint_jar_path
      }

      # It is not safe to run two lint versions concurrently since they will
      # wipe the cache on version mismatch. When using a non-default lint
      # version, make each target use their own cache directory.
      _use_custom_cache_dir = _lint_jar_path != _default_lint_jar_path

      # Save generated xml files in a consistent location for debugging.
      if (defined(invoker.lint_gen_dir)) {
        _lint_gen_dir = invoker.lint_gen_dir
      } else {
        _lint_gen_dir = "$target_gen_dir/$target_name"
      }
      _backported_methods = "//third_party/r8/backported_methods.txt"

      script = "//build/android/gyp/lint.py"
      depfile = "$target_gen_dir/$target_name.d"
      inputs = java_paths_for_inputs + [
                 _lint_jar_path,
                 _custom_lint_jar_path,
                 _backported_methods,
               ]

      args = [
        "--target-name",
        get_label_info(":${target_name}", "label_no_toolchain"),
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--lint-jar-path",
        rebase_path(_lint_jar_path, root_build_dir),
        "--custom-lint-jar-path",
        rebase_path(_custom_lint_jar_path, root_build_dir),
        "--lint-gen-dir",
        rebase_path(_lint_gen_dir, root_build_dir),
        "--android-sdk-version=${lint_android_sdk_version}",
        "--min-sdk-version=$_min_sdk_version",
        "--android-sdk-root",
        rebase_path(lint_android_sdk_root, root_build_dir),
        "--backported-methods",
        rebase_path(_backported_methods, root_build_dir),
      ]

      if (!_use_custom_cache_dir) {
        _cache_dir = "$root_build_dir/android_lint_cache"
        _create_cache_stamp_path = "$_cache_dir/build.lint.stamp"

        # By default, lint.py will use "$_lint_gen_dir/cache".
        args += [
          "--cache-dir",
          rebase_path(_cache_dir, root_build_dir),
        ]
      }

      if (defined(invoker.skip_build_server) && invoker.skip_build_server) {
        # Nocompile tests need lint to fail through ninja.
        args += [ "--skip-build-server" ]
      } else if (android_static_analysis == "build_server") {
        args += [ "--use-build-server" ]
      }

      if (defined(invoker.lint_suppressions_file)) {
        inputs += [ invoker.lint_suppressions_file ]

        args += [
          "--config-path",
          rebase_path(invoker.lint_suppressions_file, root_build_dir),
        ]
      }

      if (defined(testonly) && testonly) {
        # Allows us to ignore unnecessary checks when linting test targets.
        args += [ "--testonly" ]
      }

      if (defined(invoker.manifest_package)) {
        args += [ "--manifest-package=${invoker.manifest_package}" ]
      }

      if (treat_warnings_as_errors) {
        args += [ "--warnings-as-errors" ]
      }

      if (defined(invoker.lint_baseline_file)) {
        if (compute_inputs_for_analyze) {
          # The baseline file is included in lint.py as a depfile dep. Since
          # removing it regenerates the file, it is useful to not have this as
          # a gn input during local development. Add it only for bots' analyze.
          inputs += [ invoker.lint_baseline_file ]
        }
        args += [
          # Baseline allows us to turn on lint warnings without fixing all the
          # pre-existing issues. This stops the flood of new issues while the
          # existing ones are being fixed.
          "--baseline",
          rebase_path(invoker.lint_baseline_file, root_build_dir),
        ]
      }

      if (defined(invoker.create_cache) && invoker.create_cache) {
        # Putting the stamp file in the cache dir allows us to depend on ninja
        # to create the cache dir for us.
        args += [ "--create-cache" ]
        _stamp_path = _create_cache_stamp_path
      } else {
        _stamp_path = "$target_out_dir/$target_name/build.lint.stamp"
        deps += [ invoker.build_config_dep ]
        if (!_use_custom_cache_dir) {
          deps += [ "//build/android:prepare_android_lint_cache" ]
          inputs += [ _create_cache_stamp_path ]
        }
        inputs += [ invoker.build_config ]
        _rebased_build_config =
            rebase_path(invoker.build_config, root_build_dir)

        args += [
          "--manifest-path=@FileArg($_rebased_build_config:deps_info:lint_android_manifest)",
          "--extra-manifest-paths=@FileArg($_rebased_build_config:deps_info:lint_extra_android_manifests)",

          # Lint requires all source and all resource files to be passed in the
          # same invocation for checks like UnusedResources.
          "--sources=@FileArg($_rebased_build_config:deps_info:lint_sources)",
          "--aars=@FileArg($_rebased_build_config:deps_info:lint_aars)",
          "--srcjars=@FileArg($_rebased_build_config:deps_info:lint_srcjars)",
          "--resource-sources=@FileArg($_rebased_build_config:deps_info:lint_resource_sources)",
          "--resource-zips=@FileArg($_rebased_build_config:deps_info:lint_resource_zips)",

          # The full classpath is required for annotation checks like @IntDef.
          "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)",
        ]
      }

      outputs = [ _stamp_path ]
      args += [
        "--stamp",
        rebase_path(_stamp_path, root_build_dir),
      ]
    }
  }

  template("proguard") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    _script = "//build/android/gyp/proguard.py"
    _deps = invoker.deps

    _inputs = java_paths_for_inputs + [
                invoker.build_config,
                _r8_path,
                _custom_r8_path,
              ]
    if (defined(invoker.inputs)) {
      _inputs += invoker.inputs
    }
    if (defined(invoker.proguard_mapping_path)) {
      _mapping_path = invoker.proguard_mapping_path
    } else {
      _mapping_path = "${invoker.output_path}.mapping"
    }

    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)

    # This is generally the apk name, and serves to identify the mapping
    # file that would be required to deobfuscate a stacktrace.
    _mapping_basename = get_path_info(_mapping_path, "name")
    _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)"
    _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)"
    if (defined(invoker.package_name)) {
      _package_name = invoker.package_name
    }
    if (defined(invoker.version_code)) {
      _version_code = invoker.version_code
    }

    # The Mapping ID is parsed to when uploading mapping files.
    # See: https://crbug.com/1417308
    _source_file_template =
        "chromium-$_mapping_basename-$android_channel-$_version_code"

    _args = [
      "--mapping-output",
      rebase_path(_mapping_path, root_build_dir),
      "--classpath",
      "@FileArg($_rebased_build_config:deps_info:proguard_classpath_jars)",
      "--classpath",
      "@FileArg($_rebased_build_config:android:sdk_jars)",
      "--r8-path",
      rebase_path(_r8_path, root_build_dir),
      "--custom-r8-path",
      rebase_path(_custom_r8_path, root_build_dir),
      "--package-name=$_package_name",
      "--source-file",
      _source_file_template,
    ]
    if (treat_warnings_as_errors) {
      _args += [ "--warnings-as-errors" ]
    }

    if ((!defined(invoker.proguard_enable_obfuscation) ||
         invoker.proguard_enable_obfuscation) && enable_proguard_obfuscation) {
      _args += [ "--enable-obfuscation" ]
    }
    if (defined(invoker.repackage_classes)) {
      _args += [ "--repackage-classes=" + invoker.repackage_classes ]
    }

    if (defined(invoker.modules)) {
      foreach(_feature_module, invoker.modules) {
        _rebased_module_build_config =
            rebase_path(_feature_module.build_config, root_build_dir)
        _args += [
          "--feature-name=${_feature_module.name}",
          "--dex-dest=@FileArg($_rebased_module_build_config:final_dex:path)",
        ]

        # The bundle's build config has the correct classpaths - the individual
        # modules' build configs may double-use some jars.
        if (defined(invoker.add_view_trace_events) &&
            invoker.add_view_trace_events) {
          _args += [ "--feature-jars=@FileArg($_rebased_build_config:modules:${_feature_module.name}:trace_event_rewritten_device_classpath)" ]
        } else {
          _args += [ "--feature-jars=@FileArg($_rebased_build_config:modules:${_feature_module.name}:device_classpath)" ]
        }

        if (defined(_feature_module.uses_split)) {
          _args += [ "--uses-split=${_feature_module.name}:${_feature_module.uses_split}" ]
        }
        _deps += [ _feature_module.build_config_target ]
      }
      _stamp = "${target_gen_dir}/${target_name}.r8.stamp"
      _outputs = [ _stamp ]
      _output_arg = [
        "--stamp",
        rebase_path(_stamp, root_build_dir),
      ]
    } else {
      # We don't directly set the output arg on the _args variable since it is
      # shared with the expectation target that uses its own stamp file and
      # does not take an --output-path.
      _output_arg = [
        "--output-path",
        rebase_path(invoker.output_path, root_build_dir),
      ]
      _outputs = [ invoker.output_path ]
    }
    _outputs += [ _mapping_path ]

    if (defined(invoker.input_art_profile)) {
      _inputs += [ invoker.input_art_profile ]
      _args += [ "--input-art-profile=" +
                 rebase_path(invoker.input_art_profile, root_build_dir) ]
      if (defined(invoker.output_art_profile)) {
        _outputs += [ invoker.output_art_profile ]
        _args += [ "--output-art-profile=" +
                   rebase_path(invoker.output_art_profile, root_build_dir) ]
      }
      if (defined(invoker.enable_startup_profile) &&
          invoker.enable_startup_profile) {
        _args += [ "--apply-startup-profile" ]
      }
    }

    if (defined(invoker.enable_proguard_checks) &&
        !invoker.enable_proguard_checks) {
      _args += [ "--disable-checks" ]
    }

    _ignore_desugar_missing_deps =
        defined(invoker.ignore_desugar_missing_deps) &&
        invoker.ignore_desugar_missing_deps
    if (!_ignore_desugar_missing_deps) {
      _args += [ "--show-desugar-default-interface-warnings" ]
    }

    if (defined(invoker.custom_assertion_handler)) {
      _args += [
        "--assertion-handler",
        invoker.custom_assertion_handler,
      ]
    } else if (enable_java_asserts) {
      # The default for generating dex file format is
      # --force-disable-assertions.
      _args += [ "--force-enable-assertions" ]
    }

    if (defined(invoker.args)) {
      _args += invoker.args
    }

    if (defined(invoker.expected_proguard_config)) {
      _expectations_target =
          "${invoker.top_target_name}_validate_proguard_config"
      action_with_pydeps(_expectations_target) {
        script = _script

        # Need to depend on all deps so that proguard.txt within .aar files get
        # extracted.
        deps = _deps
        depfile = "${target_gen_dir}/${target_name}.d"
        inputs = [
          invoker.build_config,
          invoker.expected_proguard_config,
        ]
        _actual_file = "$target_gen_dir/$target_name.proguard_configs"
        _failure_file =
            "$expectations_failure_dir/" +
            string_replace(invoker.expected_proguard_config, "/", "_")
        outputs = [
          _actual_file,
          _failure_file,
        ]
        args = _args + [
                 "--depfile",
                 rebase_path(depfile, root_build_dir),
                 "--failure-file",
                 rebase_path(_failure_file, root_build_dir),
                 "--expected-file",
                 rebase_path(invoker.expected_proguard_config, root_build_dir),
                 "--actual-file",
                 rebase_path(_actual_file, root_build_dir),
                 "--only-verify-expectations",
               ]
        if (defined(invoker.expected_proguard_config_base)) {
          inputs += [ invoker.expected_proguard_config_base ]
          args += [
            "--expected-file-base",
            rebase_path(invoker.expected_proguard_config_base, root_build_dir),
          ]
        }
        if (fail_on_android_expectations) {
          args += [ "--fail-on-expectations" ]
        }
      }
      _deps += [ ":$_expectations_target" ]
    }
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "public_deps",
                             ])
      script = _script
      deps = _deps
      inputs = _inputs
      outputs = _outputs
      depfile = "${target_gen_dir}/${target_name}.d"
      args = _args + _output_arg + [
               "--depfile",
               rebase_path(depfile, root_build_dir),
             ]

      # http://crbug.com/725224. Fix for bots running out of memory.
      if (defined(java_cmd_pool_size)) {
        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
      } else {
        pool = "//build/toolchain:link_pool($default_toolchain)"
      }
    }
  }

  # Generates a script in the build bin directory to run a java binary.
  #
  # Variables
  #   main_class: The class containing the program entry point.
  #   build_config: Path to .build_config.json for the jar (contains classpath).
  #   script_name: Name of the script to generate.
  #   wrapper_script_args: List of extra arguments to pass to the executable.
  #   tiered_stop_at_level_one: Whether to pass --tiered-stop-at-level-one
  #
  template("java_binary_script") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

      _main_class = invoker.main_class
      _build_config = invoker.build_config
      _script_name = invoker.script_name
      if (defined(invoker.max_heap_size)) {
        _max_heap_size = invoker.max_heap_size
      } else {
        _max_heap_size = "1G"
      }

      script = "//build/android/gyp/create_java_binary_script.py"
      inputs = [ _build_config ]
      _java_script = "$root_build_dir/bin/$_script_name"
      outputs = [ _java_script ]
      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      args = [
        "--output",
        rebase_path(_java_script, root_build_dir),
        "--main-class",
        _main_class,
        "--classpath=@FileArg($_rebased_build_config:deps_info:host_classpath)",
        "--max-heap-size=$_max_heap_size",
      ]
      data = []
      deps = [ "//third_party/jdk:java_data" ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      if (use_jacoco_coverage) {
        args += [
          "--classpath",
          rebase_path(_jacoco_host_jar, root_build_dir),
        ]
        data += [ _jacoco_host_jar ]
      }
      if (defined(invoker.tiered_stop_at_level_one) &&
          invoker.tiered_stop_at_level_one) {
        args += [ "--tiered-stop-at-level-one" ]
      }
      if (defined(invoker.extra_classpath_jars)) {
        _rebased_extra_classpath_jars =
            rebase_path(invoker.extra_classpath_jars, root_build_dir)
        args += [ "--classpath=${_rebased_extra_classpath_jars}" ]
        data += invoker.extra_classpath_jars
      }
      if (defined(invoker.wrapper_script_args)) {
        args += [ "--" ] + invoker.wrapper_script_args
      }
    }
  }

  # Variables
  #   apply_mapping: The path to the ProGuard mapping file to apply.
  #   disable_incremental: Disable incremental dexing.
  template("dex") {
    _min_sdk_version = default_min_sdk_version
    if (defined(invoker.min_sdk_version)) {
      _min_sdk_version = invoker.min_sdk_version
    }
    assert(
        _min_sdk_version >= min_supported_sdk_version,
        get_label_info(":$target_name", "label_no_toolchain") + " has an unsupported min_sdk_version of $_min_sdk_version (min is $min_supported_sdk_version)")

    _proguard_enabled =
        defined(invoker.proguard_enabled) && invoker.proguard_enabled
    _is_dex_merging = defined(invoker.input_dex_filearg)
    _enable_desugar = !defined(invoker.enable_desugar) || invoker.enable_desugar
    _desugar_needs_classpath = _enable_desugar

    # It's not safe to dex merge with libraries dex'ed at higher api versions.
    assert(!_is_dex_merging || _min_sdk_version >= default_min_sdk_version)

    # For D8's backported method desugaring to work properly, the dex merge step
    # must not be set to a higher minSdkVersion than it was for the libraries.
    if (_enable_desugar && _is_dex_merging) {
      _min_sdk_version = default_min_sdk_version
    }

    assert(defined(invoker.output) ||
           (_proguard_enabled && defined(invoker.modules)))
    assert(!_proguard_enabled || !(defined(invoker.input_dex_filearg) ||
                                       defined(invoker.input_classes_filearg) ||
                                       defined(invoker.input_class_jars)),
           "Cannot explicitly set inputs when proguarding a dex.")

    # Dex merging should not also be dexing.
    assert(!(_is_dex_merging && defined(invoker.input_classes_filearg)))
    assert(!(_is_dex_merging && defined(invoker.input_class_jars)))

    assert(!(defined(invoker.apply_mapping) && !_proguard_enabled),
           "apply_mapping can only be specified if proguard is enabled.")
    if (defined(invoker.custom_assertion_handler)) {
      assert(_proguard_enabled,
             "Proguard is required to support the custom assertion handler.")
    }

    if (_desugar_needs_classpath || _proguard_enabled) {
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
    }

    if (_proguard_enabled) {
      _proguard_target_name = target_name

      proguard(_proguard_target_name) {
        forward_variables_from(invoker,
                               TESTONLY_AND_VISIBILITY + [
                                     "add_view_trace_events",
                                     "input_art_profile",
                                     "output_art_profile",
                                     "enable_startup_profile",
                                     "build_config",
                                     "custom_assertion_handler",
                                     "data",
                                     "data_deps",
                                     "deps",
                                     "enable_proguard_checks",
                                     "expected_proguard_config",
                                     "expected_proguard_config_base",
                                     "ignore_desugar_missing_deps",
                                     "modules",
                                     "package_name",
                                     "proguard_enable_obfuscation",
                                     "proguard_mapping_path",
                                     "proguard_sourcefile_suffix",
                                     "repackage_classes",
                                     "top_target_name",
                                     "version_code",
                                   ])
        args = [
          "--proguard-configs=@FileArg($_rebased_build_config:deps_info:proguard_all_configs)",
          "--min-api=$_min_sdk_version",
        ]
        inputs = []
        if (defined(invoker.inputs)) {
          inputs += invoker.inputs
        }
        if (defined(invoker.proguard_configs)) {
          inputs += invoker.proguard_configs
          _rebased_proguard_configs =
              rebase_path(invoker.proguard_configs, root_build_dir)
          args += [ "--proguard-configs=$_rebased_proguard_configs" ]
        }

        if (defined(invoker.has_apk_under_test) && invoker.has_apk_under_test) {
          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath_extended)" ]
        } else if (defined(invoker.add_view_trace_events) &&
                   invoker.add_view_trace_events && defined(invoker.modules)) {
          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:trace_event_rewritten_device_classpath)" ]
        } else {
          args += [ "--input-paths=@FileArg($_rebased_build_config:deps_info:device_classpath)" ]
        }
        if (defined(invoker.proguard_args)) {
          args += invoker.proguard_args
        }

        if (defined(invoker.apply_mapping)) {
          _rebased_apply_mapping_path =
              rebase_path(invoker.apply_mapping, root_build_dir)
          args += [ "--apply-mapping=$_rebased_apply_mapping_path" ]
        }

        if (defined(invoker.output)) {
          output_path = invoker.output
        } else if (!defined(proguard_mapping_path)) {
          proguard_mapping_path = "$target_out_dir/$target_name.mapping"
        }
      }
    } else {  # !_proguard_enabled
      _is_library = defined(invoker.is_library) && invoker.is_library
      assert(!(defined(invoker.input_classes_filearg) && _is_library))
      assert(_is_library == defined(invoker.unprocessed_jar_path))
      _input_class_jars = []
      if (defined(invoker.input_class_jars)) {
        _input_class_jars = invoker.input_class_jars
      }
      _deps = invoker.deps

      if (_input_class_jars != []) {
        _rebased_input_class_jars =
            rebase_path(_input_class_jars, root_build_dir)
      }

      action_with_pydeps(target_name) {
        forward_variables_from(invoker,
                               TESTONLY_AND_VISIBILITY + [
                                     "data",
                                     "data_deps",
                                   ])
        script = "//build/android/gyp/dex.py"
        deps = _deps
        depfile = "$target_gen_dir/$target_name.d"
        outputs = [ invoker.output ]
        inputs = [
                   "$android_sdk/optional/android.test.base.jar",
                   "$android_sdk/optional/org.apache.http.legacy.jar",
                   "//third_party/jdk/current/bin/java",
                   _custom_d8_path,
                   _d8_path,
                   android_sdk_jar,
                 ] + java_paths_for_inputs
        if (defined(invoker.inputs)) {
          inputs += invoker.inputs
        }

        if (!_is_library) {
          # http://crbug.com/725224. Fix for bots running out of memory.
          if (defined(java_cmd_pool_size)) {
            pool = "//build/config/android:java_cmd_pool($default_toolchain)"
          } else {
            pool = "//build/toolchain:link_pool($default_toolchain)"
          }
        }

        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--output",
          rebase_path(invoker.output, root_build_dir),
          "--min-api=$_min_sdk_version",
          "--r8-jar-path",
          rebase_path(_d8_path, root_build_dir),
          "--custom-d8-jar-path",
          rebase_path(_custom_d8_path, root_build_dir),

          # Uncomment when rebuilding custom_d8.jar.
          #"--skip-custom-d8",
        ]
        if (treat_warnings_as_errors) {
          args += [ "--warnings-as-errors" ]
        }

        if (enable_incremental_d8 && !(defined(invoker.disable_incremental) &&
                                       invoker.disable_incremental)) {
          # Don't use incremental dexing for ProGuarded inputs as a precaution.
          args += [
            "--incremental-dir",
            rebase_path("$target_out_dir/$target_name", root_build_dir),
          ]
        }
        if (_is_library) {
          args += [ "--library" ]
        }
        if (defined(invoker.input_dex_filearg)) {
          inputs += [ invoker.build_config ]
          args += [ "--dex-inputs-filearg=${invoker.input_dex_filearg}" ]
        }
        if (defined(invoker.input_classes_filearg)) {
          inputs += [ invoker.build_config ]
          args += [ "--class-inputs-filearg=${invoker.input_classes_filearg}" ]

          # Required for the same reason as unprocessed_jar_path is added to
          # classpath (see note below).
          args += [ "--classpath=${invoker.input_classes_filearg}" ]
        }
        if (_input_class_jars != []) {
          inputs += _input_class_jars
          args += [ "--class-inputs=${_rebased_input_class_jars}" ]
        }

        # Never compile intemediates with --release in order to:
        # 1) not require recompiles when toggling is_java_debug,
        # 2) allow incremental_install=1 to still have local variable
        #    information even when is_java_debug=false.
        if (!is_java_debug && !_is_library) {
          args += [ "--release" ]
        }

        if (_enable_desugar) {
          args += [ "--desugar" ]

          _ignore_desugar_missing_deps =
              defined(invoker.ignore_desugar_missing_deps) &&
              invoker.ignore_desugar_missing_deps
          if (!_ignore_desugar_missing_deps) {
            args += [ "--show-desugar-default-interface-warnings" ]
          }
        }
        if (_desugar_needs_classpath) {
          # Cannot use header jar for the active jar, because it does not
          # contain anonymous classes. https://crbug.com/1342018#c5
          # Cannot use processed .jar here because it might have classes
          # filtered out via jar_excluded_patterns.
          # Must come first in classpath in order to take precedence over
          # deps that defined the same classes (via jar_excluded_patterns).
          if (defined(invoker.unprocessed_jar_path)) {
            args += [
              "--classpath",
              rebase_path(invoker.unprocessed_jar_path, root_build_dir),

              # Pass the full classpath to find new dependencies that are not in
              # the .desugardeps file.
              "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)",
            ]
            inputs += [ invoker.unprocessed_jar_path ]
          }
          _desugar_dependencies_path =
              "$target_gen_dir/$target_name.desugardeps"
          args += [
            "--desugar-dependencies",
            rebase_path(_desugar_dependencies_path, root_build_dir),
            "--bootclasspath=@FileArg($_rebased_build_config:android:sdk_jars)",
          ]
        }

        if (defined(invoker.custom_assertion_handler)) {
          args += [
            "--assertion-handler",
            invoker.custom_assertion_handler,
          ]
        } else if (enable_java_asserts) {
          # The default for generating dex file format is
          # --force-disable-assertions.
          args += [ "--force-enable-assertions" ]
        }
      }
    }
  }

  template("jacoco_instr") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "deps",
                                   "public_deps",
                                 ])

      # The name needs to match the SOURCES_JSON_FILES_SUFFIX in
      # generate_coverage_metadata_for_java.py.
      _sources_json_file = "$target_out_dir/${target_name}__jacoco_sources.json"
      _jacococli_jar = "//third_party/jacoco/lib/jacococli.jar"

      script = "//build/android/gyp/jacoco_instr.py"
      inputs = invoker.source_files + java_paths_for_inputs + [
                 _jacococli_jar,
                 invoker.input_jar_path,
               ]
      outputs = [
        _sources_json_file,
        invoker.output_jar_path,
      ]
      args = [
        "--input-path",
        rebase_path(invoker.input_jar_path, root_build_dir),
        "--output-path",
        rebase_path(invoker.output_jar_path, root_build_dir),
        "--sources-json-file",
        rebase_path(_sources_json_file, root_build_dir),
        "--target-sources-file",
        rebase_path(invoker.target_sources_file, root_build_dir),
        "--jacococli-jar",
        rebase_path(_jacococli_jar, root_build_dir),
      ]
      if (coverage_instrumentation_input_file != "") {
        args += [
          "--files-to-instrument",
          rebase_path(coverage_instrumentation_input_file, root_build_dir),
        ]
      }
    }
  }

  template("filter_jar") {
    action_with_pydeps(target_name) {
      script = "//build/android/gyp/filter_zip.py"
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "deps",
                                   "data",
                                   "data_deps",
                                 ])
      inputs = [ invoker.input_jar ]
      if (defined(invoker.inputs)) {
        inputs += invoker.inputs
      }
      outputs = [ invoker.output_jar ]

      _jar_excluded_patterns = []
      if (defined(invoker.jar_excluded_patterns)) {
        _jar_excluded_patterns = invoker.jar_excluded_patterns
      }
      _jar_included_patterns = []
      if (defined(invoker.jar_included_patterns)) {
        _jar_included_patterns = invoker.jar_included_patterns
      }
      args = [
        "--input",
        rebase_path(invoker.input_jar, root_build_dir),
        "--output",
        rebase_path(invoker.output_jar, root_build_dir),
        "--exclude-globs=${_jar_excluded_patterns}",
        "--include-globs=${_jar_included_patterns}",
      ]
    }
  }

  template("process_java_library") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _previous_output_jar = invoker.input_jar_path

    if (invoker.jacoco_instrument) {
      _filter_jar_target_name = "${target_name}__filter_jar"
      _filter_jar_output_jar = "$target_out_dir/$target_name.filter.jar"
    } else {
      _filter_jar_target_name = target_name
      _filter_jar_output_jar = invoker.output_jar_path
    }

    filter_jar(_filter_jar_target_name) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "jar_excluded_patterns",
                               "jar_included_patterns",
                             ])
      deps = invoker.deps
      input_jar = _previous_output_jar
      output_jar = _filter_jar_output_jar
    }

    if (invoker.jacoco_instrument) {
      # Jacoco must run after desugar (or else desugar sometimes fails).
      # It must run after filtering to avoid the same (filtered) class mapping
      # to multiple .jar files.
      # We run offline code coverage processing here rather than with a
      # javaagent as the desired coverage data was not being generated.
      # See crbug.com/1097815.
      jacoco_instr(target_name) {
        deps = [ ":$_filter_jar_target_name" ] + invoker.deps
        forward_variables_from(invoker,
                               [
                                 "source_files",
                                 "target_sources_file",
                               ])

        input_jar_path = _filter_jar_output_jar
        output_jar_path = invoker.output_jar_path
      }
    }
  }

  template("bytecode_processor") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "data_deps",
                                   "deps",
                                 ])
      script = "//build/android/gyp/bytecode_processor.py"
      inputs = java_paths_for_inputs + [
                 invoker.build_config,
                 invoker.input_jar,
               ]
      outputs = [ "$target_out_dir/$target_name.bytecode.stamp" ]
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
      args = [
        "--target-name",
        get_label_info(":${target_name}", "label_no_toolchain"),
        "--gn-target=${invoker.target_label}",
        "--input-jar",
        rebase_path(invoker.input_jar, root_build_dir),
        "--stamp",
        rebase_path(outputs[0], root_build_dir),
        "--chromium-output-dir",
        rebase_path(root_build_dir, root_build_dir),
        "--direct-classpath-jars=@FileArg($_rebased_build_config:javac:classpath)",
        "--full-classpath-jars=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
        "--full-classpath-gn-targets=@FileArg($_rebased_build_config:deps_info:javac_full_classpath_targets)",
      ]
      if (auto_add_missing_java_deps) {
        args += [ "--auto-add-deps" ]
      }
      if (android_static_analysis == "build_server") {
        args += [ "--use-build-server" ]
      }
      if (invoker.include_android_sdk) {
        args += [ "--sdk-classpath-jars=@FileArg($_rebased_build_config:android:sdk_jars)" ]
      }
      if (treat_warnings_as_errors) {
        args += [ "--warnings-as-errors" ]
      }
    }
  }

  template("merge_manifests") {
    action_with_pydeps(target_name) {
      assert(
          invoker.min_sdk_version >= min_supported_sdk_version,
          get_label_info(":$target_name", "label_no_toolchain") + " has an unsupported min_sdk_version of ${invoker.min_sdk_version} (min is $min_supported_sdk_version)")
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      script = "//build/android/gyp/merge_manifest.py"
      depfile = "$target_gen_dir/$target_name.d"

      inputs = java_paths_for_inputs + [
                 invoker.build_config,
                 invoker.input_manifest,
                 _manifest_merger_jar_path,
               ]

      outputs = [ invoker.output_manifest ]
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--manifest-merger-jar",
        rebase_path(_manifest_merger_jar_path, root_build_dir),
        "--root-manifest",
        rebase_path(invoker.input_manifest, root_build_dir),
        "--output",
        rebase_path(invoker.output_manifest, root_build_dir),
        "--extras",
        "@FileArg($_rebased_build_config:extra_android_manifests)",
        "--min-sdk-version=${invoker.min_sdk_version}",
        "--target-sdk-version=${invoker.target_sdk_version}",
      ]

      if (defined(invoker.manifest_package)) {
        args += [ "--manifest-package=${invoker.manifest_package}" ]
      }

      if (defined(invoker.max_sdk_version)) {
        args += [ "--max-sdk-version=${invoker.max_sdk_version}" ]
      }

      if (treat_warnings_as_errors) {
        args += [ "--warnings-as-errors" ]
      }
    }
  }

  # This template is used to parse a set of resource directories and
  # create the R.txt, .srcjar and .resources.zip for it.
  #
  # Input variables:
  #   deps: Specifies the input dependencies for this target.
  #
  #   build_config: Path to the .build_config.json file corresponding to the target.
  #
  #   sources:
  #     List of input resource files.
  #
  #   custom_package: (optional)
  #     Package name for the generated R.java source file. Optional if
  #     android_manifest is not provided.
  #
  #   android_manifest: (optional)
  #     If custom_package is not provided, path to an AndroidManifest.xml file
  #     that is only used to extract a package name out of it.
  #
  #   r_text_in_path: (optional)
  #     Path to an input R.txt file to use to generate the R.java file.
  #     The default is to use 'aapt' to generate the file from the content
  #     of the resource directories.
  #
  # Output variables:
  #   resources_zip:
  #     Path to a .resources.zip that will simply contain all the
  #     input resources, collected in a single archive.
  #
  #   r_text_out_path: Path for the generated R.txt file.
  #
  template("prepare_resources") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "deps",
                                   "public_deps",
                                   "sources",
                                 ])
      script = "//build/android/gyp/prepare_resources.py"

      depfile = "$target_gen_dir/${invoker.target_name}.d"
      outputs = [
        invoker.resources_zip,
        invoker.resources_zip + ".info",
        invoker.r_text_out_path,
      ]

      inputs = [ invoker.res_sources_path ]

      _rebased_res_sources_path =
          rebase_path(invoker.res_sources_path, root_build_dir)

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--res-sources-path=$_rebased_res_sources_path",
        "--resource-zip-out",
        rebase_path(invoker.resources_zip, root_build_dir),
        "--r-text-out",
        rebase_path(invoker.r_text_out_path, root_build_dir),
      ]

      if (defined(invoker.r_text_in_path)) {
        _r_text_in_path = invoker.r_text_in_path
        inputs += [ _r_text_in_path ]
        args += [
          "--r-text-in",
          rebase_path(_r_text_in_path, root_build_dir),
        ]
      }

      if (defined(invoker.strip_drawables) && invoker.strip_drawables) {
        args += [ "--strip-drawables" ]
      }
      if (defined(invoker.allow_missing_resources) &&
          invoker.allow_missing_resources) {
        args += [ "--allow-missing-resources" ]
      }
    }
  }

  # A template that is used to compile all resources needed by a binary
  # (e.g. an android_apk or a robolectric_binary) into an intermediate .ar_
  # archive. It can also generate an associated .srcjar that contains the
  # final R.java sources for all resource packages the binary depends on.
  #
  # Input variables:
  #   android_sdk_dep: The sdk dep that these resources should compile against.
  #
  #   deps: Specifies the input dependencies for this target.
  #
  #   build_config: Path to the .build_config.json file corresponding to the target.
  #
  #   build_config_dep: Dep target to generate the .build_config.json file.
  #
  #   android_manifest: Path to root manifest for the binary.
  #
  #   version_code: (optional)
  #
  #   version_name: (optional)
  #
  #   shared_resources: (optional)
  #     If true, make all variables in each generated R.java file non-final,
  #     and provide an onResourcesLoaded() method that can be used to reset
  #     their package index at load time. Useful when the APK corresponds to
  #     a library that is loaded at runtime, like system_webview_apk or
  #     monochrome_apk.
  #
  #   app_as_shared_lib: (optional)
  #     If true, same effect as shared_resources, but also ensures that the
  #     resources can be used by the APK when it is loaded as a regular
  #     application as well. Useful for the monochrome_public_apk target
  #     which is both an application and a shared runtime library that
  #     implements the system webview feature.
  #
  #   shared_resources_allowlist: (optional)
  #     Path to an R.txt file. If provided, acts similar to shared_resources
  #     except that it restricts the list of non-final resource variables
  #     to the list from the input R.txt file. Overrides shared_resources
  #     when both are specified.
  #
  #   shared_resources_allowlist_locales: (optional)
  #     If shared_resources_allowlist is used, provide an optional list of
  #     Chromium locale names to determine which localized shared string
  #     resources to put in the final output, even if aapt_locale_allowlist
  #     is defined to a smaller subset.
  #
  #   aapt_locale_allowlist: (optional)
  #     Restrict compiled locale-dependent resources to a specific allowlist.
  #     NOTE: This is a list of Chromium locale names, not Android ones.
  #
  #   r_java_root_package_name: (optional)
  #     Short package name for this target's root R java file (ex. input of
  #     "base" would become "gen.base_module" for the root R java package name).
  #     Optional as defaults to "base".
  #
  #   resource_exclusion_regex: (optional)
  #
  #   resource_exclusion_exceptions: (optional)
  #
  #   resource_values_filter_rules: (optional)
  #
  #   png_to_webp: (optional)
  #     If true, convert all PNG resources (except 9-patch files) to WebP.
  #
  #   post_process_script: (optional)
  #
  #   package_name: (optional)
  #     Name of the package for the purpose of creating R class.
  #
  #   package_id: (optional)
  #     Use a custom package ID in resource IDs.
  #
  #   arsc_package_name: (optional)
  #     Use this package name in the arsc file rather than the package name
  #     found in the AndroidManifest.xml. Does not affect the package name
  #     used in AndroidManifest.xml.
  #
  #   resource_ids_provider_dep: (optional)
  #     Use resource IDs provided by another APK target when compiling resources
  #     (via. "aapt2 link --stable-ids")
  #
  #   override_target_sdk: (optional)
  #     Update the manifest to target this SDK
  #
  # Output variables:
  #   arsc_output: Path to output .ap_ file (optional).
  #
  #   proto_output: Path to output .proto.ap_ file (optional).
  #
  #   r_text_out_path: (optional):
  #       Path for the corresponding generated R.txt file.
  #
  #   proguard_file: (optional)
  #       Path to proguard configuration file for this apk target.
  #
  template("compile_resources") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _deps = invoker.deps + [ invoker.build_config_dep ]
    if (defined(invoker.android_manifest_dep)) {
      _deps += [ invoker.android_manifest_dep ]
    }

    if (defined(invoker.arsc_output)) {
      _arsc_output = invoker.arsc_output
    }
    _final_srcjar_path = "${target_gen_dir}/${target_name}.srcjar"

    _script = "//build/android/gyp/compile_resources.py"

    _target_sdk_version = invoker.target_sdk_version
    if (defined(invoker.override_target_sdk)) {
      _target_sdk_version = invoker.override_target_sdk
    }

    _inputs = [
      invoker.build_config,
      android_sdk_tools_bundle_aapt2,
      android_sdk_jar,

      # TODO(b/315080809#comment4): remove these files after fixing
      # build/print_python_deps.py.
      "//third_party/protobuf/python/google/__init__.py",
      "//third_party/protobuf/python/google/protobuf/__init__.py",
      "//third_party/protobuf/python/google/protobuf/descriptor.py",
      "//third_party/protobuf/python/google/protobuf/descriptor_database.py",
      "//third_party/protobuf/python/google/protobuf/descriptor_pb2.py",
      "//third_party/protobuf/python/google/protobuf/descriptor_pool.py",
      "//third_party/protobuf/python/google/protobuf/internal/__init__.py",
      "//third_party/protobuf/python/google/protobuf/internal/api_implementation.py",
      "//third_party/protobuf/python/google/protobuf/internal/builder.py",
      "//third_party/protobuf/python/google/protobuf/internal/containers.py",
      "//third_party/protobuf/python/google/protobuf/internal/decoder.py",
      "//third_party/protobuf/python/google/protobuf/internal/encoder.py",
      "//third_party/protobuf/python/google/protobuf/internal/enum_type_wrapper.py",
      "//third_party/protobuf/python/google/protobuf/internal/extension_dict.py",
      "//third_party/protobuf/python/google/protobuf/internal/message_listener.py",
      "//third_party/protobuf/python/google/protobuf/internal/python_message.py",
      "//third_party/protobuf/python/google/protobuf/internal/type_checkers.py",
      "//third_party/protobuf/python/google/protobuf/internal/well_known_types.py",
      "//third_party/protobuf/python/google/protobuf/internal/wire_format.py",
      "//third_party/protobuf/python/google/protobuf/message.py",
      "//third_party/protobuf/python/google/protobuf/message_factory.py",
      "//third_party/protobuf/python/google/protobuf/reflection.py",
      "//third_party/protobuf/python/google/protobuf/symbol_database.py",
      "//third_party/protobuf/python/google/protobuf/text_encoding.py",
      "//third_party/protobuf/python/google/protobuf/text_format.py",
    ]

    _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)

    _rebased_android_sdk_jar = rebase_path(android_sdk_jar, root_build_dir)
    _args = [
      "--include-resources=$_rebased_android_sdk_jar",
      "--aapt2-path",
      rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
      "--dependencies-res-zips=@FileArg($_rebased_build_config:deps_info:dependency_zips)",
      "--extra-res-packages=@FileArg($_rebased_build_config:deps_info:extra_package_names)",
      "--min-sdk-version=${invoker.min_sdk_version}",
      "--target-sdk-version=${_target_sdk_version}",
      "--webp-cache-dir=obj/android-webp-cache",
    ]

    _inputs += [ invoker.android_manifest ]
    _outputs = [ _final_srcjar_path ]
    _args += [
      "--android-manifest",
      rebase_path(invoker.android_manifest, root_build_dir),
      "--srcjar-out",
      rebase_path(_final_srcjar_path, root_build_dir),
    ]
    if (defined(invoker.version_code)) {
      _args += [
        "--version-code",
        invoker.version_code,
      ]
    }
    if (defined(invoker.version_name)) {
      _args += [
        "--version-name",
        invoker.version_name,
      ]
    }
    if (defined(_arsc_output)) {
      _outputs += [ _arsc_output ]
      _args += [
        "--arsc-path",
        rebase_path(_arsc_output, root_build_dir),
      ]
    }
    if (defined(invoker.proto_output)) {
      _outputs += [ invoker.proto_output ]
      _args += [
        "--proto-path",
        rebase_path(invoker.proto_output, root_build_dir),
      ]
    }
    if (defined(invoker.size_info_path)) {
      _outputs += [ invoker.size_info_path ]
      _args += [
        "--info-path",
        rebase_path(invoker.size_info_path, root_build_dir),
      ]
    }

    if (defined(invoker.r_java_root_package_name)) {
      _args += [
        "--r-java-root-package-name",
        invoker.r_java_root_package_name,
      ]
    }

    # Useful to have android:debuggable in the manifest even for Release
    # builds. Just omit it for officai
    if (debuggable_apks) {
      _args += [ "--debuggable" ]
    }

    if (defined(invoker.r_text_out_path)) {
      _outputs += [ invoker.r_text_out_path ]
      _args += [
        "--r-text-out",
        rebase_path(invoker.r_text_out_path, root_build_dir),
      ]
    }

    if (defined(invoker.rename_manifest_package)) {
      _args += [
        "--rename-manifest-package",
        invoker.rename_manifest_package,
      ]
    }

    # Define the flags related to shared resources.
    #
    # Note the small sanity check to ensure that the package ID of the
    # generated resources table is correct. It should be 0x02 for runtime
    # shared libraries, and 0x7f otherwise.

    if (defined(invoker.shared_resources) && invoker.shared_resources) {
      _args += [ "--shared-resources" ]
    }
    if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) {
      _args += [ "--app-as-shared-lib" ]
    }
    if (defined(invoker.package_id)) {
      _args += [ "--package-id=${invoker.package_id}" ]
    }
    if (defined(invoker.package_name)) {
      _args += [
        "--package-name",
        invoker.package_name,
      ]
    }
    if (defined(invoker.arsc_package_name)) {
      _args += [
        "--arsc-package-name",
        invoker.arsc_package_name,
      ]
    }

    if (defined(invoker.shared_resources_allowlist)) {
      _inputs += [ invoker.shared_resources_allowlist ]
      _args += [
        "--shared-resources-allowlist",
        rebase_path(invoker.shared_resources_allowlist, root_build_dir),
      ]
    }
    if (defined(invoker.shared_resources_allowlist_locales)) {
      _args += [ "--shared-resources-allowlist-locales=" +
                 "${invoker.shared_resources_allowlist_locales}" ]
    }

    if (!defined(testonly) || !testonly ||
        (defined(invoker.enforce_resource_overlays_in_tests) &&
         invoker.enforce_resource_overlays_in_tests)) {
      _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zip_overlays)" ]
    } else {
      _args += [ "--dependencies-res-zip-overlays=@FileArg($_rebased_build_config:deps_info:dependency_zips)" ]
    }

    if (defined(invoker.proguard_file)) {
      _outputs += [ invoker.proguard_file ]
      _args += [
        "--proguard-file",
        rebase_path(invoker.proguard_file, root_build_dir),
      ]
    }

    if (defined(invoker.aapt_locale_allowlist)) {
      _args += [ "--locale-allowlist=${invoker.aapt_locale_allowlist}" ]
    }
    if (defined(invoker.png_to_webp) && invoker.png_to_webp) {
      _webp_target = "//third_party/libwebp:cwebp($host_toolchain)"
      _webp_binary = get_label_info(_webp_target, "root_out_dir") + "/cwebp"
      _deps += [ _webp_target ]
      _inputs += [ _webp_binary ]
      _args += [
        "--png-to-webp",
        "--webp-binary",
        rebase_path(_webp_binary, root_build_dir),
      ]
    }
    if (defined(invoker.resource_exclusion_regex)) {
      _args +=
          [ "--resource-exclusion-regex=${invoker.resource_exclusion_regex}" ]
      if (defined(invoker.resource_exclusion_exceptions)) {
        _args += [ "--resource-exclusion-exceptions=${invoker.resource_exclusion_exceptions}" ]
      }
    }
    if (defined(invoker.resource_values_filter_rules)) {
      _args +=
          [ "--values-filter-rules=${invoker.resource_values_filter_rules}" ]
    }

    if (defined(invoker.include_resource)) {
      _inputs += [ invoker.include_resource ]
      _rebased_include_resources =
          rebase_path(invoker.include_resource, root_build_dir)
      _args += [ "--include-resources=$_rebased_include_resources" ]
    }

    if (defined(invoker._args)) {
      _args += invoker._args
    }

    if (defined(invoker.emit_ids_out_path)) {
      _outputs += [ invoker.emit_ids_out_path ]
      _rebased_emit_ids_path =
          rebase_path(invoker.emit_ids_out_path, root_out_dir)
      _args += [ "--emit-ids-out=$_rebased_emit_ids_path" ]
    }

    if (defined(invoker.resource_ids_provider_dep)) {
      _compile_res_dep =
          "${invoker.resource_ids_provider_dep}__compile_resources"
      _gen_dir = get_label_info(_compile_res_dep, "target_gen_dir")
      _name = get_label_info(_compile_res_dep, "name")
      _resource_ids_path = "$_gen_dir/$_name.resource_ids"
      _inputs += [ _resource_ids_path ]
      _rebased_ids_path = rebase_path(_resource_ids_path, root_out_dir)
      _args += [ "--use-resource-ids-path=$_rebased_ids_path" ]
      _deps += [ _compile_res_dep ]
    }

    if (defined(invoker.max_sdk_version)) {
      _max_sdk_version = invoker.max_sdk_version
      _args += [ "--max-sdk-version=$_max_sdk_version" ]
    }

    if (defined(invoker.manifest_package)) {
      _args += [ "--manifest-package=${invoker.manifest_package}" ]
    }

    if (defined(invoker.is_bundle_module) && invoker.is_bundle_module) {
      _args += [ "--is-bundle-module" ]
    }

    if (defined(invoker.uses_split)) {
      assert(invoker.is_bundle_module)
      _args += [ "--uses-split=${invoker.uses_split}" ]
    }

    if (defined(invoker.expected_android_manifest)) {
      _expectations_target =
          "${invoker.top_target_name}_validate_android_manifest"
      action_with_pydeps(_expectations_target) {
        _actual_file = "${invoker.android_manifest}.normalized"
        _failure_file =
            "$expectations_failure_dir/" +
            string_replace(invoker.expected_android_manifest, "/", "_")
        inputs = [
          invoker.android_manifest,
          invoker.build_config,
          invoker.expected_android_manifest,
        ]
        outputs = [
          _actual_file,
          _failure_file,
        ]
        deps = [
          invoker.android_manifest_dep,
          invoker.build_config_dep,
        ]
        script = _script
        args = _args + [
                 "--expected-file",
                 rebase_path(invoker.expected_android_manifest, root_build_dir),
                 "--actual-file",
                 rebase_path(_actual_file, root_build_dir),
                 "--failure-file",
                 rebase_path(_failure_file, root_build_dir),
                 "--only-verify-expectations",
               ]
        if (defined(invoker.expected_android_manifest_base)) {
          args += [
            "--expected-file-base",
            rebase_path(invoker.expected_android_manifest_base, root_build_dir),
          ]
          inputs += [ invoker.expected_android_manifest_base ]
        }
        if (defined(invoker.expected_android_manifest_version_code_offset)) {
          args += [
            "--verification-version-code-offset",
            invoker.expected_android_manifest_version_code_offset,
          ]
        }
        if (defined(invoker.expected_android_manifest_library_version_offset)) {
          args += [
            "--verification-library-version-offset",
            invoker.expected_android_manifest_library_version_offset,
          ]
        }
        if (fail_on_android_expectations) {
          args += [ "--fail-on-expectations" ]
        }
      }
      _deps += [ ":$_expectations_target" ]
    }

    action_with_pydeps(target_name) {
      script = _script
      depfile = "$target_gen_dir/${target_name}.d"
      inputs = _inputs
      outputs = _outputs
      deps = _deps
      args = _args + [
               "--depfile",
               rebase_path(depfile, root_build_dir),
             ]
    }
  }

  # A template that is used to optimize compiled resources using aapt2 optimize.
  #
  #   proto_input_path:
  #     Path to input compiled .proto.ap_ file.
  #
  #   short_resource_paths: (optional)
  #     Rename the paths within a the apk to be randomly generated short
  #     strings to reduce binary size.
  #
  #   strip_resource_names: (optional)
  #     Strip resource names from the resources table of the apk.
  #
  #   resources_configs_paths: (optional)
  #     List of resource configs to use for optimization.
  #
  #   optimized_proto_output:
  #     Path to output optimized .proto.ap_ file.
  #
  #   resources_path_map_out_path: (optional):
  #       Path for the generated map between original resource paths and
  #       shortened resource paths.
  template("optimize_resources") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, [ "deps" ])
      script = "//build/android/gyp/optimize_resources.py"
      outputs = [ invoker.optimized_proto_output ]
      inputs = [
        android_sdk_tools_bundle_aapt2,
        invoker.r_text_path,
        invoker.proto_input_path,
      ]
      args = [
        "--aapt2-path",
        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
        "--r-text-in",
        rebase_path(invoker.r_text_path, root_build_dir),
        "--proto-path",
        rebase_path(invoker.proto_input_path, root_build_dir),
        "--optimized-proto-path",
        rebase_path(invoker.optimized_proto_output, root_build_dir),
      ]

      if (defined(invoker.resources_config_paths)) {
        inputs += invoker.resources_config_paths
        _rebased_resource_configs =
            rebase_path(invoker.resources_config_paths, root_build_dir)
        args += [ "--resources-config-paths=${_rebased_resource_configs}" ]
      }

      if (defined(invoker.short_resource_paths) &&
          invoker.short_resource_paths) {
        args += [ "--short-resource-paths" ]
        if (defined(invoker.resources_path_map_out_path)) {
          outputs += [ invoker.resources_path_map_out_path ]
          args += [
            "--resources-path-map-out-path",
            rebase_path(invoker.resources_path_map_out_path, root_build_dir),
          ]
        }
      }

      if (defined(invoker.strip_resource_names) &&
          invoker.strip_resource_names) {
        args += [ "--strip-resource-names" ]
      }
    }
  }

  # A template that is used to find unused resources.
  template("unused_resources") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      script = "//build/android/gyp/unused_resources.py"
      depfile = "$target_gen_dir/${target_name}.d"
      _unused_resources_script = "$root_build_dir/bin/helper/unused_resources"
      inputs = [ _unused_resources_script ] + java_paths_for_inputs
      outputs = [
        invoker.output_config,
        invoker.output_r_txt,
      ]
      if (!defined(deps)) {
        deps = []
      }
      deps += [ "//build/android/unused_resources:unused_resources" ]
      _rebased_module_build_config =
          rebase_path(invoker.build_config, root_build_dir)
      args = [
        "--script",
        rebase_path(_unused_resources_script, root_build_dir),
        "--output-config",
        rebase_path(invoker.output_config, root_build_dir),
        "--r-text-in=@FileArg($_rebased_module_build_config:deps_info:r_text_path)",
        "--r-text-out",
        rebase_path(invoker.output_r_txt, root_build_dir),
        "--dependencies-res-zips=@FileArg($_rebased_module_build_config:deps_info:dependency_zips)",
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]

      if (defined(invoker.proguard_mapping_path)) {
        inputs += [ invoker.proguard_mapping_path ]
        args += [
          "--proguard-mapping",
          rebase_path(invoker.proguard_mapping_path, root_build_dir),
        ]
      }

      foreach(_build_config, invoker.all_module_build_configs) {
        inputs += [ _build_config ]
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        args += [
          "--dexes=@FileArg($_rebased_build_config:final_dex:path)",
          "--android-manifests=@FileArg($_rebased_build_config:deps_info:merged_android_manifest)",
        ]
      }
    }
  }

  # Create an .jar.info file by merging several .jar.info files into one.
  #
  # Variables:
  #   build_config: Path to APK's build config file. Used to extract the
  #       list of input .jar files from its dependencies.
  #   name: Name of the apk or app bundle (e.g. "Foo.apk").
  #   res_size_info_path: Path to input .ap_.info file (for apks).
  #
  template("create_size_info_files") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      script = "//build/android/gyp/create_size_info_files.py"
      _jar_info_path = "$root_build_dir/size-info/${invoker.name}.jar.info"
      _pak_info_path = "$root_build_dir/size-info/${invoker.name}.pak.info"
      _res_info_path = "$root_build_dir/size-info/${invoker.name}.res.info"
      outputs = [
        _jar_info_path,
        _pak_info_path,
        _res_info_path,
      ]
      depfile = "$target_gen_dir/$target_name.d"
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--jar-info-path",
        rebase_path(_jar_info_path, root_build_dir),
        "--pak-info-path",
        rebase_path(_pak_info_path, root_build_dir),
        "--res-info-path",
        rebase_path(_res_info_path, root_build_dir),
      ]
      _is_bundle = defined(invoker.module_build_configs)
      if (_is_bundle) {
        inputs = invoker.module_build_configs
        foreach(_build_config, invoker.module_build_configs) {
          _rebased_build_config = rebase_path(_build_config, root_build_dir)
          args += [
            "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)",
            "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
            "--in-res-info-path=@FileArg($_rebased_build_config:deps_info:res_size_info)",
            "--assets=@FileArg($_rebased_build_config:assets)",
            "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
          ]
        }
      } else {
        inputs = [
          invoker.build_config,
          invoker.res_size_info_path,
        ]
        _rebased_build_config =
            rebase_path(invoker.build_config, root_build_dir)
        args += [
          "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)",
          "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
          "--in-res-info-path",
          rebase_path(invoker.res_size_info_path, root_build_dir),
          "--assets=@FileArg($_rebased_build_config:assets)",
          "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
        ]
      }
    }
  }

  template("create_binary_profile") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, [ "deps" ])
      script = "//build/android/gyp/binary_baseline_profile.py"
      depfile = "$target_gen_dir/$target_name.d"
      outputs = [
        invoker.binary_baseline_profile_path,
        invoker.binary_baseline_profile_metadata_path,
      ]
      _profgen_path =
          "$public_android_sdk_root/cmdline-tools/latest/bin/profgen"
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
      inputs = [
        invoker.build_config,
        invoker.input_profile_path,
        _profgen_path,
      ]
      args = [
        "--profgen",
        rebase_path(_profgen_path, root_build_dir),
        "--output-profile",
        rebase_path(invoker.binary_baseline_profile_path, root_build_dir),
        "--output-metadata",
        rebase_path(invoker.binary_baseline_profile_metadata_path,
                    root_build_dir),
        "--dex=@FileArg($_rebased_build_config:final_dex:path)",
        "--input-profile-path",
        rebase_path(invoker.input_profile_path, root_build_dir),
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]
      if (defined(invoker.proguard_mapping_path)) {
        args += [
          "--proguard-mapping",
          rebase_path(invoker.proguard_mapping_path, root_build_dir),
        ]
        inputs += [ invoker.proguard_mapping_path ]
      }
    }
  }

  # Creates a signed and aligned .apk.
  #
  # Variables
  #   apk_name: (optional) APK name (without .apk suffix). If provided, will
  #       be used to generate .info files later used by the supersize tool.
  #   assets_build_config: Path to android_apk .build_config.json containing merged
  #       asset information.
  #   deps: Specifies the dependencies of this target.
  #   dex_path: Path to classes.dex file to include (optional).
  #   expected_libs_and_assets: Verify the list of included native libraries
  #     and assets is consistent with the given expectation file.
  #   expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
  #     with this file as the base.
  #   packaged_resources_path: Path to .ap_ to use.
  #   output_apk_path: Output path for the generated .apk.
  #   min_sdk_version: The minimum Android SDK version this target supports.
  #   native_lib_placeholders: List of placeholder filenames to add to the apk
  #     (optional).
  #   secondary_native_lib_placeholders: List of placeholder filenames to add to
  #     the apk for the secondary ABI (optional).
  #   loadable_modules: List of native libraries.
  #   native_libs_filearg: @FileArg() of additionally native libraries.
  #   secondary_abi_loadable_modules: (optional) List of native libraries for
  #     secondary ABI.
  #   secondary_abi_native_libs_filearg: (optional). @FileArg() of additional
  #     secondary ABI native libs.
  #   keystore_path: Path to keystore to use for signing.
  #   keystore_name: Key alias to use.
  #   keystore_password: Keystore password.
  template("package_apk") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "public_deps" ])
    _is_robolectric_apk =
        defined(invoker.is_robolectric_apk) && invoker.is_robolectric_apk
    _deps = invoker.deps
    _native_lib_placeholders = []
    if (defined(invoker.native_lib_placeholders)) {
      _native_lib_placeholders = invoker.native_lib_placeholders
    }
    _secondary_native_lib_placeholders = []
    if (defined(invoker.secondary_native_lib_placeholders)) {
      _secondary_native_lib_placeholders =
          invoker.secondary_native_lib_placeholders
    }

    _script = "//build/android/gyp/apkbuilder.py"

    _inputs = [ invoker.packaged_resources_path ]

    _outputs = [ invoker.output_apk_path ]
    _data = [ invoker.output_apk_path ]

    _rebased_compiled_resources_path =
        rebase_path(invoker.packaged_resources_path, root_build_dir)
    _rebased_packaged_apk_path =
        rebase_path(invoker.output_apk_path, root_build_dir)
    _args = [
      "--resource-apk=$_rebased_compiled_resources_path",
      "--output-apk=$_rebased_packaged_apk_path",
      "--min-sdk-version=${invoker.min_sdk_version}",
    ]

    # system_image_stub_apk does not use a build_config.json.
    if (defined(invoker.build_config)) {
      _inputs += [ invoker.build_config ]
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
      _args += [
        "--assets=@FileArg($_rebased_build_config:assets)",
        "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
      ]
      if (!_is_robolectric_apk) {
        _args += [ "--java-resources=@FileArg($_rebased_build_config:java_resources_jars)" ]
      }
    }
    if (defined(invoker.extra_assets)) {
      _args += [ "--assets=${invoker.extra_assets}" ]
    }
    if (!_is_robolectric_apk) {
      _apksigner = "$android_sdk_build_tools/lib/apksigner.jar"
      _zipalign = "$android_sdk_build_tools/zipalign"
      _keystore_path = android_keystore_path
      _keystore_name = android_keystore_name
      _keystore_password = android_keystore_password

      if (defined(invoker.keystore_path)) {
        _keystore_path = invoker.keystore_path
        _keystore_name = invoker.keystore_name
        _keystore_password = invoker.keystore_password
      }

      _inputs += [
        _apksigner,
        _zipalign,
        _keystore_path,
      ]
      _args += [
        "--apksigner-jar",
        rebase_path(_apksigner, root_build_dir),
        "--zipalign-path",
        rebase_path(_zipalign, root_build_dir),
        "--key-path",
        rebase_path(_keystore_path, root_build_dir),
        "--key-name",
        _keystore_name,
        "--key-passwd",
        _keystore_password,
      ]
      if (is_official_build) {
        _args += [ "--best-compression" ]
      }
    }
    if (defined(invoker.uncompress_dex)) {
      _uncompress_dex = invoker.uncompress_dex
    } else {
      # Uncompressed dex support started on Android P.
      _uncompress_dex = invoker.min_sdk_version >= 28
    }

    if (_uncompress_dex) {
      _args += [ "--uncompress-dex" ]
    }
    if (defined(invoker.library_always_compress)) {
      _args +=
          [ "--library-always-compress=${invoker.library_always_compress}" ]
    }
    if (defined(invoker.dex_path)) {
      _inputs += [ invoker.dex_path ]
      _args += [
        "--dex-file",
        rebase_path(invoker.dex_path, root_build_dir),
      ]
    }
    if ((defined(invoker.loadable_modules) && invoker.loadable_modules != []) ||
        defined(invoker.native_libs_filearg) ||
        _native_lib_placeholders != []) {
      _args += [ "--android-abi=$android_app_abi" ]
    }
    if (defined(android_app_secondary_abi)) {
      _args += [ "--secondary-android-abi=$android_app_secondary_abi" ]
    }
    if (defined(invoker.loadable_modules) && invoker.loadable_modules != []) {
      _inputs += invoker.loadable_modules
      _rebased_loadable_modules =
          rebase_path(invoker.loadable_modules, root_build_dir)
      _args += [ "--native-libs=$_rebased_loadable_modules" ]
    }
    if (defined(invoker.native_libs_filearg)) {
      _args += [ "--native-libs=${invoker.native_libs_filearg}" ]
    }
    if (_native_lib_placeholders != []) {
      _args += [ "--native-lib-placeholders=$_native_lib_placeholders" ]
    }

    if (defined(invoker.secondary_abi_native_libs_filearg)) {
      _args += [
        "--secondary-native-libs=${invoker.secondary_abi_native_libs_filearg}",
      ]
    }
    if (defined(invoker.secondary_abi_loadable_modules)) {
      _rebased_secondary_abi_loadable_modules =
          rebase_path(invoker.secondary_abi_loadable_modules, root_build_dir)
      _args +=
          [ "--secondary-native-libs=$_rebased_secondary_abi_loadable_modules" ]
    }
    if (_secondary_native_lib_placeholders != []) {
      _args += [ "--secondary-native-lib-placeholders=$_secondary_native_lib_placeholders" ]
    }
    if (treat_warnings_as_errors) {
      _args += [ "--warnings-as-errors" ]
    }

    if (defined(invoker.expected_libs_and_assets)) {
      _expectations_target =
          "${invoker.top_target_name}_validate_libs_and_assets"
      action_with_pydeps(_expectations_target) {
        _actual_file = "$target_gen_dir/$target_name.libs_and_assets"
        _failure_file =
            "$expectations_failure_dir/" +
            string_replace(invoker.expected_libs_and_assets, "/", "_")
        inputs = [ invoker.expected_libs_and_assets ]
        if (defined(invoker.build_config)) {
          inputs += [ invoker.build_config ]
        }
        deps = [ invoker.build_config_dep ]
        outputs = [
          _actual_file,
          _failure_file,
        ]
        script = _script
        args = _args + [
                 "--expected-file",
                 rebase_path(invoker.expected_libs_and_assets, root_build_dir),
                 "--actual-file",
                 rebase_path(_actual_file, root_build_dir),
                 "--failure-file",
                 rebase_path(_failure_file, root_build_dir),
                 "--only-verify-expectations",
               ]
        if (defined(invoker.expected_libs_and_assets_base)) {
          inputs += [ invoker.expected_libs_and_assets_base ]
          args += [
            "--expected-file-base",
            rebase_path(invoker.expected_libs_and_assets_base, root_build_dir),
          ]
        }
        if (fail_on_android_expectations) {
          args += [ "--fail-on-expectations" ]
        }
      }
      _deps += [ ":$_expectations_target" ]
    }
    action_with_pydeps(target_name) {
      depfile = "$target_gen_dir/$target_name.d"
      inputs = _inputs
      deps = _deps
      data = _data
      outputs = _outputs
      script = _script
      args = _args + [
               "--depfile",
               rebase_path(depfile, root_build_dir),
             ]
    }
  }

  # Compile Java source files into a .jar file, potentially using an
  # annotation processor, and/or the errorprone compiler. Also includes Kotlin
  # source files in the resulting info file.
  #
  # Note that the only way to specify custom annotation processors is
  # by using build_config to point to a file that corresponds to a java-related
  # target that includes javac:processor_classes entries (i.e. there is no
  # variable here that can be used for this purpose).
  #
  # Note also the peculiar use of source_files / target_sources_file. The content
  # of the source_files list and the source files in target_sources_file file must
  # match exactly.
  #
  # Variables:
  #  main_target_name: Used when extracting srcjars for codesearch.
  #  source_files: Optional list of Java and Kotlin source file paths.
  #  srcjar_deps: Optional list of .srcjar dependencies (not file paths).
  #    The corresponding source files they contain will be compiled too.
  #  target_sources_file: Optional path to file containing list of source file
  #    paths. This must always be provided if java_files is not empty and the
  #    .java files in it must match the list of java_files exactly.
  #  build_config: Path to the .build_config.json file of the corresponding
  #    java_library_impl() target. The following entries will be used by this
  #    template: javac:srcjars, deps_info:javac_full_classpath,
  #    deps_info:javac_full_interface_classpath, javac:processor_classpath,
  #    javac:processor_classes
  #  javac_jar_path: Path to the final output .jar file.
  #  javac_args: Optional list of extra arguments to pass to javac.
  #  chromium_code: Whether this corresponds to Chromium-specific sources.
  #  requires_android: True if these sources can only run on Android.
  #  additional_jar_files: Optional list of files to copy into the resulting
  #    .jar file (by default, only .class files are put there). Each entry
  #    has the 'srcPath:dstPath' format.
  #  enable_errorprone: If True, use the errorprone compiler to check for
  #    error-prone constructs in the language. If not provided, whether this is
  #    enabled depends on chromium_code and the global
  #    use_errorprone_java_compiler variable.
  #  use_turbine: If True, compile headers using turbine.py.
  #  apk_name: Optional APK name. If provided, will tell compile_java.py to also
  #    generate an .apk.jar.info file under size-info/${apk_name}.apk.jar.info
  #  processor_args_javac: List of annotation processor arguments, each one
  #    will be passed to javac as -A<entry>.
  #  deps: Dependencies for the corresponding target.
  #  testonly: Usual meaning (should be True for test-only targets)
  #
  # [1] https://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html
  #
  template("compile_java") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _build_config = invoker.build_config
    _chromium_code = invoker.chromium_code

    _processor_args = []
    if (defined(invoker.processor_args_javac)) {
      _processor_args = invoker.processor_args_javac
    }

    _additional_jar_files = []
    if (defined(invoker.additional_jar_files)) {
      _additional_jar_files = invoker.additional_jar_files
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps = invoker.srcjar_deps
    }

    _java_srcjars = []
    foreach(dep, _srcjar_deps) {
      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
      _dep_name = get_label_info(dep, "name")
      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
    }

    # generated_jar_path is an output when use_turbine and an input otherwise.
    if (!invoker.use_turbine) {
      _java_srcjars += [ invoker.generated_jar_path ]
    }

    _javac_args = []
    if (defined(invoker.javac_args)) {
      _javac_args = invoker.javac_args
    }

    action_with_pydeps(target_name) {
      if (invoker.use_turbine) {
        script = "//build/android/gyp/turbine.py"
        inputs = [
          "//third_party/jdk/current/bin/java",
          android_sdk_jar,
        ]
      } else {
        script = "//build/android/gyp/compile_java.py"
        inputs = javac_paths_for_inputs
      }

      if (target_name == "chrome_java__header") {
        # Regression test for: https://crbug.com/1154302
        # Ensures that header jars never depend on non-header jars.
        assert_no_deps = [ "//base:base_java__compile_java" ]
      }

      depfile = "$target_gen_dir/$target_name.d"
      deps = _srcjar_deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      outputs = [ invoker.output_jar_path ]
      if (!invoker.enable_errorprone && !invoker.use_turbine) {
        outputs += [ invoker.output_jar_path + ".info" ]
      }
      inputs += invoker.source_files + _java_srcjars + [
                  "$android_sdk/optional/android.test.base.jar",
                  "$android_sdk/optional/org.apache.http.legacy.jar",
                  _build_config,
                ] + java_paths_for_inputs

      if (invoker.source_files != []) {
        inputs += [ invoker.target_sources_file ]
      }

      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      _rebased_output_jar_path =
          rebase_path(invoker.output_jar_path, root_build_dir)
      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
      _rebased_depfile = rebase_path(depfile, root_build_dir)
      _rebased_generated_dir = rebase_path(
              "$target_gen_dir/${invoker.main_target_name}/generated_java",
              root_build_dir)
      args = [
        "--depfile=$_rebased_depfile",
        "--generated-dir=$_rebased_generated_dir",
        "--jar-path=$_rebased_output_jar_path",
        "--java-srcjars=$_rebased_java_srcjars",
        "--target-name",
        get_label_info(":${target_name}", "label_no_toolchain"),
      ]

      # SDK jar must be first on classpath.
      if (invoker.include_android_sdk) {
        args += [ "--classpath=@FileArg($_rebased_build_config:android:sdk_interface_jars)" ]
      }

      if (defined(invoker.header_jar_path)) {
        inputs += [ invoker.header_jar_path ]
        args += [
          "--header-jar",
          rebase_path(invoker.header_jar_path, root_build_dir),
        ]
        _header_jar_classpath =
            [ rebase_path(invoker.header_jar_path, root_build_dir) ]
        args += [ "--classpath=$_header_jar_classpath" ]
      }

      if (defined(invoker.kotlin_jar_path)) {
        inputs += [ invoker.kotlin_jar_path ]
        _rebased_kotlin_jar_path =
            rebase_path(invoker.kotlin_jar_path, root_build_dir)
        args += [
          "--kotlin-jar-path=$_rebased_kotlin_jar_path",
          "--classpath=$_rebased_kotlin_jar_path",
        ]
      }

      if (invoker.use_turbine) {
        # Prefer direct deps for turbine as much as possible.
        args += [ "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
      } else {
        args += [ "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)" ]
      }

      if (invoker.use_turbine) {
        args += [
          "--processorpath=@FileArg($_rebased_build_config:javac:processor_classpath)",
          "--processors=@FileArg($_rebased_build_config:javac:processor_classes)",
        ]
      }

      if (invoker.use_turbine) {
        _turbine_jar_path = "//third_party/turbine/turbine.jar"
        inputs += [ _turbine_jar_path ]
        outputs += [ invoker.generated_jar_path ]
        args += [
          "--turbine-jar-path",
          rebase_path(_turbine_jar_path, root_build_dir),
          "--generated-jar-path",
          rebase_path(invoker.generated_jar_path, root_build_dir),
        ]
      }

      # Flag enable_kythe_annotations requires
      # checkout_android_prebuilts_build_tools=True in .gclient.
      if (enable_kythe_annotations && !invoker.enable_errorprone) {
        args += [ "--enable-kythe-annotations" ]
      }
      if (_chromium_code) {
        args += [ "--chromium-code=1" ]
        if (treat_warnings_as_errors) {
          args += [ "--warnings-as-errors" ]
        }
      }
      if (defined(invoker.jar_excluded_patterns)) {
        args += [ "--jar-info-exclude-globs=${invoker.jar_excluded_patterns}" ]
      }

      if (invoker.enable_errorprone) {
        # Our custom plugin pulls in the main errorprone dep transitively.
        _errorprone_dep = "//tools/android/errorprone_plugin:errorprone_plugin"
        deps += [ _errorprone_dep ]
        _dep_gen_dir = get_label_info(_errorprone_dep, "target_gen_dir")
        _dep_name = get_label_info(_errorprone_dep, "name")
        _rebased_errorprone_buildconfig =
            rebase_path("$_dep_gen_dir/$_dep_name.build_config.json",
                        root_build_dir)
        args += [
          "--processorpath=@FileArg($_rebased_errorprone_buildconfig:deps_info:host_classpath)",
          "--enable-errorprone",
        ]
        inputs += [
          # errorprone requires the plugin directory to detect src dir.
          # https://source.chromium.org/chromium/chromium/src/+/main:tools/android/errorprone_plugin/src/org/chromium/tools/errorprone/plugin/UseNetworkAnnotations.java;l=84;drc=dfd88085261b662a5c0a1abea1a3b120b08e8e48
          "//tools/android/errorprone_plugin/OWNERS",
        ]
      }
      if (defined(invoker.skip_build_server) && invoker.skip_build_server) {
        # Nocompile tests need lint to fail through ninja.
        args += [ "--skip-build-server" ]
      } else if (android_static_analysis == "build_server") {
        args += [ "--use-build-server" ]
      }

      foreach(e, _processor_args) {
        args += [ "--processor-arg=" + e ]
      }

      foreach(file_tuple, _additional_jar_files) {
        # Each element is of length two, [ path_to_file, path_to_put_in_jar ]
        inputs += [ file_tuple[0] ]
        args +=
            [ "--additional-jar-file=" +
              rebase_path(file_tuple[0], root_build_dir) + ":" + file_tuple[1] ]
      }
      if (invoker.source_files != []) {
        args +=
            [ "@" + rebase_path(invoker.target_sources_file, root_build_dir) ]
      }
      foreach(e, _javac_args) {
        args += [ "--javac-arg=" + e ]
      }
    }
  }

  # Compile Kotlin source files into .class files and store them in a .jar.
  # This explicitly does not run annotation processing on the Kotlin files.
  # Java files and srcjars are also passed to kotlinc for reference, although
  # no .class files will be generated for any Java files. A subsequent call to
  # javac will be required to actually compile Java files into .class files.
  #
  # This action also creates a "header" .jar file for the Kotlin source files.
  # It is similar to using turbine to create headers for Java files, but since
  # turbine does not support Kotlin files, this is done via a plugin for
  # kotlinc instead, at the same time as compilation (whereas turbine is run as
  # a separate action before javac compilation).
  template("compile_kt") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _build_config = invoker.build_config
    _chromium_code = invoker.chromium_code

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps = invoker.srcjar_deps
    }

    _java_srcjars = []
    foreach(dep, _srcjar_deps) {
      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
      _dep_name = get_label_info(dep, "name")
      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
    }

    action_with_pydeps(target_name) {
      script = "//build/android/gyp/compile_kt.py"
      depfile = "$target_gen_dir/$target_name.d"
      deps = _srcjar_deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      outputs = [
        invoker.output_jar_path,
        invoker.output_interface_jar_path,
      ]
      inputs = invoker.source_files + _java_srcjars + [
                 _build_config,
                 invoker.target_sources_file,
               ]

      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      _rebased_output_jar_path =
          rebase_path(invoker.output_jar_path, root_build_dir)
      _rebased_output_interface_jar_path =
          rebase_path(invoker.output_interface_jar_path, root_build_dir)
      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
      _rebased_depfile = rebase_path(depfile, root_build_dir)
      _rebased_generated_dir = rebase_path(
              "$target_gen_dir/${invoker.main_target_name}/generated_java",
              root_build_dir)
      args = [
        "--depfile=$_rebased_depfile",
        "--generated-dir=$_rebased_generated_dir",
        "--jar-path=$_rebased_output_jar_path",
        "--interface-jar-path=$_rebased_output_interface_jar_path",
        "--java-srcjars=$_rebased_java_srcjars",
      ]

      # SDK jar must be first on classpath.
      if (invoker.include_android_sdk) {
        args += [ "--classpath=@FileArg($_rebased_build_config:android:sdk_interface_jars)" ]
      }

      args += [ "--classpath=@FileArg($_rebased_build_config:deps_info:javac_full_interface_classpath)" ]

      if (_chromium_code) {
        args += [ "--chromium-code" ]
        if (treat_warnings_as_errors) {
          args += [ "--warnings-as-errors" ]
        }
      }

      args += [ "@" + rebase_path(invoker.target_sources_file, root_build_dir) ]
    }
  }

  # Create an interface jar from a normal jar.
  #
  # Variables
  #   input_jar: Path to input .jar.
  #   output_jar: Path to output .ijar.
  #
  template("generate_interface_jar") {
    action_with_pydeps(target_name) {
      _ijar_target = "//third_party/ijar:ijar($host_toolchain)"
      _ijar_executable = get_label_info(_ijar_target, "root_out_dir") + "/ijar"
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "data",
                                   "data_deps",
                                   "public_deps",
                                 ])
      script = "//build/android/gyp/ijar.py"
      deps = [ _ijar_target ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      inputs = [
        invoker.input_jar,
        _ijar_executable,
      ]
      if (defined(invoker.inputs)) {
        inputs += invoker.inputs
      }
      outputs = [ invoker.output_jar ]
      args = [
        rebase_path(_ijar_executable, root_build_dir),
        rebase_path(invoker.input_jar, root_build_dir),
        rebase_path(invoker.output_jar, root_build_dir),
      ]
    }
  }

  # A rule that will handle multiple Java-related targets.
  #
  # The caller can provide a list of source files with 'java_files'
  # and 'srcjar_deps', or a prebuilt .jar file through 'jar_path'.
  #
  # In the case of a 'java_binary' target type, it can even provide none of
  # that (and the rule will just generate its wrapper script).
  #
  # The template will process the input .jar file (either the prebuilt one,
  # or the result of compiling the sources), for example to apply Proguard,
  # but also other ranges of bytecode-level rewriting schemes.
  #
  # Variables:
  #  type: type of Java target, valid values: 'java_library', 'java_binary',
  #    'robolectric_binary', 'java_annotation_processor', and 'android_apk'
  #  main_target_name: optional. If provided, overrides target_name when
  #    creating sub-targets (e.g. "${main_target_name}__dex") and
  #    some output files (e.g. "${main_target_name}.sources"). Only used
  #    for 'android_apk' types at the moment, where main_target_name will
  #    be the name of the main APK target.
  #  supports_android: Optional. True if target can run on Android.
  #  requires_android: Optional. True if target can only run on Android.
  #  source_files: Optional list of Java source file paths for this target.
  #  javac_args: Optional list of extra arguments to pass to javac.
  #  errorprone_args: Optional list of extra arguments to pass to.
  #  srcjar_deps: Optional list of .srcjar targets (not file paths). The Java
  #    source files they contain will also be compiled for this target.
  #  target_sources_file: Optional path to a file which will be written with
  #    the content of source_files. If not provided, the file will be written
  #    under $target_gen_dir/$main_target_name.sources. Ignored if
  #    sources_files is empty. If not
  #  jar_path: Optional path to a prebuilt .jar file for this target.
  #    Mutually exclusive with java_files and srcjar_deps.
  #  output_name: Optional output name for the final jar path. Used to
  #    determine the name of the final jar. Default is to use the same
  #    name as jar_path, if provided, or main_target_name.
  #  main_class: Main Java class name for 'java_binary', 'robolectric_binary' and
  #    'java_annotation_processor' target types. Should not be set for other
  #    ones.
  #  deps: Dependencies for this target.
  #  public_deps: Dependencies that this target exposes as part of its public API.
  #    public_deps do not need to be listed in both the 'deps' and 'public_deps' lists.
  #  testonly: True iff target should only be used for tests.
  #  chromium_code: Optional. Whether this is Chromium-specific code. If not
  #    provided, this is determined automatically, based on the location of
  #    the source files (i.e. anything under third_party/ is not
  #    Chromium-specific unless it is in a 'chromium' sub-directory).
  #  jacoco_never_instrument: Optional. If provided, whether to forbid
  #    instrumentation with the Jacoco coverage processor. If not provided,
  #    this is controlled by the global use_jacoco_coverage build arg variable
  #    and only used for non-test Chromium code.
  #  include_android_sdk: Optional. Whether or not the android SDK dep
  #    should be added to deps. Defaults to true for non-system libraries
  #    that support android.
  #  alternative_android_sdk_dep: Optional. Alternative Android system
  #    android java target to use.
  #  annotation_processor_deps: Optional list of dependencies corresponding
  #    to annotation processors used to compile these sources.
  #  input_jars_paths: Optional list of additional .jar file paths, which will
  #    be added to the compile-time classpath when building this target (but
  #    not to the runtime classpath).
  #  gradle_treat_as_prebuilt: Cause generate_gradle.py to reference this
  #    library via its built .jar rather than including its .java sources.
  #  proguard_enabled: Optional. True to enable ProGuard obfuscation.
  #  proguard_configs: Optional list of additional proguard config file paths.
  #  is_robolectric: Optional. If True, this is a host side android test binary
  #    which is allowed to depend on other android targets.
  #  include_java_resources: Optional. If True, include Java (not Android)
  #    resources into final .jar file.
  #  jar_excluded_patterns: Optional list of .class file patterns to exclude
  #    from the final .jar file.
  #  jar_included_patterns: Optional list of .class file patterns to include
  #    in the final .jar file. jar_excluded_patterns take precedence over this.
  #  low_classpath_priority: Indicates that the library should be placed at the
  #    end of the classpath. The default classpath order has libraries ordered
  #    before the libraries that they depend on. 'low_classpath_priority' is
  #    useful when one java_library() overrides another via
  #    'jar_excluded_patterns' and the overriding library does not depend on the
  #    overridee.
  #
  # For 'android_apk' and 'android_app_bundle_module' targets only:
  #
  #  apk_path: Path to the final APK file.
  #  android_manifest: Path to AndroidManifest.xml file for the APK.
  #  android_manifest_dep: Optional. Dependency target that generates
  #    android_manifest.
  #  apk_under_test: For 'android_apk' targets used to test other APKs,
  #    this is the target name of APK being tested.
  #  incremental_apk_path: Path to the incremental APK.
  #  incremental_install_json_path: Path to the incremental install json.
  #  native_lib_placeholders: Optional. List of placeholder filenames to add to
  #    the APK.
  #  proguard_mapping_path: Path to .mapping file produced from ProGuard step.
  #  shared_libraries_runtime_deps_file: Optional. Path to a file listing the
  #    native shared libraries required at runtime by the APK.
  #  secondary_abi_shared_libraries_runtime_deps_file:
  #  secondary_native_lib_placeholders: Optional. List of placeholder filenames
  #    to add to the APK for the secondary ABI.
  #  loadable_modules: Optional list of extra native libraries to
  #    be stored in the APK.
  #  secondary_abi_loadable_modules: Optional list of native libraries for
  #    secondary ABI.
  #  proto_resources_path: The path of an zip archive containing the APK's
  #    resources compiled to the protocol buffer format (instead of regular
  #    binary xml + resources.arsc).
  #  r_text_path: The path of the R.txt file generated when compiling the
  #    resources for this target.
  #  module_pathmap_path: The path of the pathmap file generated when compiling
  #    the resources for the bundle module, if path shortening is enabled.
  #  base_allowlist_rtxt_path: The path of the R.txt file containing the
  #    list of string resources to keep in the base split APK for any bundle
  #    that uses this target.
  #
  # For 'java_binary' and 'robolectric_binary' targets only. Ignored by others:
  #
  #  wrapper_script_name: Optional name for the generated wrapper script.
  #    Default is main target name.
  #  wrapper_script_args: Optional list of extra arguments used by the
  #    generated wrapper script.
  #
  template("java_library_impl") {
    # TODO(crbug.com/1042017): Remove.
    not_needed(invoker, [ "no_build_hooks" ])

    forward_variables_from(invoker, [ "testonly" ])
    _is_prebuilt = defined(invoker.jar_path)
    _type = invoker.type
    _is_annotation_processor = _type == "java_annotation_processor"
    _is_java_binary = _type == "java_binary" || _type == "robolectric_binary"
    _is_library = _type == "java_library"
    _supports_android =
        defined(invoker.supports_android) && invoker.supports_android
    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android
    _supports_host = !_requires_android
    if (_is_java_binary || _is_annotation_processor) {
      assert(!_requires_android && !_supports_android)
    }

    _bypass_platform_checks = defined(invoker.bypass_platform_checks) &&
                              invoker.bypass_platform_checks
    _is_robolectric = defined(invoker.is_robolectric) && invoker.is_robolectric

    _invoker_deps = []
    if (defined(invoker.deps)) {
      _invoker_deps += invoker.deps
    }
    if (defined(invoker.public_deps)) {
      _invoker_deps += invoker.public_deps
    }

    _main_target_name = target_name
    if (defined(invoker.main_target_name)) {
      _main_target_name = invoker.main_target_name
    }

    _source_files = []
    if (defined(invoker.sources)) {
      _source_files = invoker.sources
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps = invoker.srcjar_deps
    }
    _has_sources = _source_files != [] || _srcjar_deps != []

    if (_is_prebuilt) {
      assert(!_has_sources)
    } else {
      # Allow java_binary to not specify any sources. This is needed when a prebuilt
      # is needed as a library as well as a binary.
      assert(_is_annotation_processor || _is_java_binary || _has_sources)
    }

    if (_is_java_binary) {
      assert(defined(invoker.main_class), "${_type}() must set main_class")
    } else if (_is_annotation_processor) {
      assert(defined(invoker.main_class),
             "java_annotation_processor() must set main_class")
    } else {
      assert(!defined(invoker.main_class),
             "main_class cannot be used for target of type ${_type}")
    }

    if (defined(invoker.chromium_code)) {
      _chromium_code = invoker.chromium_code
    } else {
      # Default based on whether target is in third_party.
      _chromium_code =
          filter_exclude([ get_label_info(":$_main_target_name", "dir") ],
                         [ "*\bthird_party\b*" ]) != []
      if (!_chromium_code && !_is_prebuilt && _source_files != []) {
        # Unless third_party code has an org.chromium file in it.
        _chromium_code =
            filter_exclude(_source_files, [ "*\bchromium\b*" ]) != _source_files
      }
    }

    # Define build_config_deps which will be a list of targets required to
    # build the _build_config.
    _build_config = "$target_gen_dir/$_main_target_name.build_config.json"
    _build_config_target_name =
        "${_main_target_name}$build_config_target_suffix"

    # The only target that might have no prebuilt and no sources is a java_binary.
    _build_host_jar = false
    _build_device_jar = false
    if (_is_prebuilt || _has_sources) {
      if (defined(invoker.output_name)) {
        _output_name = invoker.output_name
      } else {
        _output_name = _main_target_name
      }

      _build_host_jar =
          _is_java_binary || _is_annotation_processor || _type == "java_library"
      _build_device_jar = _type != "system_java_library" && _supports_android

      _jacoco_instrument =
          use_jacoco_coverage && _chromium_code && _source_files != [] &&
          _build_device_jar && (!defined(invoker.testonly) || !invoker.testonly)
      if (defined(invoker.jacoco_never_instrument)) {
        _jacoco_instrument =
            !invoker.jacoco_never_instrument && _jacoco_instrument
      }
      if (_jacoco_instrument) {
        _invoker_deps += [ _jacoco_dep ]
      }

      if (_build_host_jar) {
        # Jar files can be needed at runtime (by Robolectric tests or java binaries),
        # so do not put them under obj/.
        # TODO(agrieve): I suspect it would be better to use dist_jar for java_binary
        #     rather than archiving unnecessary .jar files within lib.java.
        _target_dir_name = get_label_info(":$_main_target_name", "dir")
        _host_processed_jar_path =
            "$root_out_dir/lib.java$_target_dir_name/$_output_name.jar"
      }
      if (_build_device_jar) {
        _dex_path = "$target_out_dir/$_main_target_name.dex.jar"
        _enable_desugar =
            !defined(invoker.enable_desugar) || invoker.enable_desugar

        # Build speed optimization: Skip "process device" step if the step
        # would be just a copy and avoid the copy.
        _process_device_jar =
            defined(invoker.bytecode_rewriter_target) || _jacoco_instrument ||
            defined(invoker.jar_excluded_patterns) ||
            defined(invoker.jar_included_patterns)
        if (!_process_device_jar && _is_prebuilt) {
          _device_processed_jar_path = invoker.jar_path
        } else {
          _device_processed_jar_path =
              "$target_out_dir/$_output_name.processed.jar"
        }
      }

      # For static libraries, the javac jar output is created at the intermediate
      # path so that it can be processed by another target and moved to the final
      # spot that the .build_config.json knows about. Technically this should be done
      # for the ijar as well, but this is only used for APK targets where
      # the ijar path isn't actually used.
      if (_has_sources) {
        _final_ijar_path = "$target_out_dir/$_output_name.turbine.jar"
      } else {
        _final_ijar_path = "$target_out_dir/$_output_name.ijar.jar"
      }

      if (_has_sources) {
        if (_build_device_jar && !_process_device_jar) {
          _javac_jar_path = _device_processed_jar_path
        } else {
          _javac_jar_path = "$target_out_dir/$_main_target_name.javac.jar"
        }
        _generated_jar_path =
            "$target_gen_dir/$_main_target_name.generated.srcjar"
      }

      if (_is_prebuilt) {
        _unprocessed_jar_path = invoker.jar_path
      } else {
        _unprocessed_jar_path = _javac_jar_path
      }
    }

    _java_assetres_deps = filter_include(_invoker_deps, java_resource_patterns)

    # Cannot use minus operator because it does not work when the operand has
    # repeated entries.
    _invoker_deps_minus_assetres =
        filter_exclude(_invoker_deps, _java_assetres_deps)
    _lib_deps =
        filter_include(_invoker_deps_minus_assetres, java_library_patterns)
    _non_java_deps = filter_exclude(_invoker_deps_minus_assetres, _lib_deps)

    _java_header_deps = []  # Turbine / ijar

    # It would be more ideal to split this into __host and __javac, but we
    # combine the two concepts to save on a group() target.
    _java_host_deps = []  # Processed host .jar + javac .jar.
    _java_validate_deps = []  # Bytecode checker & errorprone.

    foreach(_lib_dep, _lib_deps) {
      # Expand //foo/java -> //foo/java:java
      _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
      _java_assetres_deps += [ "${_lib_dep}__assetres" ]
      _java_header_deps += [ "${_lib_dep}__header" ]
      _java_host_deps += [ "${_lib_dep}__host" ]
      _java_validate_deps += [ "${_lib_dep}__validate" ]
    }

    # APK and base module targets are special because:
    # 1) They do not follow java target naming scheme (since they are not
    #    generally deps, there is no need for them to).
    # 2) They do not bother to define a __host target.
    # Since __host is used as an indirect dep for the compile_java artifacts,
    # add the __compile_java target directly for them.
    if (defined(invoker.apk_under_test)) {
      _java_assetres_deps += [ "${invoker.apk_under_test}__java__assetres" ]
      _java_header_deps += [ "${invoker.apk_under_test}__java__header" ]
      _java_validate_deps += [ "${invoker.apk_under_test}__java__validate" ]
      _java_host_deps += [ "${invoker.apk_under_test}__compile_java" ]
    }
    if (defined(invoker.base_module_target)) {
      _java_assetres_deps += [ "${invoker.base_module_target}__java__assetres" ]
      _java_header_deps += [ "${invoker.base_module_target}__java__header" ]
      _java_validate_deps += [ "${invoker.base_module_target}__java__validate" ]
      _java_host_deps += [ "${invoker.base_module_target}__compile_java" ]
    }

    not_needed([ "_non_java_deps" ])

    if (_is_prebuilt || _has_sources) {
      # Classpath deps are used for header and dex targets, they do not need
      # __assetres deps.
      # _non_java_deps are needed for input_jars_paths that are generated.
      _header_classpath_deps =
          _java_header_deps + _non_java_deps + [ ":$_build_config_target_name" ]

      _javac_classpath_deps =
          _java_host_deps + _non_java_deps + [ ":$_build_config_target_name" ]

      _include_android_sdk = _build_device_jar
      if (defined(invoker.include_android_sdk)) {
        _include_android_sdk = invoker.include_android_sdk
      }
      if (_include_android_sdk) {
        if (defined(invoker.alternative_android_sdk_dep)) {
          _android_sdk_dep = invoker.alternative_android_sdk_dep
        } else {
          _android_sdk_dep = default_android_sdk_dep
        }

        _header_classpath_deps += [ "${_android_sdk_dep}__header" ]
        _javac_classpath_deps += [ "${_android_sdk_dep}" ]
      }
    }

    # Often needed, but too hard to figure out when ahead of time.
    not_needed([
                 "_header_classpath_deps",
                 "_javac_classpath_deps",
               ])

    if (_source_files != []) {
      _target_sources_file = "$target_gen_dir/$_main_target_name.sources"
      write_file(_target_sources_file,
                 rebase_path(_source_files, root_build_dir))
    }

    write_build_config(_build_config_target_name) {
      forward_variables_from(invoker,
                             [
                               "aar_path",
                               "annotation_processor_deps",
                               "base_allowlist_rtxt_path",
                               "gradle_treat_as_prebuilt",
                               "input_jars_paths",
                               "preferred_dep",
                               "low_classpath_priority",
                               "main_class",
                               "mergeable_android_manifests",
                               "module_name",
                               "parent_module_target",
                               "proguard_configs",
                               "proguard_enabled",
                               "proguard_mapping_path",
                               "public_target_label",
                               "r_text_path",
                               "type",
                               "version_code",
                               "version_name",
                             ])
      if (_type == "android_apk" || _type == "android_app_bundle_module") {
        forward_variables_from(
            invoker,
            [
              "android_manifest",
              "android_manifest_dep",
              "merged_android_manifest",
              "final_dex_path",
              "loadable_modules",
              "native_lib_placeholders",
              "res_size_info_path",
              "secondary_abi_loadable_modules",
              "secondary_abi_shared_libraries_runtime_deps_file",
              "secondary_native_lib_placeholders",
              "shared_libraries_runtime_deps_file",
              "library_always_compress",
            ])
      }
      if (_type == "android_apk") {
        forward_variables_from(invoker,
                               [
                                 "apk_path",
                                 "apk_under_test",
                                 "incremental_apk_path",
                                 "incremental_install_json_path",
                               ])
      }
      if (_type == "android_app_bundle_module") {
        forward_variables_from(invoker,
                               [
                                 "add_view_trace_events",
                                 "base_module_target",
                                 "module_pathmap_path",
                                 "proto_resources_path",
                               ])
      }
      chromium_code = _chromium_code
      build_config = _build_config
      is_prebuilt = _is_prebuilt

      # Specifically avoid passing in invoker.base_module_target as one of the
      # possible_config_deps.
      possible_config_deps = []
      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }
      if (defined(invoker.public_deps)) {
        possible_config_public_deps = invoker.public_deps
      }
      if (defined(invoker.asset_deps)) {
        possible_config_deps += invoker.asset_deps
      }
      if (defined(apk_under_test)) {
        possible_config_deps += [ apk_under_test ]
      }
      if (defined(_jacoco_instrument) && _jacoco_instrument) {
        possible_config_deps += [ _jacoco_dep ]
      }
      if (defined(_android_sdk_dep)) {
        possible_config_deps += [ _android_sdk_dep ]
      }

      supports_android = _supports_android
      requires_android = _requires_android
      is_robolectric = _is_robolectric
      bypass_platform_checks = _bypass_platform_checks

      if (defined(invoker.resources_package)) {
        custom_package = invoker.resources_package
      }
      if (_is_prebuilt || _has_sources) {
        ijar_path = _final_ijar_path
        unprocessed_jar_path = _unprocessed_jar_path
      }
      if (_build_host_jar) {
        host_jar_path = _host_processed_jar_path
      }
      if (_build_device_jar) {
        device_jar_path = _device_processed_jar_path
        dex_path = _dex_path
      }
      if (_source_files != []) {
        target_sources_file = _target_sources_file
      }

      bundled_srcjars = []
      foreach(d, _srcjar_deps) {
        _dep_gen_dir = get_label_info(d, "target_gen_dir")
        _dep_name = get_label_info(d, "name")
        bundled_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
      }
      if (defined(invoker.include_java_resources) &&
          invoker.include_java_resources) {
        java_resources_jar = _unprocessed_jar_path
        if (defined(invoker.jar_path)) {
          # Use original jar_path because _jar_path points to a library without
          # resources.
        } else {
          java_resources_jar = _device_processed_jar_path
        }
      }
    }

    if (_is_prebuilt || _has_sources) {
      _header_target_name = "${target_name}__header"
    }

    if (_has_sources) {
      _kt_files = filter_include(_source_files, [ "*.kt" ])
      _java_files = filter_exclude(_source_files, [ "*.kt" ])

      if (defined(invoker.enable_errorprone)) {
        _enable_errorprone = invoker.enable_errorprone
      } else {
        _enable_errorprone =
            _java_files != [] && _chromium_code && use_errorprone_java_compiler
      }

      if (defined(invoker.resources_package) && _type == "java_library") {
        # TODO(crbug.com/1296632): remove _bypass_platform_checks from the list
        # once all robolectric targets have migrated to robolectric_library.
        assert(_requires_android || _bypass_platform_checks || _is_robolectric,
               "Setting resources_package applicable only for " +
                   "android_library(), or robolectric_library(). " +
                   "Target=$target_name")

        # Serves double purpose: Generating R.java, as well as being the
        #__assetres target (instead of using a separate group).
        _fake_rjava_target = "${target_name}__assetres"
        generate_r_java(_fake_rjava_target) {
          deps = [ ":$_build_config_target_name" ] + _java_assetres_deps +
                 _non_java_deps
          build_config = _build_config

          # Filepath has to be exactly this because compile_java looks for the
          # srcjar of srcjar_deps at this location $gen_dir/$target_name.srcjar
          srcjar_path = "$target_gen_dir/$target_name.srcjar"
          package = invoker.resources_package
        }
        _srcjar_deps += [ ":$_fake_rjava_target" ]
      }

      if (_kt_files != []) {
        _kt_allowlist = [
          "android/java/src/org/chromium/chrome/browser/tabmodel/AsyncTabParamsManagerImpl.kt",
          "java/androidx/core/os/BuildCompat.kt",
          "webengine_shell_apk/src/org/chromium/webengine/shell/*.kt",
        ]
        _found_kt = filter_exclude(_kt_files, _kt_allowlist)
        assert(
            _found_kt == [],
            "Only a files in the allowlist can be included for now. Feel " + "free to remove this assert when experimenting locally. Found: $_found_kt")
        _compile_kt_target_name = "${_main_target_name}__compile_kt"
        _kotlinc_jar_path = "$target_out_dir/$_output_name.kotlinc.jar"
        _kotlin_interface_jar_path =
            "$target_out_dir/$_output_name.kt-jvm-abi.jar"
        assert(filter_include(_lib_deps, [ _kotlin_stdlib_dep ]) != [],
               "${_main_target_name} is missing dep: $_kotlin_stdlib_dep")
        compile_kt(_compile_kt_target_name) {
          deps = _header_classpath_deps
          output_jar_path = _kotlinc_jar_path
          output_interface_jar_path = _kotlin_interface_jar_path
          main_target_name = _main_target_name
          build_config = _build_config
          srcjar_deps = _srcjar_deps
          source_files = _source_files
          target_sources_file = _target_sources_file
          chromium_code = _chromium_code
          include_android_sdk = _is_robolectric || _requires_android
        }
      }

      template("compile_java_helper") {
        _enable_errorprone =
            defined(invoker.enable_errorprone) && invoker.enable_errorprone
        if (_enable_errorprone) {
          # Rely on the header jar to provide all .class files so that it is
          # safe to omit generated files entirely for errorprone.
          _filtered_source_files =
              filter_exclude(_source_files, [ "$root_gen_dir*" ])
        }
        if (_enable_errorprone && _filtered_source_files == []) {
          # Filtering out generated files resulted in no files left.
          group(target_name) {
            not_needed(invoker, "*")
            deps = _header_classpath_deps
          }
        } else {
          compile_java(target_name) {
            forward_variables_from(invoker,
                                   "*",
                                   TESTONLY_AND_VISIBILITY + [ "deps" ])
            deps = _header_classpath_deps
            if (defined(invoker.deps)) {
              deps += invoker.deps
            }
            output_jar_path = invoker.output_jar_path
            if (defined(invoker.kotlin_jar_path)) {
              deps += [ ":$_compile_kt_target_name" ]
              kotlin_jar_path = invoker.kotlin_jar_path
            }
            enable_errorprone = _enable_errorprone
            use_turbine = defined(invoker.use_turbine) && invoker.use_turbine

            main_target_name = _main_target_name
            build_config = _build_config

            if (_enable_errorprone) {
              source_files = _filtered_source_files
            } else {
              source_files = _source_files
              srcjar_deps = _srcjar_deps
            }

            if (source_files != []) {
              target_sources_file = _target_sources_file
            }
            chromium_code = _chromium_code
            include_android_sdk = _is_robolectric || _requires_android
          }
        }
      }
      _compile_java_forward_variables = [
        "additional_jar_files",
        "apk_name",
        "jar_excluded_patterns",
        "javac_args",
        "processor_args_javac",
        "skip_build_server",
      ]
      _annotation_processor_deps = []
      if (defined(invoker.annotation_processor_deps)) {
        _annotation_processor_deps = invoker.annotation_processor_deps
      }

      compile_java_helper(_header_target_name) {
        forward_variables_from(invoker, _compile_java_forward_variables)
        use_turbine = true
        output_jar_path = _final_ijar_path
        generated_jar_path = _generated_jar_path
        deps = _annotation_processor_deps
        if (_kt_files != []) {
          kotlin_jar_path = _kotlin_interface_jar_path
        }
      }

      _compile_java_target = "${_main_target_name}__compile_java"
      compile_java_helper(_compile_java_target) {
        forward_variables_from(invoker, _compile_java_forward_variables)
        output_jar_path = _javac_jar_path
        deps = [ ":$_header_target_name" ]
        header_jar_path = _final_ijar_path
        generated_jar_path = _generated_jar_path
        if (_kt_files != []) {
          kotlin_jar_path = _kotlinc_jar_path
        }
      }
      if (_enable_errorprone) {
        _compile_java_errorprone_target = "${_main_target_name}__errorprone"
        compile_java_helper(_compile_java_errorprone_target) {
          forward_variables_from(invoker, _compile_java_forward_variables)
          enable_errorprone = true
          if (defined(invoker.errorprone_args)) {
            if (!defined(javac_args)) {
              javac_args = []
            }
            javac_args += invoker.errorprone_args
          }
          deps = [ ":$_header_target_name" ]
          if (_kt_files != []) {
            kotlin_jar_path = _kotlinc_jar_path
          }
          header_jar_path = _final_ijar_path
          generated_jar_path = _generated_jar_path
          output_jar_path = "$target_out_dir/$target_name.errorprone.stamp"
        }
        _java_validate_deps += [ ":$_compile_java_errorprone_target" ]
      }
    }  # _has_sources

    if (_is_prebuilt || _build_device_jar || _build_host_jar) {
      if (_has_sources) {
        _unprocessed_jar_deps = [ ":$_compile_java_target" ]
      } else {
        # jars might be generated by a dep.
        _unprocessed_jar_deps = _non_java_deps
      }
    }

    if (defined(invoker.bytecode_rewriter_target)) {
      assert(_build_host_jar || _build_device_jar,
             "A host or device jar must be created to use bytecode rewriting")

      _rewritten_jar = "$target_out_dir/${target_name}_rewritten.jar"
      _rewritten_jar_target_name = "${target_name}__rewritten"
      _rewriter_path = root_build_dir + "/bin/helper/" +
                       get_label_info(invoker.bytecode_rewriter_target, "name")
      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      action_with_pydeps(_rewritten_jar_target_name) {
        script = "//build/android/gyp/bytecode_rewriter.py"
        inputs = java_paths_for_inputs + [
                   _rewriter_path,
                   _build_config,
                   _unprocessed_jar_path,
                 ]
        outputs = [ _rewritten_jar ]
        depfile = "$target_gen_dir/$target_name.d"
        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--script",
          rebase_path(_rewriter_path, root_build_dir),
          "--classpath",
          "@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
          "--classpath",
          "@FileArg($_rebased_build_config:android:sdk_jars)",
          "--input-jar",
          rebase_path(_unprocessed_jar_path, root_build_dir),
          "--output-jar",
          rebase_path(_rewritten_jar, root_build_dir),
        ]
        deps = _unprocessed_jar_deps + _javac_classpath_deps +
               [ invoker.bytecode_rewriter_target ]
      }

      _unprocessed_jar_deps = []
      _unprocessed_jar_deps = [ ":$_rewritten_jar_target_name" ]
      _unprocessed_jar_path = _rewritten_jar
    }

    if (_is_prebuilt) {
      generate_interface_jar(_header_target_name) {
        # Always used the unfiltered .jar to create the interface jar so that
        # other targets will resolve filtered classes when depending on
        # BuildConfig, NativeLibraries, etc.
        input_jar = _unprocessed_jar_path
        output_jar = _final_ijar_path

        # ijar needs only _unprocessed_jar_deps, but this also needs to export
        # __header target from deps.
        deps = _unprocessed_jar_deps + _java_header_deps
      }
    }

    if (_build_host_jar || _build_device_jar) {
      _enable_bytecode_checks =
          (!defined(invoker.enable_bytecode_checks) ||
           invoker.enable_bytecode_checks) && !_is_prebuilt &&
          android_static_analysis != "off"
      if (_enable_bytecode_checks) {
        _validate_target_name = "${target_name}__validate"
        bytecode_processor(_validate_target_name) {
          forward_variables_from(invoker, [ "missing_classes_allowlist" ])
          deps = _unprocessed_jar_deps + _javac_classpath_deps +
                 [ ":$_build_config_target_name" ]
          data_deps = _java_validate_deps
          if (defined(_compile_java_errorprone_target)) {
            data_deps += [ ":$_compile_java_errorprone_target" ]
          }

          include_android_sdk = _requires_android || _is_robolectric
          target_label =
              get_label_info(":${invoker.target_name}", "label_no_toolchain")
          input_jar = _unprocessed_jar_path
          build_config = _build_config
        }
      } else {
        not_needed(invoker, [ "missing_classes_allowlist" ])
      }

      if (_build_host_jar) {
        _process_host_jar_target_name = "${target_name}__host"
        process_java_library(_process_host_jar_target_name) {
          forward_variables_from(invoker,
                                 [
                                   "jar_excluded_patterns",
                                   "jar_included_patterns",
                                 ])

          # Robolectric tests require these to be on swarming.
          data = [ _host_processed_jar_path ]
          input_jar_path = _unprocessed_jar_path
          deps = _unprocessed_jar_deps + _javac_classpath_deps
          output_jar_path = _host_processed_jar_path
          jacoco_instrument = _jacoco_instrument
          if (_jacoco_instrument) {
            source_files = _source_files
            target_sources_file = _target_sources_file
          }

          # _java_host_deps isn't necessary for process_java_library(), but is
          # necessary so that this target can be used to depend on transitive
          # __device targets without the need to create a separate group()
          # target. This trade-off works because process_java_library is fast.
          deps += _java_host_deps

          # Add runtime_deps here since robolectric_binary does not depend on top-level group.
          if (defined(invoker.data)) {
            data += invoker.data
          }
          if (defined(invoker.data_deps)) {
            data_deps = invoker.data_deps
          }
        }
      }

      if (_build_device_jar) {
        if (_process_device_jar) {
          _process_device_jar_target_name = "${target_name}__process_device"
          process_java_library(_process_device_jar_target_name) {
            forward_variables_from(invoker,
                                   [
                                     "jar_excluded_patterns",
                                     "jar_included_patterns",
                                   ])
            input_jar_path = _unprocessed_jar_path

            deps = _unprocessed_jar_deps + _javac_classpath_deps
            output_jar_path = _device_processed_jar_path
            jacoco_instrument = _jacoco_instrument
            if (_jacoco_instrument) {
              source_files = _source_files
              target_sources_file = _target_sources_file
            }
          }
          _process_device_jar_deps = [ ":${_process_device_jar_target_name}" ]
        } else {
          assert(_unprocessed_jar_path == _device_processed_jar_path)
          _process_device_jar_deps = _unprocessed_jar_deps
        }

        _dex_target_name = "${target_name}__dex"
        dex(_dex_target_name) {
          forward_variables_from(invoker,
                                 [
                                   "proguard_enable_obfuscation",
                                   "repackage_classes",
                                 ])
          input_class_jars = [ _device_processed_jar_path ]
          enable_desugar = _enable_desugar
          ignore_desugar_missing_deps = !_enable_bytecode_checks

          # There's no value in per-class dexing prebuilts since they never
          # change just one class at a time.
          disable_incremental = _is_prebuilt
          output = _dex_path
          deps = _process_device_jar_deps

          if (enable_desugar) {
            # Desugaring with D8 requires full classpath.
            build_config = _build_config
            unprocessed_jar_path = _unprocessed_jar_path
            deps += _header_classpath_deps + _unprocessed_jar_deps
          }

          is_library = true

          # proguard_configs listed on java_library targets need to be marked
          # as inputs to at least one target so that "gn analyze" will know
          # about them. Although this target doesn't use them, it's a convenient spot
          # to list them.
          # https://crbug.com/827197
          if (compute_inputs_for_analyze && defined(invoker.proguard_configs)) {
            inputs = invoker.proguard_configs

            # For the aapt-generated proguard rules.
            deps += _non_java_deps + _srcjar_deps
          }
        }
      }
    }

    if (_is_java_binary) {
      # Targets might use the generated script while building, so make it a dep
      # rather than a data_dep.
      _java_binary_script_target_name = "${target_name}__java_binary_script"
      java_binary_script(_java_binary_script_target_name) {
        forward_variables_from(invoker,
                               [
                                 "tiered_stop_at_level_one",
                                 "main_class",
                                 "max_heap_size",
                                 "wrapper_script_args",
                               ])
        build_config = _build_config
        script_name = _main_target_name
        if (defined(invoker.wrapper_script_name)) {
          script_name = invoker.wrapper_script_name
        }
        deps = [ ":$_build_config_target_name" ]
        if (_is_robolectric) {
          # For robolectric tests, we add the sdk stub jars so that classes
          # that reference Android types can be loaded without throwing
          # NoClassDefFoundErrors. The Robolectric sandbox makes these types
          # available in non-stub form, but not until test classes are loaded
          # into it. Before being loaded into the sandbox, they must be loaded
          # outside of it in order to read their annotations (which configure
          # the sandbox), and to enumerate test methods.
          extra_classpath_jars = [
            android_sdk_jar,
            "$android_sdk/optional/android.test.base.jar",
            "$android_sdk/optional/android.test.runner.jar",
          ]
        }
      }
    }

    if (!defined(_validate_target_name)) {
      _validate_target_name = "${target_name}__validate"

      # Allow other targets to depend on this __validate one.
      group(_validate_target_name) {
        deps = _java_validate_deps
      }
    }

    if (_supports_host && !defined(_process_host_jar_target_name)) {
      group("${target_name}__host") {
        deps = _java_host_deps
      }
    }

    # robolectric_library can depend on java_library, so java_library must
    # define __assetres.
    if ((_is_library || _supports_android || _is_robolectric) &&
        !defined(_fake_rjava_target)) {
      group("${target_name}__assetres") {
        if (_supports_android || _is_robolectric) {
          deps = _java_assetres_deps
        }
      }
    }

    # The top-level group is used:
    # 1) To allow building the target explicitly via ninja,
    # 2) To trigger all analysis deps,
    # 3) By custom action() targets that want to use artifacts as inputs.
    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "assert_no_deps",
                               "data",
                               "data_deps",
                               "visibility",
                             ])
      if (_requires_android || (_supports_android && _is_library)) {
        # For non-robolectric targets, depend on other java target's top-level
        # groups so that the __dex step gets depended on.
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "public_deps",
                               ])
        if (!defined(deps)) {
          deps = []
        }
        if (is_cronet_build) {
          _abs_deps = []
          if (defined(invoker.deps)) {
            foreach(dep, invoker.deps) {
              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
            }
          }
          if (defined(invoker.public_deps)) {
            foreach(dep, invoker.public_deps) {
              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
            }
          }
          if (defined(invoker.srcjar_deps)) {
            foreach(dep, invoker.srcjar_deps) {
              _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
            }
          }
          _abs_path_source_files = []
          if (defined(invoker.sources)) {
            foreach(source_file, invoker.sources) {
              _abs_path_source_files +=
                  [ get_path_info(source_file, "abspath") ]
            }
          }
          _abs_jar_path = ""
          if (defined(invoker.jar_path)) {
            _abs_jar_path = get_path_info(invoker.jar_path, "abspath")
          }
          _sdk_version = "current"
          if (defined(invoker.alternative_android_sdk_dep)) {
            _sdk_version = "system_current"
          }

          # See crbug/1449896 for more details about the metadata fields
          # and why they are added.
          metadata = {
            jar_path = [ _abs_jar_path ]
            source_files = _abs_path_source_files
            all_deps = _abs_deps
            target_type = [ _type ]
            sdk_version = [ _sdk_version ]
          }
        }
        if (!defined(public_deps)) {
          public_deps = []
        }
      } else {
        # For robolectric targets, depend only on non-java deps and the specific
        # subtargets below, which will not include __dex.
        deps = _non_java_deps
        public_deps = []
        if (defined(invoker.public_deps)) {
          public_deps +=
              filter_exclude(invoker.public_deps, java_target_patterns)
        }
      }
      if (defined(_jacoco_instrument) && _jacoco_instrument) {
        deps += [ _jacoco_dep ]
      }
      if (defined(invoker.apk_under_test)) {
        deps += [ invoker.apk_under_test ]
      }
      if (defined(_process_device_jar_target_name)) {
        public_deps += [ ":$_process_device_jar_target_name" ]
      }
      if (defined(_dex_target_name)) {
        public_deps += [ ":$_dex_target_name" ]
      }
      if (_supports_android && _is_library) {
        # Robolectric targets define __assetres, but there's no need to build it
        # by default.
        public_deps += [ ":${target_name}__assetres" ]
      }
      if (_supports_host) {
        # android_* targets define __host, but there's no need to build it by
        # default.
        public_deps += [ ":${target_name}__host" ]
      }
      if (_is_java_binary) {
        public_deps += [ ":$_java_binary_script_target_name" ]
      }
      if (!defined(data_deps)) {
        data_deps = []
      }
      if (defined(_validate_target_name)) {
        data_deps += [ ":$_validate_target_name" ]
      } else {
        data_deps += _java_validate_deps
      }
    }
  }
}

# Create a zip archive corresponding to an application bundle module.
#
# Compile all the components of a given android_apk_or_module() target into a
# zip archive suitable to later create an android_app_bundle() target. This
# archive's format is very similar to that on an APK, except for a few
# differences in internal directory layouts, and the fact that resources, as
# well as xml files, are compiled using a protocol-buffer based format (instead
# of the regular binary xml + resources.arsc).
#
# A final application bundle is built from one or more module bundle modules,
# plus some configuration file.
#
# Variables:
#   module_zip_path: Output module path.
#   build_config: Path to build_config of the android_apk_or_module() target.
#   dex_path: If module is proguarded separately from the base module, dex_path
#     is the path to its dex file and is passed directly to the creation script.
#     Otherwise, dex_path is undefined and we retrieve the module's dex file
#     using its build_config.
#   expected_libs_and_assets: Verify the list of included native libraries
#     and assets is consistent with the given expectation file.
#   expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
#     with this file as the base.
#   is_multi_abi: If true will add a library placeholder for the missing ABI if
#     either the primary or the secondary ABI has no native libraries set.
#   module_name: The module's name.
#   native_libraries_config: Path to file listing native libraries to be
#     packaged into each module.
#   proguard_enabled: Optional. True if proguarding is enabled for this
#     bundle. Default is to enable this only for release builds. Note that
#     this will always perform synchronized proguarding.
template("create_android_app_bundle_module") {
  _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
  _rebased_native_libraries_config =
      rebase_path(invoker.native_libraries_config, root_build_dir)

  forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
  _deps = invoker.deps
  _script = "//build/android/gyp/apkbuilder.py"

  # NOTE: Compared to the inputs of the "package_apk" template action,
  #       this list is much smaller, since finalize_apk is never called
  #       by apkbuild.py --format=bundle-module. This means not using
  #       apksigner and zipalign as well, nor the keystore. Other
  #       dependencies like extra native libraries are all pulled from the
  #       .build_config.json through @FileArg() references (see below) and
  #       will be listed in the generated depfile instead.
  _inputs = [
    invoker.build_config,
    invoker.native_libraries_config,
  ]
  _outputs = [ invoker.module_zip_path ]
  _args = [
    "--format=bundle-module",
    "--output-apk",
    rebase_path(invoker.module_zip_path, root_build_dir),
    "--resource-apk=@FileArg(" +
        "$_rebased_build_config:deps_info:proto_resources_path)",
    "--assets=@FileArg($_rebased_build_config:assets)",
    "--uncompressed-assets=@FileArg(" +
        "$_rebased_build_config:uncompressed_assets)",
    "--native-libs=@FileArg($_rebased_native_libraries_config" +
        ":${invoker.module_name})",
    "--native-lib-placeholders=@FileArg($_rebased_build_config" +
        ":native:native_library_placeholders)",
    "--secondary-native-lib-placeholders=@FileArg($_rebased_build_config" +
        ":native:secondary_native_library_placeholders)",
    "--android-abi=$android_app_abi",
    "--min-sdk-version=${invoker.min_sdk_version}",
    "--library-always-compress=@FileArg($_rebased_build_config:native:library_always_compress)",
  ]
  if (defined(android_app_secondary_abi)) {
    _rebased_secondary_abi_native_libraries_config =
        rebase_path(invoker.secondary_abi_native_libraries_config,
                    root_build_dir)
    _args += [
      "--secondary-native-libs",
      "@FileArg($_rebased_secondary_abi_native_libraries_config" +
          ":${invoker.module_name})",
      "--secondary-android-abi=$android_app_secondary_abi",
    ]
  }
  if (defined(invoker.is_multi_abi) && invoker.is_multi_abi) {
    _args += [ "--is-multi-abi" ]
  }
  if (defined(invoker.uncompress_dex) && invoker.uncompress_dex) {
    _args += [ "--uncompress-dex" ]
  }
  if (defined(invoker.extra_assets)) {
    _args += [ "--assets=${invoker.extra_assets}" ]
  }

  # Use either provided dex path or build config path based on type of module.
  if (defined(invoker.dex_path)) {
    _inputs += [ invoker.dex_path ]
    _rebased_dex_path = rebase_path(invoker.dex_path, root_build_dir)
    _args += [ "--dex-file=$_rebased_dex_path" ]
  } else {
    _args += [ "--dex-file=@FileArg($_rebased_build_config:final_dex:path)" ]
  }

  if (treat_warnings_as_errors) {
    _args += [ "--warnings-as-errors" ]
  }

  if (defined(invoker.expected_libs_and_assets)) {
    _expectations_target = "${invoker.top_target_name}_validate_libs_and_assets"
    action_with_pydeps(_expectations_target) {
      _actual_file = "$target_gen_dir/$target_name.libs_and_assets"
      _failure_file = "$expectations_failure_dir/" +
                      string_replace(invoker.expected_libs_and_assets, "/", "_")
      inputs = [
        invoker.expected_libs_and_assets,
        invoker.build_config,
        invoker.native_libraries_config,
      ]
      deps = [
        invoker.build_config_target,
        invoker.native_libraries_config_target,
      ]
      if (defined(android_app_secondary_abi)) {
        inputs += [ invoker.secondary_abi_native_libraries_config ]
        deps += [ invoker.secondary_abi_native_libraries_config_target ]
      }
      outputs = [
        _actual_file,
        _failure_file,
      ]
      script = _script
      args = _args + [
               "--expected-file",
               rebase_path(invoker.expected_libs_and_assets, root_build_dir),
               "--actual-file",
               rebase_path(_actual_file, root_build_dir),
               "--failure-file",
               rebase_path(_failure_file, root_build_dir),
               "--only-verify-expectations",
             ]
      if (defined(invoker.expected_libs_and_assets_base)) {
        inputs += [ invoker.expected_libs_and_assets_base ]
        args += [
          "--expected-file-base",
          rebase_path(invoker.expected_libs_and_assets_base, root_build_dir),
        ]
      }
      if (fail_on_android_expectations) {
        args += [ "--fail-on-expectations" ]
      }
    }
    _deps += [ ":$_expectations_target" ]
  }

  action_with_pydeps(target_name) {
    deps = _deps
    inputs = _inputs
    outputs = _outputs
    script = _script
    depfile = "$target_gen_dir/$target_name.d"
    args = _args + [
             "--depfile",
             rebase_path(depfile, root_build_dir),
           ]
  }
}

# Allots native libraries depended on by feature modules to the module the
# libraries should be packaged into. The packaging module may be different from
# the dependee module in case a library is depended on by multiple modules. In
# that case the library will be allotted to the closest ancestor given a module
# dependency tree (see |parent| below).
#
# Variables:
#   modules: List of scopes with the following format:
#     name: The module's name.
#     parent: The module's parent's name.
#     build_config: Path to the module's build config.
#     build_config_target: Target creating |build_config|.
#   native_libraries_filearg_keys: Keys to be used in
#     @FileArg(|build_config|:<keys>) expressions pointing to a list of native
#     libraries to consider in |build_config|.
#   output: Path to native libraries config.
template("allot_native_libraries") {
  action_with_pydeps(target_name) {
    script = "//build/android/gyp/allot_native_libraries.py"
    args = [
      "--output",
      rebase_path(invoker.output, root_build_dir),
    ]
    outputs = [ invoker.output ]
    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    inputs = []
    foreach(_module, invoker.modules) {
      deps += [ _module.build_config_target ]
      inputs += [ _module.build_config ]
      _rebased_build_config = rebase_path(_module.build_config, root_out_dir)
      foreach(_key, invoker.native_libraries_filearg_keys) {
        args += [
          "--libraries",
          "${_module.name},@FileArg($_rebased_build_config:$_key)",
        ]
      }
      if (defined(_module.parent)) {
        args += [
          "--dep",
          "${_module.parent}:${_module.name}",
        ]
      }
    }
  }
}