summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/build/config/android/rules.gni
blob: 45499a37818a454d4748a7b7900bea9d0999956d (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
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
# Copyright 2014 The Chromium Authors. All rights reserved.
# 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/android/internal_rules.gni")
import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/coverage/coverage.gni")
import("//build/config/python.gni")
import("//build/config/rts.gni")
import("//build/config/zip.gni")
import("//build/toolchain/toolchain.gni")

assert(is_android)

declare_args() {
  enable_jni_tracing = false
}

if (target_cpu == "arm") {
  _sanitizer_arch = "arm"
} else if (target_cpu == "arm64") {
  _sanitizer_arch = "aarch64"
} else if (target_cpu == "x86") {
  _sanitizer_arch = "i686"
}

_sanitizer_runtimes = []
if (use_cfi_diag || is_ubsan || is_ubsan_security || is_ubsan_vptr) {
  _sanitizer_runtimes = [ "$clang_base_path/lib/clang/$clang_version/lib/linux/libclang_rt.ubsan_standalone-$_sanitizer_arch-android.so" ]
}

# Creates a dist directory for a native executable.
#
# Running a native executable on a device requires all the shared library
# dependencies of that executable. To make it easier to install and run such an
# executable, this will create a directory containing the native exe and all
# it's library dependencies.
#
# Note: It's usually better to package things as an APK than as a native
# executable.
#
# Variables
#   dist_dir: Directory for the exe and libraries. Everything in this directory
#     will be deleted before copying in the exe and libraries.
#   binary: Path to (stripped) executable.
#   extra_files: List of extra files to copy in (optional).
#
# Example
#   create_native_executable_dist("foo_dist") {
#     dist_dir = "$root_build_dir/foo_dist"
#     binary = "$root_build_dir/foo"
#     deps = [ ":the_thing_that_makes_foo" ]
#   }
template("create_native_executable_dist") {
  forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

  _libraries_list = "${target_gen_dir}/${target_name}_library_dependencies.list"

  _sanitizer_runtimes_target_name = "${target_name}__sanitizer_runtimes"
  group(_sanitizer_runtimes_target_name) {
    metadata = {
      shared_libraries = _sanitizer_runtimes
    }
  }

  generated_file("${target_name}__library_list") {
    forward_variables_from(invoker, [ "deps" ])
    if (!defined(deps)) {
      deps = []
    }
    deps += [ ":${_sanitizer_runtimes_target_name}" ]
    output_conversion = "json"
    outputs = [ _libraries_list ]
    data_keys = [ "shared_libraries" ]
    walk_keys = [ "shared_libraries_barrier" ]
    rebase = root_build_dir
  }

  copy_ex(target_name) {
    inputs = [
      _libraries_list,
      invoker.binary,
    ]

    dest = invoker.dist_dir
    data = [ "${invoker.dist_dir}/" ]

    _rebased_libraries_list = rebase_path(_libraries_list, root_build_dir)
    _rebased_binaries_list = rebase_path([ invoker.binary ], root_build_dir)
    args = [
      "--clear",
      "--files=@FileArg($_rebased_libraries_list)",
      "--files=$_rebased_binaries_list",
    ]
    if (defined(invoker.extra_files)) {
      _rebased_extra_files = rebase_path(invoker.extra_files, root_build_dir)
      args += [ "--files=$_rebased_extra_files" ]
    }

    _depfile = "$target_gen_dir/$target_name.d"
    _stamp_file = "$target_gen_dir/$target_name.stamp"
    outputs = [ _stamp_file ]
    args += [
      "--depfile",
      rebase_path(_depfile, root_build_dir),
      "--stamp",
      rebase_path(_stamp_file, root_build_dir),
    ]

    deps = [ ":${target_name}__library_list" ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
  }
}

if (enable_java_templates) {
  import("//build/config/sanitizers/sanitizers.gni")

  # JNI target implementation. See generate_jni or generate_jar_jni for usage.
  template("generate_jni_impl") {
    _jni_output_dir = "${target_gen_dir}/${target_name}"
    if (defined(invoker.jni_generator_include)) {
      _jni_generator_include = invoker.jni_generator_include
      _jni_generator_include_deps = []
    } else {
      _jni_generator_include =
          "//base/android/jni_generator/jni_generator_helper.h"
      _jni_generator_include_deps = [
        # Using //base/android/jni_generator/jni_generator_helper.h introduces
        # a dependency on buildflags targets indirectly through
        # base/android/jni_android.h, which is part of the //base target.
        # This can't depend directly on //base without causing a dependency
        # cycle, though.
        "//base:debugging_buildflags",
        "//base:logging_buildflags",
        "//build:chromeos_buildflags",
      ]
    }

    action_with_pydeps(target_name) {
      # The sources aren't compiled so don't check their dependencies.
      check_includes = false
      script = "//base/android/jni_generator/jni_generator.py"
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "deps",
                                   "public_deps",
                                 ])
      if (!defined(public_deps)) {
        public_deps = []
      }
      public_deps += _jni_generator_include_deps
      inputs = []
      args = [
        "--ptr_type=long",
        "--includes",
        rebase_path(_jni_generator_include, _jni_output_dir),
      ]

      if (defined(invoker.classes)) {
        if (defined(invoker.jar_file)) {
          _jar_file = invoker.jar_file
        } else {
          _jar_file = android_sdk_jar
        }
        inputs += [ _jar_file ]
        args += [
          "--jar_file",
          rebase_path(_jar_file, root_build_dir),
        ]
        _input_args = invoker.classes
        _input_names = invoker.classes
        if (defined(invoker.always_mangle) && invoker.always_mangle) {
          args += [ "--always_mangle" ]
        }
      } else {
        assert(defined(invoker.sources))
        inputs += invoker.sources
        _input_args = rebase_path(invoker.sources, root_build_dir)
        _input_names = invoker.sources
        if (use_hashed_jni_names) {
          args += [ "--use_proxy_hash" ]
        }
        if (defined(invoker.namespace)) {
          args += [ "-n ${invoker.namespace}" ]
        }
      }
      if (defined(invoker.split_name)) {
        args += [ "--split_name=${invoker.split_name}" ]
      }

      outputs = []
      foreach(_name, _input_names) {
        _name_part = get_path_info(_name, "name")
        outputs += [ "${_jni_output_dir}/${_name_part}_jni.h" ]
      }

      # Avoid passing GN lists because not all webrtc embedders use //build.
      foreach(_output, outputs) {
        args += [
          "--output_file",
          rebase_path(_output, root_build_dir),
        ]
      }
      foreach(_input, _input_args) {
        args += [ "--input_file=$_input" ]
      }

      if (enable_profiling) {
        args += [ "--enable_profiling" ]
      }
      if (enable_jni_tracing) {
        args += [ "--enable_tracing" ]
      }
    }
  }

  # Declare a jni target
  #
  # This target generates the native jni bindings for a set of .java files.
  #
  # See base/android/jni_generator/jni_generator.py for more info about the
  # format of generating JNI bindings.
  #
  # Variables
  #   sources: list of .java files to generate jni for
  #   namespace: Specify the namespace for the generated header file.
  #   deps, public_deps: As normal
  #
  # Example
  #   # Target located in base/BUILD.gn.
  #   generate_jni("foo_jni") {
  #     # Generates gen/base/foo_jni/Foo_jni.h
  #     # To use: #include "base/foo_jni/Foo_jni.h"
  #     sources = [
  #       "android/java/src/org/chromium/foo/Foo.java",
  #       ...,
  #     ]
  #   }
  template("generate_jni") {
    generate_jni_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    }
  }

  # Declare a jni target for a prebuilt jar
  #
  # This target generates the native jni bindings for a set of classes in a .jar.
  #
  # See base/android/jni_generator/jni_generator.py for more info about the
  # format of generating JNI bindings.
  #
  # Variables
  #   classes: list of .class files in the jar to generate jni for. These should
  #     include the full path to the .class file.
  #   jar_file: the path to the .jar. If not provided, will default to the sdk's
  #     android.jar
  #   always_mangle: Mangle all generated method names. By default, the script
  #     only mangles methods that cause ambiguity due to method overload.
  #   deps, public_deps: As normal
  #
  # Example
  #   # Target located in base/BUILD.gn.
  #   generate_jar_jni("foo_jni") {
  #     # Generates gen/base/foo_jni/Runnable_jni.h
  #     # To use: #include "base/foo_jni/Runnable_jni.h"
  #     classes = [
  #       "android/view/Foo.class",
  #     ]
  #   }
  template("generate_jar_jni") {
    generate_jni_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    }
  }

  # Declare a jni registration target.
  #
  # This target generates a srcjar containing a copy of GEN_JNI.java, which has
  # the native methods of all dependent java files. It can also create a .h file
  # for use with manual JNI registration.
  #
  # The script does not scan any generated sources (those within .srcjars, or
  # within root_build_dir). This could be fixed by adding deps & logic to scan
  # .srcjars, but isn't currently needed.
  #
  # See base/android/jni_generator/jni_registration_generator.py for more info
  # about the format of the header file.
  #
  # Variables
  #   targets: List of .build_config.json supported targets to provide java sources.
  #   header_output: Path to the generated .h file (optional).
  #   sources_exclusions: List of .java files that should be skipped. (optional)
  #   namespace: Registration functions will be wrapped into this. (optional)
  #   require_native_mocks: Enforce that any native calls using
  #     org.chromium.base.annotations.NativeMethods must have a mock set
  #     (optional).
  #   enable_native_mocks: Allow native calls using
  #     org.chromium.base.annotations.NativeMethods to be mocked in tests
  #     (optional).
  #   no_transitive_deps: Generate registration for only the Java source in the
  #     specified target(s). This is useful for generating registration for
  #     feature modules, without including base module dependencies.
  #
  # Example
  #   generate_jni_registration("chrome_jni_registration") {
  #     targets = [ ":chrome_public_apk" ]
  #     header_output = "$target_gen_dir/$target_name.h"
  #     sources_exclusions = [
  #       "//path/to/Exception.java",
  #     ]
  #   }
  template("generate_jni_registration") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

      script = "//base/android/jni_generator/jni_registration_generator.py"
      inputs = []
      deps = []
      _srcjar_output = "$target_gen_dir/$target_name.srcjar"
      outputs = [ _srcjar_output ]
      depfile = "$target_gen_dir/$target_name.d"

      args = [
        "--srcjar-path",
        rebase_path(_srcjar_output, root_build_dir),
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]
      foreach(_target, invoker.targets) {
        deps += [ "${_target}$build_config_target_suffix($default_toolchain)" ]
        _build_config =
            get_label_info("${_target}($default_toolchain)", "target_gen_dir") +
            "/" + get_label_info("${_target}($default_toolchain)", "name") +
            ".build_config.json"
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        inputs += [ _build_config ]

        if (defined(invoker.no_transitive_deps) && invoker.no_transitive_deps) {
          args += [ "--sources-files=@FileArg($_rebased_build_config:deps_info:java_sources_file)" ]
        } else {
          args += [
            # This is a list of .sources files.
            "--sources-files=@FileArg($_rebased_build_config:deps_info:jni:all_source)",
          ]
        }
      }

      if (use_hashed_jni_names) {
        args += [ "--use_proxy_hash" ]
      }

      if (defined(invoker.enable_native_mocks) && invoker.enable_native_mocks) {
        args += [ "--enable_proxy_mocks" ]

        if (defined(invoker.require_native_mocks) &&
            invoker.require_native_mocks) {
          args += [ "--require_mocks" ]
        }
      }

      if (defined(invoker.header_output)) {
        outputs += [ invoker.header_output ]
        args += [
          "--header-path",
          rebase_path(invoker.header_output, root_build_dir),
        ]
      }

      if (defined(invoker.sources_exclusions)) {
        _rebase_sources_exclusions =
            rebase_path(invoker.sources_exclusions, root_build_dir)
        args += [ "--sources-exclusions=$_rebase_sources_exclusions" ]
      }

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

  # Declare a target for c-preprocessor-generated java files
  #
  # NOTE: For generating Java conterparts to enums prefer using the java_cpp_enum
  #       rule instead.
  #
  # This target generates java files using the host C pre-processor. Each file in
  # sources will be compiled using the C pre-processor. If include_path is
  # specified, it will be passed (with --I) to the pre-processor.
  #
  # This target will create a single .srcjar. Adding this target to an
  # android_library target's srcjar_deps will make the generated java files be
  # included in that library's final outputs.
  #
  # Variables
  #   sources: list of files to be processed by the C pre-processor. For each
  #     file in sources, there will be one .java file in the final .srcjar. For a
  #     file named FooBar.template, a java file will be created with name
  #     FooBar.java.
  #   inputs: additional compile-time dependencies. Any files
  #     `#include`-ed in the templates should be listed here.
  #   defines: List of -D arguments for the preprocessor.
  #
  # Example
  #   java_cpp_template("foo_generated_enum") {
  #     sources = [
  #       "android/java/templates/Foo.template",
  #     ]
  #     inputs = [
  #       "android/java/templates/native_foo_header.h",
  #     ]
  #   }
  template("java_cpp_template") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "deps",
                               "inputs",
                               "public_deps",
                               "sources",
                               "testonly",
                               "visibility",
                             ])
      script = "//build/android/gyp/gcc_preprocess.py"
      outputs = [ "$target_gen_dir/$target_name.srcjar" ]

      _include_dirs = [
        "//",
        root_gen_dir,
      ]
      _rebased_include_dirs = rebase_path(_include_dirs, root_build_dir)
      args = [
        "--include-dirs=$_rebased_include_dirs",
        "--output",
        rebase_path(outputs[0], root_build_dir),
      ]
      if (defined(invoker.defines)) {
        foreach(_define, invoker.defines) {
          args += [
            "--define",
            _define,
          ]
        }
      }
      args += rebase_path(sources, root_build_dir)
    }
  }

  # Declare a target for generating Java classes from C++ enums.
  #
  # This target generates Java files from C++ enums using a script.
  #
  # This target will create a single .srcjar. Adding this target to an
  # android_library target's srcjar_deps will make the generated java files be
  # included in that library's final outputs.
  #
  # Variables
  #   sources: list of files to be processed by the script. For each annotated
  #     enum contained in the sources files the script will generate a .java
  #     file with the same name as the name of the enum.
  #
  # Example
  #   java_cpp_enum("foo_generated_enum") {
  #     sources = [
  #       "src/native_foo_header.h",
  #     ]
  #   }
  template("java_cpp_enum") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "sources" ])

      # The sources aren't compiled so don't check their dependencies.
      check_includes = false
      script = "//build/android/gyp/java_cpp_enum.py"

      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
      _rebased_sources = rebase_path(invoker.sources, root_build_dir)

      args = [ "--srcjar=$_rebased_srcjar_path" ] + _rebased_sources
      outputs = [ _srcjar_path ]
    }
  }

  # Declare a target for generating Java classes with string constants matching
  # those found in C++ files using a python script.
  #
  # This target will create a single .srcjar. Adding this target to an
  # android_library target's srcjar_deps will make the generated java files be
  # included in that library's final outputs.
  #
  # Variables
  #   sources: list of files to be processed by the script. For each string
  #            constant in the source files, the script will add a corresponding
  #            Java string to the specified template file.
  # Example
  #   java_cpp_strings("foo_switches") {
  #     sources = [
  #       "src/foo_switches.cc",
  #     ]
  #     template = "src/templates/FooSwitches.java.tmpl
  #   }
  #
  # foo_switches.cc:
  #
  # // A switch.
  # const char kASwitch = "a-switch";
  #
  # FooSwitches.java.tmpl
  #
  # // Copyright {YEAR} The Chromium Authors. All rights reserved.
  # // Use of this source code is governed by a BSD-style license that can be
  # // found in the LICENSE file.
  #
  # // This file is autogenerated by
  # //     {SCRIPT_NAME}
  # // From
  # //     {SOURCE_PATH}, and
  # //     {TEMPLATE_PATH}
  #
  # package my.java.package;
  #
  # public abstract class FooSwitches {{
  #     // ...snip...
  # {NATIVE_STRINGS}
  #     // ...snip...
  # }}
  #
  # result:
  #   A FooSwitches.java file, defining a class named FooSwitches in the package
  #   my.java.package.
  template("java_cpp_strings") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "sources" ])

      # The sources aren't compiled so don't check their dependencies.
      check_includes = false
      script = "//build/android/gyp/java_cpp_strings.py"

      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
      _rebased_sources = rebase_path(invoker.sources, root_build_dir)
      _rebased_template = rebase_path(invoker.template, root_build_dir)

      args = [
        "--srcjar=$_rebased_srcjar_path",
        "--template=$_rebased_template",
      ]
      args += _rebased_sources
      sources += [ invoker.template ]

      outputs = [ _srcjar_path ]
    }
  }

  # Declare a target for generating Java classes with string constants matching
  # those found in C++ base::Feature declarations, using a python script.
  #
  # This target will create a single .srcjar. Adding this target to an
  # android_library target's srcjar_deps will make the generated java files be
  # included in that library's final outputs.
  #
  # Variables
  #   sources: list of files to be processed by the script. For each
  #            base::Feature in the source files, the script will add a
  #            corresponding Java string for that feature's name to the
  #            specified template file.
  # Example
  #   java_cpp_features("foo_features") {
  #     sources = [
  #       "src/foo_features.cc",
  #     ]
  #     template = "src/templates/FooFeatures.java.tmpl
  #   }
  #
  # foo_features.cc:
  #
  # // A feature.
  # const base::Feature kSomeFeature{"SomeFeature",
  #                                  base::FEATURE_DISABLED_BY_DEFAULT};
  #
  # FooFeatures.java.tmpl
  #
  # // Copyright $YEAR The Chromium Authors. All rights reserved.
  # // Use of this source code is governed by a BSD-style license that can be
  # // found in the LICENSE file.
  #
  # package my.java.package;
  #
  # public final class FooFeatures {{
  #     // ...snip...
  # {NATIVE_STRINGS}
  #     // ...snip...
  #     // Do not instantiate this class.
  #     private FooFeatures() {{}}
  # }}
  #
  # result:
  #   A FooFeatures.java file, defining a class named FooFeatures in the package
  #   my.java.package.
  template("java_cpp_features") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "sources" ])

      # The sources aren't compiled so don't check their dependencies.
      check_includes = false
      script = "//build/android/gyp/java_cpp_features.py"

      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
      _rebased_sources = rebase_path(invoker.sources, root_build_dir)
      _rebased_template = rebase_path(invoker.template, root_build_dir)

      args = [
        "--srcjar=$_rebased_srcjar_path",
        "--template=$_rebased_template",
      ]
      args += _rebased_sources
      sources += [ invoker.template ]

      outputs = [ _srcjar_path ]
    }
  }

  # Declare a target for processing a Jinja template.
  #
  # Variables
  #   input: The template file to be processed.
  #   includes: List of files {% include %}'ed by input.
  #   output: Where to save the result.
  #   variables: (Optional) A list of variables to make available to the template
  #     processing environment, e.g. ["name=foo", "color=red"].
  #
  # Example
  #   jinja_template("chrome_public_manifest") {
  #     input = "java/AndroidManifest.xml"
  #     output = "$target_gen_dir/AndroidManifest.xml"
  #   }
  template("jinja_template") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      inputs = [ invoker.input ]
      if (defined(invoker.includes)) {
        inputs += invoker.includes
      }
      script = "//build/android/gyp/jinja_template.py"

      outputs = [ invoker.output ]

      args = [
        "--loader-base-dir",
        rebase_path("//", root_build_dir),
        "--inputs",
        rebase_path(invoker.input, root_build_dir),
        "--output",
        rebase_path(invoker.output, root_build_dir),
        "--check-includes",
      ]
      if (defined(invoker.includes)) {
        _rebased_includes = rebase_path(invoker.includes, root_build_dir)
        args += [ "--includes=$_rebased_includes" ]
      }
      if (defined(invoker.variables)) {
        args += [ "--variables=${invoker.variables}" ]
      }
    }
  }

  # Writes native libraries to a NativeLibaries.java file.
  #
  # This target will create a single .srcjar. Adding this target to an
  # android_library target's srcjar_deps will make the generated java files be
  # included in that library's final outputs.
  #
  # Variables:
  #   native_libraries_list_file: (Optional) Path to file listing all native
  #     libraries to write.
  #   version_number: (Optional) String of expected version of 'main' native
  #     library.
  #   enable_chromium_linker: (Optional) Whether to use the Chromium linker.
  #   load_library_from_apk: (Optional) Whether libraries should be loaded from
  #     the APK without uncompressing.
  #   use_final_fields: True to use final fields. When false, all other
  #       variables must not be set.
  template("write_native_libraries_java") {
    _native_libraries_file = "$target_gen_dir/$target_name.srcjar"
    if (target_cpu == "arm" || target_cpu == "arm64") {
      _cpu_family = "CPU_FAMILY_ARM"
    } else if (target_cpu == "x86" || target_cpu == "x64") {
      _cpu_family = "CPU_FAMILY_X86"
    } else if (target_cpu == "mipsel" || target_cpu == "mips64el") {
      _cpu_family = "CPU_FAMILY_MIPS"
    } else {
      assert(false, "Unsupported CPU family")
    }

    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      script = "//build/android/gyp/write_native_libraries_java.py"
      outputs = [ _native_libraries_file ]
      args = [
        "--output",
        rebase_path(_native_libraries_file, root_build_dir),
        "--cpu-family",
        _cpu_family,
      ]
      if (invoker.use_final_fields) {
        # Write native_libraries_list_file via depfile rather than specifyin it
        # as a dep in order allow R8 to run in parallel with native compilation.
        depfile = "$target_gen_dir/$target_name.d"
        args += [
          "--final",
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--native-libraries-list",
          rebase_path(invoker.native_libraries_list_file, root_build_dir),
        ]
        if (defined(invoker.main_component_library)) {
          args += [
            "--main-component-library",
            invoker.main_component_library,
          ]
        }
        if (defined(invoker.enable_chromium_linker) &&
            invoker.enable_chromium_linker) {
          args += [ "--enable-chromium-linker" ]
        }
        if (defined(invoker.load_library_from_apk) &&
            invoker.load_library_from_apk) {
          args += [ "--load-library-from-apk" ]
        }
        if (defined(invoker.use_modern_linker) && invoker.use_modern_linker) {
          args += [ "--use-modern-linker" ]
        }
      }
    }
  }

  # Declare a target for a set of Android resources generated at build
  # time and stored in a single zip archive. The content of the archive
  # should match the layout of a regular Android res/ folder (but the
  # archive should not include a top-level res/ directory).
  #
  # Note that there is no associated .srcjar, R.txt or package name
  # associated with this target.
  #
  # Variables:
  #   generated_resources_zip: Generated zip archive path.
  #   generating_target: Name of the target generating
  #     generated_resources_zip. This rule will check that it is part
  #     of its outputs.
  #   deps: Specifies the dependencies of this target. Any Android resources
  #     listed here will be also be included *after* this one when compiling
  #     all resources for a final apk or junit binary. This is useful to
  #     ensure that the resources of the current target override those of the
  #     dependency as well (and would not work if you have these deps to the
  #     generating target's dependencies).
  #
  # Example
  #   _zip_archive = "$target_gen_dir/${target_name}.resources_zip"
  #
  #   action("my_resources__create_zip") {
  #     _depfile = "$target_gen_dir/${target_name}.d"
  #     script = "//build/path/to/create_my_resources_zip.py"
  #     args = [
  #       "--depfile", rebase_path(_depfile, root_build_dir),
  #       "--output-zip", rebase_path(_zip_archive, root_build_dir),
  #     ]
  #     inputs = []
  #     outputs = _zip_archive
  #     depfile = _depfile
  #   }
  #
  #   android_generated_resources("my_resources") {
  #      generated_resources_zip = _zip_archive
  #      generating_target = ":my_resources__create_zip"
  #   }
  #
  template("android_generated_resources") {
    forward_variables_from(invoker, [ "testonly" ])
    _build_config = "$target_gen_dir/${target_name}.build_config.json"
    _rtxt_out_path = "$target_gen_dir/${target_name}.R.txt"
    write_build_config("$target_name$build_config_target_suffix") {
      forward_variables_from(invoker, [ "resource_overlay" ])

      build_config = _build_config
      resources_zip = invoker.generated_resources_zip
      type = "android_resources"
      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }
      r_text = _rtxt_out_path
    }
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, [ "visibility" ])
      public_deps = [
        ":$target_name$build_config_target_suffix",
        invoker.generating_target,
      ]
      inputs = [ invoker.generated_resources_zip ]
      outputs = [ _rtxt_out_path ]
      script = "//build/android/gyp/create_r_txt.py"
      args = [
        "--resources-zip-path",
        rebase_path(invoker.generated_resources_zip, root_build_dir),
        "--rtxt-path",
        rebase_path(_rtxt_out_path, root_build_dir),
      ]
    }
  }

  # Declare a target for processing Android resources as Jinja templates.
  #
  # This takes an Android resource directory where each resource is a Jinja
  # template, processes each template, then packages the results in a zip file
  # which can be consumed by an android resources, library, or apk target.
  #
  # If this target is included in the deps of an android resources/library/apk,
  # the resources will be included with that target.
  #
  # Variables
  #   resources: The list of resources files to process.
  #   res_dir: The resource directory containing the resources.
  #   variables: (Optional) A list of variables to make available to the template
  #     processing environment, e.g. ["name=foo", "color=red"].
  #
  # Example
  #   jinja_template_resources("chrome_public_template_resources") {
  #     res_dir = "res_template"
  #     resources = ["res_template/xml/syncable.xml"]
  #     variables = ["color=red"]
  #   }
  template("jinja_template_resources") {
    _resources_zip = "$target_out_dir/${target_name}.resources.zip"
    _generating_target_name = "${target_name}__template"

    action_with_pydeps(_generating_target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      inputs = invoker.resources
      script = "//build/android/gyp/jinja_template.py"

      outputs = [ _resources_zip ]

      _rebased_resources = rebase_path(invoker.resources, root_build_dir)
      args = [
        "--inputs=${_rebased_resources}",
        "--inputs-base-dir",
        rebase_path(invoker.res_dir, root_build_dir),
        "--outputs-zip",
        rebase_path(_resources_zip, root_build_dir),
        "--check-includes",
      ]
      if (defined(invoker.variables)) {
        variables = invoker.variables
        args += [ "--variables=${variables}" ]
      }
    }

    android_generated_resources(target_name) {
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "deps",
                                   "resource_overlay",
                                 ])
      generating_target = ":$_generating_target_name"
      generated_resources_zip = _resources_zip
    }
  }

  # Declare a prebuilt android native library.
  #
  # This takes a base directory and library name and then looks for the library
  # in <base dir>/$android_app_abi/<library name>.
  #
  # If you depend on this target, the library is stripped and output to the
  # same locations non-prebuilt libraries are output.
  #
  # Variables
  #   base_dir: Directory where all ABIs of the library live.
  #   library_name: Name of the library .so file.
  #
  # Example
  #   android_native_prebuilt("elements_native") {
  #     base_dir = "//third_party/elements"
  #     lib_name = "elements.so"
  #   }
  template("android_native_prebuilt") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])
      script = "//build/android/gyp/process_native_prebuilt.py"
      _lib_path = "${invoker.base_dir}/$android_app_abi/${invoker.lib_name}"
      _stripped_output_path = "$root_out_dir/${invoker.lib_name}"
      _unstripped_output_path =
          "$root_out_dir/lib.unstripped/${invoker.lib_name}"
      inputs = [ _lib_path ]
      outputs = [
        _stripped_output_path,
        _unstripped_output_path,
      ]

      # Add unstripped output to runtime deps for use by bots during stacktrace
      # symbolization.
      data = [ _unstripped_output_path ]

      _rebased_lib_path = rebase_path(_lib_path, root_build_dir)
      _rebased_stripped_ouput_path =
          rebase_path(_stripped_output_path, root_build_dir)
      _rebased_unstripped_ouput_path =
          rebase_path(_unstripped_output_path, root_build_dir)
      _strip_tool_path =
          rebase_path("//buildtools/third_party/eu-strip/bin/eu-strip",
                      root_build_dir)

      args = [
        "--strip-path=$_strip_tool_path",
        "--input-path=$_rebased_lib_path",
        "--stripped-output-path=$_rebased_stripped_ouput_path",
        "--unstripped-output-path=$_rebased_unstripped_ouput_path",
      ]
    }
  }

  # Declare an Android resources target
  #
  # This creates a resources zip file that will be used when building an Android
  # library or apk and included into a final apk.
  #
  # To include these resources in a library/apk, this target should be listed in
  # the library's deps. A library/apk will also include any resources used by its
  # own dependencies.
  #
  # Variables
  #   sources: List of resource files for this target.
  #   deps: Specifies the dependencies of this target. Any Android resources
  #     listed in deps will be included by libraries/apks that depend on this
  #     target.
  #   alternative_android_sdk_dep: Optional. Alternative Android system
  #     android java target to use.
  #   android_manifest: AndroidManifest.xml for this target (optional). Will be
  #     merged into apks that directly or indirectly depend on this target.
  #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
  #   custom_package: java package for generated .java files.
  #   allow_missing_resources: Do not fail if a resource exists in a directory
  #      but is not listed in sources.
  #   shared_resources: If true make a resource package that can be loaded by a
  #     different application at runtime to access the package's resources.
  #   resource_overlay: Whether the resources in 'sources' should override
  #     resources with the same name. Does not affect the behaviour of any
  #     android_resources() deps of this target. If a target with
  #     resource_overlay=true depends on another target with
  #     resource_overlay=true the target with the dependency overrides the
  #     other.
  #   r_text_file: (optional) path to pre-generated R.txt to be used when
  #     generating R.java instead of resource-based aapt-generated one.
  #   recursive_resource_deps: (optional) whether deps should be walked
  #     recursively to find resource deps.
  #
  # Example:
  #   android_resources("foo_resources") {
  #     deps = [":foo_strings_grd"]
  #     sources = [
  #       "res/drawable/foo1.xml",
  #       "res/drawable/foo2.xml",
  #     ]
  #     custom_package = "org.chromium.foo"
  #   }
  #
  #   android_resources("foo_resources_overrides") {
  #     deps = [":foo_resources"]
  #     sources = [
  #       "res_overrides/drawable/foo1.xml",
  #       "res_overrides/drawable/foo2.xml",
  #     ]
  #   }
  template("android_resources") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _base_path = "$target_gen_dir/$target_name"
    if (defined(invoker.v14_skip)) {
      not_needed(invoker, [ "v14_skip" ])
    }

    _res_sources_path = "$target_gen_dir/${invoker.target_name}.res.sources"

    _resources_zip = "$target_out_dir/$target_name.resources.zip"
    _r_text_out_path = _base_path + "_R.txt"
    _build_config = _base_path + ".build_config.json"
    _build_config_target_name = "$target_name$build_config_target_suffix"

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

    if (defined(invoker.alternative_android_sdk_dep)) {
      _deps += [ invoker.alternative_android_sdk_dep ]
    } else {
      _deps += [ "//third_party/android_sdk:android_sdk_java" ]
    }

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

    _rebased_resource_files = rebase_path(_resource_files, root_build_dir)
    write_file(_res_sources_path, _rebased_resource_files)

    # This is necessary so we only lint chromium resources.
    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(":$target_name", "dir") ],
                         [ "*\bthird_party\b*" ]) != []
    }

    write_build_config(_build_config_target_name) {
      type = "android_resources"
      build_config = _build_config
      resources_zip = _resources_zip
      res_sources_path = _res_sources_path
      chromium_code = _chromium_code

      forward_variables_from(invoker,
                             [
                               "android_manifest",
                               "android_manifest_dep",
                               "custom_package",
                               "mergeable_android_manifests",
                               "resource_overlay",
                               "recursive_resource_deps",
                             ])

      r_text = _r_text_out_path
      possible_config_deps = _deps

      # Always merge manifests from resources.
      # * Might want to change this at some point for consistency and clarity,
      #   but keeping for backwards-compatibility.
      if (!defined(mergeable_android_manifests) && defined(android_manifest)) {
        mergeable_android_manifests = [ android_manifest ]
      }
    }

    prepare_resources(target_name) {
      forward_variables_from(invoker,
                             [
                               "strip_drawables",
                               "allow_missing_resources",
                               "visibility",
                             ])
      deps = _deps

      res_sources_path = _res_sources_path
      sources = _resource_files

      resources_zip = _resources_zip
      r_text_out_path = _r_text_out_path

      if (defined(invoker.r_text_file)) {
        r_text_in_path = invoker.r_text_file
      }
    }
  }

  # Declare an Android assets target.
  #
  # Defines a set of files to include as assets in a dependent apk.
  #
  # To include these assets in an apk, this target should be listed in
  # the apk's deps, or in the deps of a library target used by an apk.
  #
  # Variables
  #   deps: Specifies the dependencies of this target. Any Android assets
  #     listed in deps will be included by libraries/apks that depend on this
  #     target.
  #   sources: List of files to include as assets.
  #   renaming_sources: List of files to include as assets and be renamed.
  #   renaming_destinations: List of asset paths for files in renaming_sources.
  #   disable_compression: Whether to disable compression for files that are
  #     known to be compressable (default: false).
  #   treat_as_locale_paks: Causes base's BuildConfig.java to consider these
  #     assets to be locale paks.
  #
  # Example:
  # android_assets("content_shell_assets") {
  #   deps = [
  #     ":generates_foo",
  #     ":other_assets",
  #     ]
  #   sources = [
  #     "//path/asset1.png",
  #     "//path/asset2.png",
  #     "$target_gen_dir/foo.dat",
  #   ]
  # }
  #
  # android_assets("overriding_content_shell_assets") {
  #   deps = [ ":content_shell_assets" ]
  #   # Override foo.dat from content_shell_assets.
  #   sources = [ "//custom/foo.dat" ]
  #   renaming_sources = [ "//path/asset2.png" ]
  #   renaming_destinations = [ "renamed/asset2.png" ]
  # }
  template("android_assets") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _build_config = "$target_gen_dir/$target_name.build_config.json"
    _build_config_target_name = "$target_name$build_config_target_suffix"

    write_build_config(_build_config_target_name) {
      type = "android_assets"
      build_config = _build_config

      forward_variables_from(invoker,
                             [
                               "disable_compression",
                               "treat_as_locale_paks",
                             ])

      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }

      if (defined(invoker.sources)) {
        asset_sources = invoker.sources
      }
      if (defined(invoker.renaming_sources)) {
        assert(defined(invoker.renaming_destinations))
        _source_count = 0
        foreach(_, invoker.renaming_sources) {
          _source_count += 1
        }
        _dest_count = 0
        foreach(_, invoker.renaming_destinations) {
          _dest_count += 1
        }
        assert(
            _source_count == _dest_count,
            "android_assets() renaming_sources.length != renaming_destinations.length")
        asset_renaming_sources = invoker.renaming_sources
        asset_renaming_destinations = invoker.renaming_destinations
      }
    }

    group(target_name) {
      forward_variables_from(invoker, [ "deps" ])
      public_deps = [ ":$_build_config_target_name" ]
    }
  }

  # Declare a group() that supports forwarding java dependency information.
  #
  # Example
  #  java_group("conditional_deps") {
  #    if (enable_foo) {
  #      deps = [":foo_java"]
  #    }
  #  }
  template("java_group") {
    _build_config_vars = [
      "input_jars_paths",
      "mergeable_android_manifests",
      "proguard_configs",
    ]
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    write_build_config("$target_name$build_config_target_suffix") {
      forward_variables_from(invoker, _build_config_vars)
      type = "group"
      build_config = "$target_gen_dir/${invoker.target_name}.build_config.json"
      supports_android = true
      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }
    }
    foreach(_group_name,
            [
              "header",
              "impl",
              "assetres",
            ]) {
      java_lib_group("${target_name}__${_group_name}") {
        forward_variables_from(invoker, [ "deps" ])
        group_name = _group_name
      }
    }
    group(target_name) {
      forward_variables_from(invoker,
                             "*",
                             _build_config_vars + TESTONLY_AND_VISIBILITY)
      if (!defined(deps)) {
        deps = []
      }
      deps += [ ":$target_name$build_config_target_suffix" ]
    }
  }

  # Declare a Java executable target
  #
  # Same as java_library, but also creates a wrapper script within
  # $root_out_dir/bin.
  #
  # Supports all variables of java_library(), plus:
  #   main_class: When specified, a wrapper script is created within
  #     $root_build_dir/bin to launch the binary with the given class as the
  #     entrypoint.
  #   wrapper_script_name: Filename for the wrapper script (default=target_name)
  #   wrapper_script_args: List of additional arguments for the wrapper script.
  #
  # Example
  #   java_binary("foo") {
  #     sources = [ "org/chromium/foo/FooMain.java" ]
  #     deps = [ ":bar_java" ]
  #     main_class = "org.chromium.foo.FooMain"
  #   }
  #
  #   java_binary("foo") {
  #     jar_path = "lib/prebuilt.jar"
  #     deps = [ ":bar_java" ]
  #     main_class = "org.chromium.foo.FooMain"
  #   }
  template("java_binary") {
    java_library_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      type = "java_binary"
      if (!defined(data_deps)) {
        data_deps = []
      }
      data_deps += [ "//third_party/jdk:java_data" ]
    }
  }

  # Declare a Java Annotation Processor.
  #
  # Supports all variables of java_library(), plus:
  #   jar_path: Path to a prebuilt jar. Mutually exclusive with sources &
  #     srcjar_deps.
  #   main_class: The fully-quallified class name of the processor's entry
  #       point.
  #
  # Example
  #   java_annotation_processor("foo_processor") {
  #     sources = [ "org/chromium/foo/FooProcessor.java" ]
  #     deps = [ ":bar_java" ]
  #     main_class = "org.chromium.foo.FooProcessor"
  #   }
  #
  #   java_annotation_processor("foo_processor") {
  #     jar_path = "lib/prebuilt.jar"
  #     main_class = "org.chromium.foo.FooMain"
  #   }
  #
  #   java_library("...") {
  #     annotation_processor_deps = [":foo_processor"]
  #   }
  #
  template("java_annotation_processor") {
    java_library_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      type = "java_annotation_processor"
    }
  }

  # Declare a Junit executable target
  #
  # This target creates an executable from java code for running as a junit test
  # suite. The executable will be in the output folder's /bin/ directory.
  #
  # Supports all variables of java_binary().
  #
  # Example
  #   junit_binary("foo") {
  #     sources = [ "org/chromium/foo/FooTest.java" ]
  #     deps = [ ":bar_java" ]
  #   }
  template("junit_binary") {
    testonly = true

    _java_binary_target_name = "${target_name}__java_binary"
    _test_runner_target_name = "${target_name}__test_runner_script"
    _main_class = "org.chromium.testing.local.JunitTestMain"

    _build_config = "$target_gen_dir/$target_name.build_config.json"
    _build_config_target_name = "$target_name$build_config_target_suffix"
    _deps = [
      "//testing/android/junit:junit_test_support",
      "//third_party/android_deps:robolectric_all_java",
      "//third_party/junit",
      "//third_party/mockito:mockito_java",
    ]
    if (defined(invoker.deps)) {
      _deps += invoker.deps
    }
    if (defined(invoker.alternative_android_sdk_dep)) {
      _android_sdk_dep = invoker.alternative_android_sdk_dep
    } else {
      _android_sdk_dep = "//third_party/android_sdk:android_sdk_java"
    }

    # a package name or a manifest is required to have resources. This is
    # added so that junit tests that do not care about the package name can
    # still use resources without having to explicitly set one.
    if (defined(invoker.package_name)) {
      _package_name = invoker.package_name
    } else if (!defined(invoker.android_manifest)) {
      _package_name = "org.chromium.test"
    }

    _resource_arsc_output = "${target_gen_dir}/${target_name}.ap_"
    _compile_resources_target = "${target_name}__compile_resources"
    compile_resources(_compile_resources_target) {
      forward_variables_from(invoker, [ "android_manifest" ])
      deps = _deps
      android_sdk_dep = _android_sdk_dep
      build_config_dep = ":$_build_config_target_name"
      build_config = _build_config
      if (defined(_package_name)) {
        rename_manifest_package = _package_name
      }
      if (!defined(android_manifest)) {
        android_manifest = "//build/android/AndroidManifest.xml"
      }
      arsc_output = _resource_arsc_output
      min_sdk_version = default_min_sdk_version
      target_sdk_version = android_sdk_version
    }

    _jni_srcjar_target = "${target_name}__final_jni"
    _outer_target_name = target_name
    generate_jni_registration(_jni_srcjar_target) {
      enable_native_mocks = true
      require_native_mocks = true
      targets = [ ":$_outer_target_name" ]
    }

    java_library_impl(_java_binary_target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY + [ "deps" ])
      type = "junit_binary"
      main_target_name = invoker.target_name

      # Include the android SDK jar(s) for resource processing.
      include_android_sdk = true

      # Robolectric can handle deps that set !supports_android as well those
      # that set requires_android.
      bypass_platform_checks = true
      deps = _deps
      testonly = true
      main_class = _main_class
      wrapper_script_name = "helper/$main_target_name"

      # As of April 2021, adding -XX:TieredStopAtLevel=1 does not affect the
      # wall time of a single robolectric shard, but does reduce the CPU time by
      # 66%, which makes sharding more effective.
      tiered_stop_at_level_one = true

      if (!defined(srcjar_deps)) {
        srcjar_deps = []
      }
      srcjar_deps += [
        ":$_compile_resources_target",
        ":$_jni_srcjar_target",

        # This dep is required for any targets that depend on //base:base_java.
        "//build/android:build_config_gen",
      ]
    }

    test_runner_script(_test_runner_target_name) {
      test_name = invoker.target_name
      test_suite = invoker.target_name
      test_type = "junit"
      ignore_all_data_deps = true
      resource_apk = _resource_arsc_output
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "assert_no_deps",
                               "visibility",
                             ])
      public_deps = [
        ":$_build_config_target_name",
        ":$_java_binary_target_name",
        ":$_test_runner_target_name",
      ]
    }
  }

  # Declare a java library target
  #
  # Variables
  #   deps: Specifies the dependencies of this target. Java targets in this list
  #     will be added to the javac classpath.
  #   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.
  #   annotation_processor_deps: List of java_annotation_processor targets to
  #     use when compiling.
  #
  #   jar_path: Path to a prebuilt jar. Mutually exclusive with sources &
  #     srcjar_deps.
  #   sources: List of .java files included in this library.
  #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
  #     will be added to sources and be included in this library.
  #
  #   input_jars_paths: A list of paths to the jars that should be included
  #     in the compile-time classpath. These are in addition to library .jars
  #     that appear in deps.
  #
  #   chromium_code: If true, extra analysis warning/errors will be enabled.
  #   enable_errorprone: If true, enables the errorprone compiler.
  #   skip_build_server: If true, avoids sending tasks to the build server.
  #
  #   jar_excluded_patterns: List of patterns of .class files to exclude.
  #   jar_included_patterns: List of patterns of .class files to include.
  #     When omitted, all classes not matched by jar_excluded_patterns are
  #     included. When specified, all non-matching .class files are stripped.
  #
  #   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.
  #
  #   output_name: File name for the output .jar (not including extension).
  #     Defaults to the input .jar file name.
  #
  #   proguard_configs: List of proguard configs to use in final apk step for
  #     any apk that depends on this library.
  #
  #   supports_android: If true, Android targets (android_library, android_apk)
  #     may depend on this target. Note: if true, this target must only use the
  #     subset of Java available on Android.
  #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
  #     dependencies for this target. This will allow depending on an
  #     android_library target, for example.
  #   enable_desugar: If false, disables desugaring of lambdas, etc. Use this
  #     only when you are sure the library does not require desugaring. E.g.
  #     to hide warnings shown from desugaring.
  #
  #   additional_jar_files: Use to package additional files (Java resources)
  #     into the output jar. Pass a list of length-2 lists with format:
  #         [ [ path_to_file, path_to_put_in_jar ] ]
  #
  #   javac_args: Additional arguments to pass to javac.
  #   errorprone_args: Additional arguments to pass to errorprone.
  #
  #   data_deps, testonly
  #
  # Example
  #   java_library("foo_java") {
  #     sources = [
  #       "org/chromium/foo/Foo.java",
  #       "org/chromium/foo/FooInterface.java",
  #       "org/chromium/foo/FooService.java",
  #     ]
  #     deps = [
  #       ":bar_java"
  #     ]
  #     srcjar_deps = [
  #       ":foo_generated_enum"
  #     ]
  #     jar_excluded_patterns = [
  #       "*/FooService.class", "org/chromium/FooService\$*.class"
  #     ]
  #   }
  template("java_library") {
    java_library_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      type = "java_library"
    }
  }

  # Declare a java library target for a prebuilt jar
  #
  # Supports all variables of java_library().
  #
  # Example
  #   java_prebuilt("foo_java") {
  #     jar_path = "foo.jar"
  #     deps = [
  #       ":foo_resources",
  #       ":bar_java"
  #     ]
  #   }
  template("java_prebuilt") {
    java_library_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      type = "java_library"
    }
  }

  # Combines all dependent .jar files into a single .jar file.
  #
  # Variables:
  #   output: Path to the output jar.
  #   override_build_config: Use a pre-existing .build_config. Must be of type
  #     "apk".
  #   use_interface_jars: Use all dependent interface .jars rather than
  #     implementation .jars.
  #   use_unprocessed_jars: Use unprocessed / undesugared .jars.
  #   direct_deps_only: Do not recurse on deps.
  #   jar_excluded_patterns (optional)
  #     List of globs for paths to exclude.
  #
  # Example
  #   dist_jar("lib_fatjar") {
  #     deps = [ ":my_java_lib" ]
  #     output = "$root_build_dir/MyLibrary.jar"
  #   }
  template("dist_jar") {
    # TODO(crbug.com/1042017): Remove.
    not_needed(invoker, [ "no_build_hooks" ])
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    _supports_android =
        !defined(invoker.supports_android) || invoker.supports_android
    _use_interface_jars =
        defined(invoker.use_interface_jars) && invoker.use_interface_jars
    _use_unprocessed_jars =
        defined(invoker.use_unprocessed_jars) && invoker.use_unprocessed_jars
    _direct_deps_only =
        defined(invoker.direct_deps_only) && invoker.direct_deps_only
    assert(!(_use_unprocessed_jars && _use_interface_jars),
           "Cannot set both use_interface_jars and use_unprocessed_jars")

    _jar_target_name = target_name

    _deps = []
    if (defined(invoker.deps)) {
      _deps = invoker.deps
    }
    if (_supports_android) {
      _deps += [ "//third_party/android_sdk:android_sdk_java" ]
    }

    if (defined(invoker.override_build_config)) {
      _build_config = invoker.override_build_config
    } else {
      _build_config = "$target_gen_dir/$target_name.build_config.json"
      _build_config_target_name = "$target_name$build_config_target_suffix"

      write_build_config(_build_config_target_name) {
        type = "dist_jar"
        supports_android = _supports_android
        requires_android =
            defined(invoker.requires_android) && invoker.requires_android
        possible_config_deps = _deps
        ignore_dependency_public_deps = _direct_deps_only
        build_config = _build_config
      }

      _deps += [ ":$_build_config_target_name" ]
    }

    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    action_with_pydeps(_jar_target_name) {
      forward_variables_from(invoker, [ "data" ])
      script = "//build/android/gyp/zip.py"
      depfile = "$target_gen_dir/$target_name.d"
      deps = _deps

      inputs = [ _build_config ]

      outputs = [ invoker.output ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--output",
        rebase_path(invoker.output, root_build_dir),
        "--no-compress",
      ]

      if (_direct_deps_only) {
        if (_use_interface_jars) {
          args += [ "--input-zips=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
        } else if (_use_unprocessed_jars) {
          args += [
            "--input-zips=@FileArg($_rebased_build_config:javac:classpath)",
          ]
        } else {
          assert(
              false,
              "direct_deps_only does not work without use_interface_jars or use_unprocessed_jars")
        }
      } else {
        if (_use_interface_jars) {
          args += [ "--input-zips=@FileArg($_rebased_build_config:dist_jar:all_interface_jars)" ]
        } else if (_use_unprocessed_jars) {
          args += [ "--input-zips=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)" ]
        } else {
          args += [ "--input-zips=@FileArg($_rebased_build_config:deps_info:device_classpath)" ]
        }
      }
      _excludes = []
      if (defined(invoker.jar_excluded_patterns)) {
        _excludes += invoker.jar_excluded_patterns
      }
      if (_use_interface_jars) {
        # Turbine adds files like: META-INF/TRANSITIVE/.../Foo.class
        # These confuse proguard: https://crbug.com/1081443
        _excludes += [ "META-INF/*" ]
      }
      if (_excludes != []) {
        args += [ "--input-zips-excluded-globs=$_excludes" ]
      }
    }
  }

  # Combines all dependent .jar files into a single proguarded .dex file.
  #
  # Variables:
  #   output: Path to the output dex.
  #   proguard_enabled: Whether to enable R8.
  #   proguard_configs: List of proguard configs.
  #   proguard_enable_obfuscation: Whether to enable obfuscation (default=true).
  #
  # Example
  #   dist_dex("lib_fatjar") {
  #     deps = [ ":my_java_lib" ]
  #     output = "$root_build_dir/MyLibrary.jar"
  #   }
  template("dist_dex") {
    _deps = [ "//third_party/android_sdk:android_sdk_java" ]
    if (defined(invoker.deps)) {
      _deps += invoker.deps
    }

    _build_config = "$target_gen_dir/$target_name.build_config.json"
    _build_config_target_name = "$target_name$build_config_target_suffix"

    write_build_config(_build_config_target_name) {
      type = "dist_jar"
      forward_variables_from(invoker,
                             [
                               "proguard_configs",
                               "proguard_enabled",
                             ])
      supports_android = true
      requires_android = true
      possible_config_deps = _deps
      build_config = _build_config
    }

    _deps += [ ":$_build_config_target_name" ]

    dex(target_name) {
      forward_variables_from(invoker,
                             TESTONLY_AND_VISIBILITY + [
                                   "data",
                                   "data_deps",
                                   "proguard_configs",
                                   "proguard_enabled",
                                   "proguard_enable_obfuscation",
                                   "min_sdk_version",
                                 ])
      deps = _deps
      build_config = _build_config
      enable_multidex = false
      output = invoker.output
      if (defined(proguard_enabled) && proguard_enabled) {
        # The individual dependencies would have caught real missing deps in
        # their respective dex steps. False positives that were suppressed at
        # per-target dex steps are emitted here since this is using jar files
        # rather than dex files.
        ignore_desugar_missing_deps = true

        # When trying to build a stand-alone .dex, don't add in jdk_libs_dex.
        supports_jdk_library_desugaring = false
      } else {
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        input_dex_filearg =
            "@FileArg(${_rebased_build_config}:final_dex:all_dex_files)"
      }
    }
  }

  # Creates an Android .aar library.
  #
  # Currently supports:
  #   * AndroidManifest.xml
  #   * classes.jar
  #   * jni/
  #   * res/
  #   * R.txt
  #   * proguard.txt
  # Does not yet support:
  #   * public.txt
  #   * annotations.zip
  #   * assets/
  # See: https://developer.android.com/studio/projects/android-library.html#aar-contents
  #
  # Variables:
  #   output: Path to the output .aar.
  #   proguard_configs: List of proguard configs (optional).
  #   android_manifest: Path to AndroidManifest.xml (optional).
  #   native_libraries: list of native libraries (optional).
  #   direct_deps_only: Do not recurse on deps. (optional, defaults false).
  #   jar_excluded_patterns (optional): List of globs for paths to exclude.
  #   jar_included_patterns (optional): List of globs for paths to include.
  #
  # Example
  #   dist_aar("my_aar") {
  #     deps = [ ":my_java_lib" ]
  #     output = "$root_build_dir/MyLibrary.aar"
  #   }
  template("dist_aar") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

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

    _direct_deps_only =
        defined(invoker.direct_deps_only) && invoker.direct_deps_only

    _build_config = "$target_gen_dir/$target_name.build_config.json"
    _build_config_target_name = "$target_name$build_config_target_suffix"

    write_build_config(_build_config_target_name) {
      type = "dist_aar"
      forward_variables_from(invoker, [ "proguard_configs" ])
      possible_config_deps = _deps
      supports_android = true
      requires_android = true
      ignore_dependency_public_deps = _direct_deps_only
      build_config = _build_config
    }

    _deps += [ ":$_build_config_target_name" ]

    _rebased_build_config = rebase_path(_build_config, root_build_dir)

    action_with_pydeps(target_name) {
      forward_variables_from(invoker, [ "data" ])
      depfile = "$target_gen_dir/$target_name.d"
      deps = _deps
      script = "//build/android/gyp/dist_aar.py"

      inputs = [ _build_config ]

      # Although these will be listed as deps in the depfile, they must also
      # appear here so that "gn analyze" knows about them.
      # https://crbug.com/827197
      if (defined(invoker.proguard_configs)) {
        inputs += invoker.proguard_configs
      }

      outputs = [ invoker.output ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--output",
        rebase_path(invoker.output, root_build_dir),
        "--dependencies-res-zips=@FileArg($_rebased_build_config:deps_info:dependency_zips)",
        "--r-text-files=@FileArg($_rebased_build_config:deps_info:dependency_r_txt_files)",
        "--proguard-configs=@FileArg($_rebased_build_config:deps_info:proguard_all_configs)",
      ]
      if (_direct_deps_only) {
        args += [ "--jars=@FileArg($_rebased_build_config:javac:classpath)" ]
      } else {
        args += [ "--jars=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)" ]
      }
      if (defined(invoker.android_manifest)) {
        args += [
          "--android-manifest",
          rebase_path(invoker.android_manifest, root_build_dir),
        ]
      }
      if (defined(invoker.native_libraries) && invoker.native_libraries != []) {
        inputs += invoker.native_libraries
        _rebased_native_libraries =
            rebase_path(invoker.native_libraries, root_build_dir)

        args += [
          "--native-libraries=$_rebased_native_libraries",
          "--abi=$android_app_abi",
        ]
      }
      if (defined(invoker.jar_excluded_patterns)) {
        args += [ "--jar-excluded-globs=${invoker.jar_excluded_patterns}" ]
      }
      if (defined(invoker.jar_included_patterns)) {
        args += [ "--jar-included-globs=${invoker.jar_included_patterns}" ]
      }
      if (defined(invoker.resource_included_patterns)) {
        args += [
          "--resource-included-globs=${invoker.resource_included_patterns}",
        ]
      }
    }
  }

  # Declare an Android library target
  #
  # This target creates an Android library containing java code and Android
  # resources.
  #
  # Supports all variables of java_library(), plus:
  #   deps: In addition to defining java deps, this can also include
  #     android_assets() and android_resources() targets.
  #   alternative_android_sdk_ijar: if set, the given android_sdk_ijar file
  #     replaces the default android_sdk_ijar.
  #   alternative_android_sdk_ijar_dep: the target that generates
  #      alternative_android_sdk_ijar, must be set if alternative_android_sdk_ijar
  #      is used.
  #   alternative_android_sdk_jar: actual jar corresponding to
  #      alternative_android_sdk_ijar, must be set if alternative_android_sdk_ijar
  #      is used.
  #
  # Example
  #   android_library("foo_java") {
  #     sources = [
  #       "android/org/chromium/foo/Foo.java",
  #       "android/org/chromium/foo/FooInterface.java",
  #       "android/org/chromium/foo/FooService.java",
  #     ]
  #     deps = [
  #       ":bar_java"
  #     ]
  #     srcjar_deps = [
  #       ":foo_generated_enum"
  #     ]
  #     jar_excluded_patterns = [
  #       "*/FooService.class", "org/chromium/FooService\$*.class"
  #     ]
  #   }
  template("android_library") {
    java_library(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

      supports_android = true
      requires_android = true

      if (!defined(jar_excluded_patterns)) {
        jar_excluded_patterns = []
      }
      jar_excluded_patterns += [
        "*/R.class",
        "*/R\$*.class",
        "*/Manifest.class",
        "*/Manifest\$*.class",
        "*/GEN_JNI.class",
      ]
    }
  }

  # Declare an Android library target for a prebuilt jar
  #
  # This target creates an Android library containing java code and Android
  # resources.
  #
  # Supports all variables of android_library().
  #
  # Example
  #   android_java_prebuilt("foo_java") {
  #     jar_path = "foo.jar"
  #     deps = [
  #       ":foo_resources",
  #       ":bar_java"
  #     ]
  #   }
  template("android_java_prebuilt") {
    android_library(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    }
  }

  template("android_system_java_prebuilt") {
    java_library_impl(target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      supports_android = true
      type = "system_java_library"
    }
  }

  # Creates org/chromium/build/BuildConfig.java
  # This doesn't really belong in //build since it genates a file for //base.
  # However, we don't currently have a better way to include this file in all
  # apks that depend on //base:base_java.
  #
  # Variables:
  #   use_final_fields: True to use final fields. When false, all other
  #       variables must not be set.
  #   enable_multidex: Value for ENABLE_MULTIDEX.
  #   min_sdk_version: Value for MIN_SDK_VERSION.
  #   bundles_supported: Whether or not this target can be treated as a bundle.
  #   resources_version_variable:
  #   is_incremental_install:
  #   isolated_splits_enabled: Value for ISOLATED_SPLITS_ENABLED.
  template("generate_build_config_srcjar") {
    java_cpp_template(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      sources = [ "//build/android/java/templates/BuildConfig.template" ]
      defines = []

      # Set these even when !use_final_fields so that they have correct default
      # values within junit_binary(), which ignores jar_excluded_patterns.
      if (enable_java_asserts) {
        defines += [ "_ENABLE_ASSERTS" ]
      }
      if (use_cfi_diag || is_ubsan || is_ubsan_security || is_ubsan_vptr) {
        defines += [ "_IS_UBSAN" ]
      }

      if (is_chrome_branded) {
        defines += [ "_IS_CHROME_BRANDED" ]
      }

      if (is_chromecast && chromecast_branding == "internal") {
        defines += [ "_IS_CHROMECAST_BRANDING_INTERNAL" ]
      }

      if (defined(invoker.bundles_supported) && invoker.bundles_supported) {
        defines += [ "_BUNDLES_SUPPORTED" ]
      }

      if (defined(invoker.isolated_splits_enabled) &&
          invoker.isolated_splits_enabled) {
        defines += [ "_ISOLATED_SPLITS_ENABLED" ]
      }

      if (defined(invoker.is_incremental_install) &&
          invoker.is_incremental_install) {
        defines += [ "_IS_INCREMENTAL_INSTALL" ]
      }

      if (invoker.use_final_fields) {
        forward_variables_from(invoker, [ "deps" ])
        defines += [ "USE_FINAL" ]
        if (invoker.enable_multidex) {
          defines += [ "ENABLE_MULTIDEX" ]
        }
        if (defined(invoker.min_sdk_version)) {
          defines += [ "_MIN_SDK_VERSION=${invoker.min_sdk_version}" ]
        }
        if (defined(invoker.resources_version_variable)) {
          defines += [
            "_RESOURCES_VERSION_VARIABLE=${invoker.resources_version_variable}",
          ]
        }
      }
    }
  }

  # Creates ProductConfig.java, a file containing product-specific configuration.
  #
  # Currently, this includes the list of locales, both in their compressed and
  # uncompressed format, as well as library loading
  #
  # Variables:
  #   build_config: Path to build_config used for locale lists.
  #   is_bundle_module: Whether or not this target is part of a bundle build.
  #   java_package: Java package for the generated class.
  #   use_chromium_linker:
  #   use_modern_linker:
  template("generate_product_config_srcjar") {
    java_cpp_template(target_name) {
      defines = []
      _use_final =
          defined(invoker.build_config) ||
          defined(invoker.use_chromium_linker) ||
          defined(invoker.use_modern_linker) || defined(invoker.is_bundle)
      if (_use_final) {
        defines += [ "USE_FINAL" ]
      }

      sources = [ "//build/android/java/templates/ProductConfig.template" ]
      defines += [ "PACKAGE=${invoker.java_package}" ]

      _use_chromium_linker =
          defined(invoker.use_chromium_linker) && invoker.use_chromium_linker
      _use_modern_linker =
          defined(invoker.use_modern_linker) && invoker.use_modern_linker
      _is_bundle = defined(invoker.is_bundle_module) && invoker.is_bundle_module
      defines += [
        "USE_CHROMIUM_LINKER_VALUE=$_use_chromium_linker",
        "USE_MODERN_LINKER_VALUE=$_use_modern_linker",
        "IS_BUNDLE_VALUE=$_is_bundle",
      ]
      if (defined(invoker.build_config)) {
        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
        _rebased_build_config =
            rebase_path(invoker.build_config, root_build_dir)
        defines += [ "LOCALE_LIST=@FileArg($_rebased_build_config:deps_info:locales_java_list)" ]
      }
    }
  }

  # Declare an Android app module target, which is used as the basis for an
  # Android APK or an Android app bundle module.
  #
  # Supports all variables of android_library(), plus:
  #   android_manifest: Path to AndroidManifest.xml. NOTE: This manifest must
  #     not contain a <uses-sdk> element. Use [min|target|max]_sdk_version
  #     instead.
  #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
  #   png_to_webp: If true, pngs (with the exception of 9-patch) are
  #     converted to webp during resource packaging.
  #   loadable_modules: List of paths to native libraries to include. Different
  #     from |shared_libraries| in that:
  #       * dependencies of this .so are not automatically included
  #       * ".cr.so" is never added
  #       * they are not side-loaded when incremental_install=true.
  #       * load_library_from_apk, use_chromium_linker,
  #         and enable_relocation_packing do not apply
  #     Use this instead of shared_libraries when you are going to load the library
  #     conditionally, and only when shared_libraries doesn't work for you.
  #   secondary_abi_loadable_modules: This is the loadable_modules analog to
  #     secondary_abi_shared_libraries.
  #   shared_libraries: List shared_library targets to bundle. If these
  #     libraries depend on other shared_library targets, those dependencies will
  #     also be included in the apk (e.g. for is_component_build).
  #   secondary_abi_shared_libraries: secondary abi shared_library targets to
  #     bundle. If these libraries depend on other shared_library targets, those
  #     dependencies will also be included in the apk (e.g. for is_component_build).
  #   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).
  #   generate_buildconfig_java: If defined and false, skip generating the
  #     BuildConfig java class describing the build configuration. The default
  #     is true when building with Chromium for non-test APKs.
  #   generate_final_jni: If defined and false, skip generating the
  #     GEN_JNI srcjar.
  #   jni_registration_header: If specified, causes the
  #     ${target_name}__final_jni target to additionally output a
  #     header file to this path for use with manual JNI registration.
  #   jni_sources_exclusions: List of source path to exclude from the
  #     final_jni step.
  #   aapt_locale_allowlist: If set, all locales not in this list will be
  #     stripped from resources.arsc.
  #   resource_exclusion_regex: Causes all drawable images matching the regex to
  #     be excluded (mipmaps are still included).
  #   resource_exclusion_exceptions: A list of globs used when
  #     resource_exclusion_regex is set. Files that match this list will
  #     still be included.
  #   resource_values_filter_rules: List of "source_path:name_regex" used to
  #     filter out unwanted values/ resources.
  #   shared_resources: True if this is a runtime shared library APK, like
  #     the system_webview_apk target. Ensures that its resources can be
  #     used by the loading application process.
  #   app_as_shared_lib: True if this is a regular application apk that can
  #     also serve as a runtime shared library, like the monochrome_public_apk
  #     target. Ensures that the resources are usable both by the APK running
  #     as an application, or by another process that loads it at runtime.
  #   shared_resources_allowlist_target: Optional name of a target specifying
  #     an input R.txt file that lists the resources that can be exported
  #     by the APK when shared_resources or app_as_shared_lib is defined.
  #   uncompress_shared_libraries: True if shared libraries should be stored
  #     uncompressed in the APK. Must be unset or true if load_library_from_apk
  #     is set to true.
  #   uncompress_dex: Store final .dex files uncompressed in the apk.
  #   strip_resource_names: True if resource names should be stripped from the
  #     resources.arsc file in the apk or module.
  #   strip_unused_resources: True if unused resources should be stripped from
  #     the apk or module.
  #   short_resource_paths: True if resource paths should be shortened in the
  #     apk or module.
  #   resources_config_paths: List of paths to the aapt2 optimize config files
  #     that tags resources with acceptable/non-acceptable optimizations.
  #   expected_android_manifest: Enables verification of expected merged
  #     manifest based on a golden file.
  #   resource_ids_provider_dep: If passed, this target will use the resource
  #     IDs generated by {resource_ids_provider_dep}__compile_res during
  #     resource compilation.
  #   enforce_resource_overlays_in_tests: Enables check for testonly targets that
  #     dependent resource targets which override another target set
  #     overlay_resources=true. This check is on for non-test targets and
  #     cannot be disabled.
  #   static_library_dependent_targets: A list of scopes describing targets that
  #     use this target as a static library. Common Java code from the targets
  #     listed in static_library_dependent_targets will be moved into this
  #     target. Scope members are name and is_resource_ids_provider.
  #   static_library_provider: Specifies a single target that this target will
  #     use as a static library APK.
  #   static_library_synchronized_proguard: When proguard is enabled, the
  #     static_library_provider target will provide the dex file(s) for this
  #     target.
  #   min_sdk_version: The minimum Android SDK version this target supports.
  #     Optional, default $default_min_sdk_version.
  #   target_sdk_version: The target Android SDK version for this target.
  #     Optional, default to android_sdk_version.
  #   max_sdk_version: The maximum Android SDK version this target supports.
  #     Optional, default not set.
  #   require_native_mocks: Enforce that any native calls using
  #     org.chromium.base.annotations.NativeMethods must have a mock set
  #     (optional).
  #   enable_native_mocks: Allow native calls using
  #     org.chromium.base.annotations.NativeMethods to be mocked in tests
  #     (optional).
  #   product_config_java_packages: Optional list of java packages. If given, a
  #     ProductConfig.java file will be generated for each package.
  #   enable_proguard_checks: Turns on -checkdiscard directives and missing
  #     symbols check in the proguard step (default=true).
  #   disable_r8_outlining: Turn off outlining during the proguard step.
  #   annotation_processor_deps: List of java_annotation_processor targets to
  #     use when compiling the sources given to this target (optional).
  #   processor_args_javac: List of args to pass to annotation processors when
  #     compiling sources given to this target (optional).
  #   bundles_supported: Enable Java code to treat this target as a bundle
  #     whether (by default determined by the target type).
  #   main_component_library: Specifies the name of the base component's library
  #     in a component build. If given, the system will find dependent native
  #     libraries at runtime by inspecting this library (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.
  #   expected_proguard_config: Checks that the merged set of proguard flags
  #     matches the given config.
  #   expected_proguard_config_base: Treat expected_proguard_config as a diff
  #     with this file as the base.
  template("android_apk_or_module") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    assert(defined(invoker.android_manifest))
    _base_path = "$target_out_dir/$target_name/$target_name"
    _build_config = "$target_gen_dir/$target_name.build_config.json"
    _build_config_target = "$target_name$build_config_target_suffix"

    _min_sdk_version = default_min_sdk_version
    _target_sdk_version = android_sdk_version
    if (defined(invoker.min_sdk_version)) {
      _min_sdk_version = invoker.min_sdk_version
    }
    if (defined(invoker.target_sdk_version)) {
      _target_sdk_version = invoker.target_sdk_version
    }

    _template_name = target_name

    _is_bundle_module =
        defined(invoker.is_bundle_module) && invoker.is_bundle_module
    if (_is_bundle_module) {
      _is_base_module =
          defined(invoker.is_base_module) && invoker.is_base_module
    }

    _enable_multidex =
        !defined(invoker.enable_multidex) || invoker.enable_multidex

    if (!_is_bundle_module) {
      _final_apk_path = invoker.final_apk_path
      _final_rtxt_path = "${_final_apk_path}.R.txt"
    }

    _res_size_info_path = "$target_out_dir/$target_name.ap_.info"
    if (!_is_bundle_module) {
      _final_apk_path_no_ext_list =
          process_file_template([ _final_apk_path ],
                                "{{source_dir}}/{{source_name_part}}")
      _final_apk_path_no_ext = _final_apk_path_no_ext_list[0]
      not_needed([ "_final_apk_path_no_ext" ])
    }

    # Non-base bundle modules create only proto resources.
    if (!_is_bundle_module || _is_base_module) {
      _arsc_resources_path = "$target_out_dir/$target_name.ap_"
    }
    if (_is_bundle_module) {
      # Path to the intermediate proto-format resources zip file.
      _proto_resources_path = "$target_out_dir/$target_name.proto.ap_"
    } else {
      # resource_sizes.py needs to be able to find the unpacked resources.arsc
      # file based on apk name to compute normatlized size.
      _resource_sizes_arsc_path =
          "$root_out_dir/arsc/" +
          rebase_path(_final_apk_path_no_ext, root_build_dir) + ".ap_"
    }

    if (defined(invoker.version_code)) {
      _version_code = invoker.version_code
    } else {
      _version_code = android_default_version_code
    }

    if (android_override_version_code != "") {
      _version_code = android_override_version_code
    }

    if (defined(invoker.version_name)) {
      _version_name = invoker.version_name
    } else {
      _version_name = android_default_version_name
    }

    if (android_override_version_name != "") {
      _version_name = android_override_version_name
    }

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

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

    _android_root_manifest_deps = []
    if (defined(invoker.android_manifest_dep)) {
      _android_root_manifest_deps = [ invoker.android_manifest_dep ]
    }
    _android_root_manifest = invoker.android_manifest

    _use_chromium_linker =
        defined(invoker.use_chromium_linker) && invoker.use_chromium_linker
    _use_modern_linker =
        defined(invoker.use_modern_linker) && invoker.use_modern_linker

    _load_library_from_apk =
        defined(invoker.load_library_from_apk) && invoker.load_library_from_apk

    not_needed([
                 "_use_chromium_linker",
                 "_use_modern_linker",
               ])
    assert(!_load_library_from_apk || _use_chromium_linker,
           "load_library_from_apk requires use_chromium_linker")

    # Make sure that uncompress_shared_libraries is set to true if
    # load_library_from_apk is true.
    if (defined(invoker.uncompress_shared_libraries)) {
      _uncompress_shared_libraries = invoker.uncompress_shared_libraries
      assert(!_load_library_from_apk || _uncompress_shared_libraries)
    } else {
      _uncompress_shared_libraries = _load_library_from_apk
    }

    # The dependency that makes the chromium linker, if any is needed.
    _native_libs_deps = []
    _shared_libraries_is_valid =
        defined(invoker.shared_libraries) && invoker.shared_libraries != []

    if (_shared_libraries_is_valid) {
      _native_libs_deps += invoker.shared_libraries

      # Write shared library output files of all dependencies to a file. Those
      # will be the shared libraries packaged into the APK.
      _shared_library_list_file =
          "$target_gen_dir/${_template_name}.native_libs"
      generated_file("${_template_name}__shared_library_list") {
        deps = _native_libs_deps
        outputs = [ _shared_library_list_file ]
        data_keys = [ "shared_libraries" ]
        walk_keys = [ "shared_libraries_barrier" ]
        rebase = root_build_dir
      }
    } else {
      # Must exist for instrumentation_test_apk() to depend on.
      group("${_template_name}__shared_library_list") {
      }
    }

    _secondary_abi_native_libs_deps = []

    if (defined(invoker.secondary_abi_shared_libraries) &&
        invoker.secondary_abi_shared_libraries != []) {
      _secondary_abi_native_libs_deps = invoker.secondary_abi_shared_libraries

      # Write shared library output files of all dependencies to a file. Those
      # will be the shared libraries packaged into the APK.
      _secondary_abi_shared_library_list_file =
          "$target_gen_dir/${_template_name}.secondary_abi_native_libs"
      generated_file("${_template_name}__secondary_abi_shared_library_list") {
        deps = _secondary_abi_native_libs_deps
        outputs = [ _secondary_abi_shared_library_list_file ]
        data_keys = [ "shared_libraries" ]
        walk_keys = [ "shared_libraries_barrier" ]
        rebase = root_build_dir
      }
    } else {
      # Must exist for instrumentation_test_apk() to depend on.
      group("${_template_name}__secondary_abi_shared_library_list") {
      }
    }

    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    assert(_rebased_build_config != "")  # Mark as used.

    _generate_buildconfig_java = !defined(invoker.apk_under_test)
    if (defined(invoker.generate_buildconfig_java)) {
      _generate_buildconfig_java = invoker.generate_buildconfig_java
    }

    _generate_productconfig_java = defined(invoker.product_config_java_packages)

    # JNI generation usually goes hand-in-hand with buildconfig generation.
    _generate_final_jni = _generate_buildconfig_java
    if (defined(invoker.generate_final_jni)) {
      _generate_final_jni = invoker.generate_final_jni
    }

    _proguard_enabled =
        defined(invoker.proguard_enabled) && invoker.proguard_enabled

    if (!_is_bundle_module && _proguard_enabled) {
      _proguard_mapping_path = "$_final_apk_path.mapping"
    }

    # TODO(http://crbug.com/901465): Move shared Java code to static libraries
    # when !_proguard_enabled too.
    _is_static_library_provider =
        defined(invoker.static_library_dependent_targets) && _proguard_enabled
    if (_is_static_library_provider) {
      _static_library_sync_dex_path = "$_base_path.synchronized.r8dex.jar"
      _resource_ids_provider_deps = []
      foreach(_target, invoker.static_library_dependent_targets) {
        if (_target.is_resource_ids_provider) {
          assert(_resource_ids_provider_deps == [],
                 "Can only have 1 resource_ids_provider_dep")
          _resource_ids_provider_deps += [ _target.name ]
        }
      }
      _resource_ids_provider_dep = _resource_ids_provider_deps[0]
    } else if (defined(invoker.resource_ids_provider_dep)) {
      _resource_ids_provider_dep = invoker.resource_ids_provider_dep
    }

    if (_is_static_library_provider) {
      _shared_resources_allowlist_target = _resource_ids_provider_dep
    } else if (defined(invoker.shared_resources_allowlist_target)) {
      _shared_resources_allowlist_target =
          invoker.shared_resources_allowlist_target
    }

    _uses_static_library = defined(invoker.static_library_provider)
    _uses_static_library_synchronized_proguard =
        defined(invoker.static_library_synchronized_proguard) &&
        invoker.static_library_synchronized_proguard

    if (_uses_static_library_synchronized_proguard) {
      assert(_uses_static_library)

      # These will be provided by the static library APK.
      _generate_buildconfig_java = false
      _generate_final_jni = false
    }

    # TODO(crbug.com/864142): Allow incremental installs of bundle modules.
    _incremental_apk = !_is_bundle_module &&
                       !(defined(invoker.never_incremental) &&
                         invoker.never_incremental) && incremental_install
    if (_incremental_apk) {
      _target_dir_name = get_label_info(target_name, "dir")
      _incremental_install_json_path = "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.incremental.json"
      _incremental_apk_path = "${_final_apk_path_no_ext}_incremental.apk"
    }

    if (!_incremental_apk) {
      # Bundle modules don't build the dex here, but need to write this path
      # to their .build_config.json file.
      if (_proguard_enabled) {
        _final_dex_path = "$_base_path.r8dex.jar"
      } else {
        _final_dex_path = "$_base_path.mergeddex.jar"
      }
    }

    _android_manifest =
        "$target_gen_dir/${_template_name}_manifest/AndroidManifest.xml"
    _merge_manifest_target = "${_template_name}__merge_manifests"
    merge_manifests(_merge_manifest_target) {
      forward_variables_from(invoker,
                             [
                               "manifest_package",
                               "max_sdk_version",
                             ])
      input_manifest = _android_root_manifest
      output_manifest = _android_manifest
      build_config = _build_config
      min_sdk_version = _min_sdk_version
      target_sdk_version = _target_sdk_version
      deps = _android_root_manifest_deps + [ ":$_build_config_target" ]
    }

    _final_deps = []

    _enable_main_dex_list = _enable_multidex && _min_sdk_version < 21
    if (_enable_main_dex_list) {
      _generated_proguard_main_dex_config =
          "$_base_path.resources.main-dex-proguard.txt"
    }
    _generated_proguard_config = "$_base_path.resources.proguard.txt"

    if (_generate_buildconfig_java &&
        defined(invoker.product_version_resources_dep)) {
      # Needs to be added as a .build_config.json dep to pick up resources.
      _deps += [ invoker.product_version_resources_dep ]
    }

    if (defined(invoker.alternative_android_sdk_dep)) {
      _android_sdk_dep = invoker.alternative_android_sdk_dep
    } else {
      _android_sdk_dep = "//third_party/android_sdk:android_sdk_java"
    }

    if (defined(_shared_resources_allowlist_target)) {
      _allowlist_gen_dir =
          get_label_info(_shared_resources_allowlist_target, "target_gen_dir")
      _allowlist_target_name =
          get_label_info(_shared_resources_allowlist_target, "name")
      _allowlist_r_txt_path =
          "${_allowlist_gen_dir}/${_allowlist_target_name}" +
          "__compile_resources_R.txt"
      _allowlist_deps =
          "${_shared_resources_allowlist_target}__compile_resources"
    }

    _compile_resources_target = "${_template_name}__compile_resources"
    _compile_resources_rtxt_out =
        "${target_gen_dir}/${_compile_resources_target}_R.txt"
    _compile_resources_emit_ids_out =
        "${target_gen_dir}/${_compile_resources_target}.resource_ids"
    compile_resources(_compile_resources_target) {
      forward_variables_from(invoker,
                             [
                               "aapt_locale_allowlist",
                               "app_as_shared_lib",
                               "enforce_resource_overlays_in_tests",
                               "expected_android_manifest",
                               "expected_android_manifest_base",
                               "extra_verification_manifest",
                               "extra_verification_manifest_dep",
                               "manifest_package",
                               "max_sdk_version",
                               "no_xml_namespaces",
                               "package_id",
                               "package_name",
                               "png_to_webp",
                               "r_java_root_package_name",
                               "resource_exclusion_exceptions",
                               "resource_exclusion_regex",
                               "resource_values_filter_rules",
                               "shared_resources",
                               "shared_resources_allowlist_locales",
                               "uses_split",
                             ])
      android_manifest = _android_manifest
      android_manifest_dep = ":$_merge_manifest_target"
      version_code = _version_code
      version_name = _version_name
      min_sdk_version = _min_sdk_version
      target_sdk_version = _target_sdk_version

      if (defined(expected_android_manifest)) {
        top_target_name = _template_name
      }

      if (defined(_resource_ids_provider_dep)) {
        resource_ids_provider_dep = _resource_ids_provider_dep
      }

      if (defined(invoker.post_process_package_resources_script)) {
        post_process_script = invoker.post_process_package_resources_script
      }
      r_text_out_path = _compile_resources_rtxt_out
      emit_ids_out_path = _compile_resources_emit_ids_out
      size_info_path = _res_size_info_path
      proguard_file = _generated_proguard_config
      if (_enable_main_dex_list) {
        proguard_file_main_dex = _generated_proguard_main_dex_config
      }

      build_config = _build_config
      build_config_dep = ":$_build_config_target"
      android_sdk_dep = _android_sdk_dep
      deps = _deps

      # The static library uses the R.txt files generated by the
      # static_library_dependent_targets when generating the final R.java file.
      if (_is_static_library_provider) {
        foreach(_dep, invoker.static_library_dependent_targets) {
          deps += [ "${_dep.name}__compile_resources" ]
        }
      }

      if (defined(invoker.apk_under_test)) {
        # Set the arsc package name to match the apk_under_test package name
        # So that test resources can references under_test resources via
        # @type/name syntax.
        r_java_root_package_name = "test"
        arsc_package_name =
            "@FileArg($_rebased_build_config:deps_info:arsc_package_name)"

        # Passing in the --emit-ids mapping will cause aapt2 to assign resources
        # IDs that do not conflict with those from apk_under_test.
        assert(!defined(resource_ids_provider_dep))
        resource_ids_provider_dep = invoker.apk_under_test

        include_resource =
            get_label_info(invoker.apk_under_test, "target_out_dir") + "/" +
            get_label_info(invoker.apk_under_test, "name") + ".ap_"
        _link_against = invoker.apk_under_test
      }

      if (_is_bundle_module) {
        is_bundle_module = true
        proto_output = _proto_resources_path

        if (defined(invoker.base_module_target)) {
          include_resource =
              get_label_info(invoker.base_module_target, "target_out_dir") +
              "/" + get_label_info(invoker.base_module_target, "name") + ".ap_"
          _link_against = invoker.base_module_target
        }
      }

      if (defined(_link_against)) {
        deps += [ "${_link_against}__compile_resources" ]
        include_resource = get_label_info(_link_against, "target_out_dir") +
                           "/" + get_label_info(_link_against, "name") + ".ap_"
      }

      # Bundle modules have to reference resources from the base module.
      if (!_is_bundle_module || _is_base_module) {
        arsc_output = _arsc_resources_path
      }

      if (defined(_shared_resources_allowlist_target)) {
        # Used to ensure that the WebView resources are properly shared
        # (i.e. are non-final and with package ID 0).
        shared_resources_allowlist = _allowlist_r_txt_path
        deps += [ _allowlist_deps ]
      }
    }
    _srcjar_deps += [ ":$_compile_resources_target" ]

    # We don't ship apks anymore, only optimize bundle builds
    if (_is_bundle_module) {
      _short_resource_paths =
          defined(invoker.short_resource_paths) &&
          invoker.short_resource_paths && enable_arsc_obfuscation
      _strip_resource_names =
          defined(invoker.strip_resource_names) &&
          invoker.strip_resource_names && enable_arsc_obfuscation
      _strip_unused_resources = defined(invoker.strip_unused_resources) &&
                                invoker.strip_unused_resources
      _optimize_resources = _strip_resource_names || _short_resource_paths ||
                            _strip_unused_resources
    }

    if (_is_bundle_module && _optimize_resources) {
      _optimized_proto_resources_path =
          "$target_out_dir/$target_name.optimized.proto.ap_"
      if (_short_resource_paths) {
        _resources_path_map_out_path =
            "${target_gen_dir}/${_template_name}_resources_path_map.txt"
      }
      _optimize_resources_target = "${_template_name}__optimize_resources"
      optimize_resources(_optimize_resources_target) {
        deps = _deps + [ ":$_compile_resources_target" ]
        short_resource_paths = _short_resource_paths
        strip_resource_names = _strip_resource_names
        if (_short_resource_paths) {
          resources_path_map_out_path = _resources_path_map_out_path
        }
        r_text_path = _compile_resources_rtxt_out
        proto_input_path = _proto_resources_path
        optimized_proto_output = _optimized_proto_resources_path
        if (_strip_unused_resources) {
          # These need to be kept in sync with the target names + output paths
          # in the android_app_bundle template.
          _unused_resources_target = "${_template_name}__unused_resources"
          _unused_resources_config_path =
              "$target_gen_dir/${_template_name}_unused_resources.config"
          resources_config_paths = [ _unused_resources_config_path ]
          deps += [ ":$_unused_resources_target" ]
        } else {
          resources_config_paths = []
        }
        if (defined(invoker.resources_config_paths)) {
          resources_config_paths += invoker.resources_config_paths
        }
      }
    }

    if (!_is_bundle_module) {
      # Output the R.txt file to a more easily discoverable location for
      # archiving. This is necessary when stripping resource names so that we
      # have an archive of resource names to ids for shipped apks (for
      # debugging purposes). We copy the file rather than change the location
      # of the original because other targets rely on the location of the R.txt
      # file.
      _copy_rtxt_target = "${_template_name}__copy_rtxt"
      copy(_copy_rtxt_target) {
        deps = [ ":$_compile_resources_target" ]
        sources = [ _compile_resources_rtxt_out ]
        outputs = [ _final_rtxt_path ]
      }
      _final_deps += [ ":$_copy_rtxt_target" ]
    }

    if (defined(_resource_sizes_arsc_path)) {
      _copy_arsc_target = "${_template_name}__copy_arsc"
      copy(_copy_arsc_target) {
        deps = [ ":$_compile_resources_target" ]

        # resource_sizes.py doesn't care if it gets the optimized .arsc.
        sources = [ _arsc_resources_path ]
        outputs = [ _resource_sizes_arsc_path ]
      }
      _final_deps += [ ":$_copy_arsc_target" ]
    }

    _generate_native_libraries_java =
        (!_is_bundle_module || _is_base_module) &&
        (_native_libs_deps != [] || _secondary_abi_native_libs_deps != []) &&
        !_uses_static_library_synchronized_proguard
    if (_generate_native_libraries_java) {
      write_native_libraries_java("${_template_name}__native_libraries") {
        forward_variables_from(invoker, [ "main_component_library" ])

        # Do not add a dep on the generated_file target in order to avoid having
        # to build the native libraries before this target. The dependency is
        # instead captured via a depfile.
        if (_native_libs_deps != []) {
          native_libraries_list_file = _shared_library_list_file
        } else {
          native_libraries_list_file = _secondary_abi_shared_library_list_file
        }
        enable_chromium_linker = _use_chromium_linker
        load_library_from_apk = _load_library_from_apk
        use_modern_linker = _use_modern_linker
        use_final_fields = true
      }
      _srcjar_deps += [ ":${_template_name}__native_libraries" ]
    }

    _loadable_modules = []
    if (defined(invoker.loadable_modules)) {
      _loadable_modules = invoker.loadable_modules
    }

    if (_native_libs_deps != []) {
      _loadable_modules += _sanitizer_runtimes
    }

    if (_generate_buildconfig_java) {
      generate_build_config_srcjar("${_template_name}__build_config_srcjar") {
        forward_variables_from(invoker,
                               [
                                 "min_sdk_version",
                                 "isolated_splits_enabled",
                               ])
        _bundles_supported = _is_bundle_module || _is_static_library_provider
        if (defined(invoker.bundles_supported)) {
          _bundles_supported = invoker.bundles_supported
        }
        bundles_supported = _bundles_supported
        use_final_fields = true
        enable_multidex = _enable_multidex
        is_incremental_install = _incremental_apk
        if (defined(invoker.product_version_resources_dep)) {
          resources_version_variable =
              "org.chromium.base.R.string.product_version"
        }
        deps = [ ":$_build_config_target" ]
      }
      _srcjar_deps += [ ":${_template_name}__build_config_srcjar" ]
    }

    if (_generate_productconfig_java) {
      foreach(_package, invoker.product_config_java_packages) {
        _locale_target_name =
            "${_template_name}_${_package}__product_config_srcjar"
        generate_product_config_srcjar("$_locale_target_name") {
          forward_variables_from(invoker, [ "is_bundle_module" ])
          build_config = _build_config
          java_package = _package
          use_chromium_linker = _use_chromium_linker
          use_modern_linker = _use_modern_linker
          deps = [ ":$_build_config_target" ]
        }
        _srcjar_deps += [ ":$_locale_target_name" ]
      }
    }

    if (_generate_final_jni) {
      generate_jni_registration("${_template_name}__final_jni") {
        forward_variables_from(invoker,
                               [
                                 "enable_native_mocks",
                                 "require_native_mocks",
                               ])
        if (defined(invoker.bundle_target)) {
          targets = [ invoker.bundle_target ]
        } else {
          targets = [ ":$_template_name" ]
        }
        if (_is_static_library_provider) {
          foreach(_target, invoker.static_library_dependent_targets) {
            targets += [ _target.name ]
          }
        }
        if (defined(invoker.jni_registration_header)) {
          header_output = invoker.jni_registration_header
        }
        if (defined(invoker.jni_sources_exclusions)) {
          sources_exclusions = invoker.jni_sources_exclusions
        }
      }
      _srcjar_deps += [ ":${_template_name}__final_jni" ]
    } else {
      not_needed(invoker,
                 [
                   "enable_native_mocks",
                   "jni_registration_header",
                 ])
    }

    _java_target = "${_template_name}__java"

    java_library_impl(_java_target) {
      forward_variables_from(invoker,
                             [
                               "alternative_android_sdk_dep",
                               "android_manifest",
                               "android_manifest_dep",
                               "annotation_processor_deps",
                               "apk_under_test",
                               "base_module_target",
                               "chromium_code",
                               "jacoco_never_instrument",
                               "jar_excluded_patterns",
                               "javac_args",
                               "native_lib_placeholders",
                               "processor_args_javac",
                               "secondary_abi_loadable_modules",
                               "secondary_native_lib_placeholders",
                               "sources",
                               "static_library_dependent_targets",
                               "use_classic_desugar",
                               "library_always_compress",
                               "library_renames",
                             ])
      deps = _deps
      if (_uses_static_library_synchronized_proguard) {
        if (!defined(jar_excluded_patterns)) {
          jar_excluded_patterns = []
        }

        # The static library will provide all R.java files, but we still need to
        # make the base module R.java files available at compile time since DFM
        # R.java classes extend base module classes.
        jar_excluded_patterns += [
          "*/R.class",
          "*/R\$*.class",
        ]
      }
      if (_is_bundle_module) {
        type = "android_app_bundle_module"
        res_size_info_path = _res_size_info_path
        is_base_module = _is_base_module
        forward_variables_from(invoker,
                               [
                                 "version_code",
                                 "version_name",
                               ])
      } else {
        type = "android_apk"
      }
      r_text_path = _compile_resources_rtxt_out
      main_target_name = _template_name
      supports_android = true
      requires_android = true
      srcjar_deps = _srcjar_deps
      merged_android_manifest = _android_manifest
      if (defined(_final_dex_path)) {
        final_dex_path = _final_dex_path
      }

      if (_is_bundle_module) {
        proto_resources_path = _proto_resources_path
        if (_optimize_resources) {
          proto_resources_path = _optimized_proto_resources_path
          if (_short_resource_paths) {
            module_pathmap_path = _resources_path_map_out_path
          }
        }
      } else {
        apk_path = _final_apk_path
        if (_incremental_apk) {
          incremental_apk_path = _incremental_apk_path
          incremental_install_json_path = _incremental_install_json_path
        }
      }

      proguard_enabled = _proguard_enabled
      if (_proguard_enabled) {
        proguard_configs = [ _generated_proguard_config ]
        if (defined(invoker.proguard_configs)) {
          proguard_configs += invoker.proguard_configs
        }
        if (!enable_java_asserts && (!defined(testonly) || !testonly) &&
            # Injected JaCoCo code causes -checkdiscards to fail.
            !use_jacoco_coverage) {
          proguard_configs += [ "//build/android/dcheck_is_off.flags" ]
        }
        if (!_is_bundle_module) {
          proguard_mapping_path = _proguard_mapping_path
        }
      }

      # Do not add a dep on the generated_file target in order to avoid having
      # to build the native libraries before this target. The dependency is
      # instead captured via a depfile.
      if (_native_libs_deps != []) {
        shared_libraries_runtime_deps_file = _shared_library_list_file
      }
      if (defined(_secondary_abi_shared_library_list_file)) {
        secondary_abi_shared_libraries_runtime_deps_file =
            _secondary_abi_shared_library_list_file
      }

      loadable_modules = _loadable_modules

      uncompress_shared_libraries = _uncompress_shared_libraries

      if (defined(_allowlist_r_txt_path) && _is_bundle_module) {
        # Used to write the file path to the target's .build_config.json only.
        base_allowlist_rtxt_path = _allowlist_r_txt_path
      }
    }

    # TODO(cjhopman): This is only ever needed to calculate the list of tests to
    # run. See build/android/pylib/instrumentation/test_jar.py. We should be
    # able to just do that calculation at build time instead.
    if (defined(invoker.dist_ijar_path)) {
      _dist_ijar_path = invoker.dist_ijar_path
      dist_jar("${_template_name}_dist_ijar") {
        override_build_config = _build_config
        output = _dist_ijar_path
        data = [ _dist_ijar_path ]
        use_interface_jars = true
        deps = [
          ":$_build_config_target",
          ":$_java_target",
        ]
      }
    }

    if (_uses_static_library_synchronized_proguard) {
      _final_dex_target_dep = "${invoker.static_library_provider}__dexsplitter"
    } else if (_is_bundle_module && _proguard_enabled) {
      _final_deps += [ ":$_java_target" ]
    } else if (_incremental_apk) {
      if (defined(invoker.enable_proguard_checks)) {
        not_needed(invoker, [ "enable_proguard_checks" ])
      }
      if (defined(invoker.disable_r8_outlining)) {
        not_needed(invoker, [ "disable_r8_outlining" ])
      }
      if (defined(invoker.dexlayout_profile)) {
        not_needed(invoker, [ "dexlayout_profile" ])
      }
    } else {
      # Dex generation for app bundle modules with proguarding enabled takes
      # place later due to synchronized proguarding.
      _final_dex_target_name = "${_template_name}__final_dex"
      dex(_final_dex_target_name) {
        forward_variables_from(invoker,
                               [
                                 "disable_r8_outlining",
                                 "dexlayout_profile",
                                 "enable_proguard_checks",
                                 "proguard_enable_obfuscation",
                               ])
        min_sdk_version = _min_sdk_version
        proguard_enabled = _proguard_enabled
        build_config = _build_config
        deps = [
          ":$_build_config_target",
          ":$_java_target",
        ]
        if (_proguard_enabled) {
          deps += _deps + [ ":$_compile_resources_target" ]
          proguard_mapping_path = _proguard_mapping_path
          proguard_sourcefile_suffix = "$android_channel-$_version_code"
          has_apk_under_test = defined(invoker.apk_under_test)
        } else if (_min_sdk_version >= default_min_sdk_version) {
          # Enable dex merging only when min_sdk_version is >= what the library
          # .dex files were created with.
          input_dex_filearg =
              "@FileArg(${_rebased_build_config}:final_dex:all_dex_files)"
        } else {
          input_classes_filearg =
              "@FileArg($_rebased_build_config:deps_info:device_classpath)"
        }

        if (_is_static_library_provider) {
          # The list of input jars is already recorded in the .build_config.json, but
          # we need to explicitly add the java deps here to ensure they're
          # available to be used as inputs to the dex step.
          foreach(_dep, invoker.static_library_dependent_targets) {
            _target_label = get_label_info(_dep.name, "label_no_toolchain")
            deps += [ "${_target_label}__java" ]
          }
          output = _static_library_sync_dex_path
          is_static_library = true
        } else {
          output = _final_dex_path
        }
        enable_multidex = _enable_multidex

        # The individual dependencies would have caught real missing deps in
        # their respective dex steps. False positives that were suppressed at
        # per-target dex steps are emitted here since this may use jar files
        # rather than dex files.
        ignore_desugar_missing_deps = true

        if (_enable_main_dex_list) {
          extra_main_dex_proguard_config = _generated_proguard_main_dex_config
          deps += [ ":$_compile_resources_target" ]
        }
      }

      _final_dex_target_dep = ":$_final_dex_target_name"

      # For static libraries, a single Proguard run is performed that includes
      # code from the static library APK and the APKs that use the static
      # library (done via. classpath merging in write_build_config.py).
      # This dexsplitter target splits the synchronized dex output into dex
      # files for each APK/Bundle. In the Bundle case, another dexsplitter step
      # is later performed to split the dex further for each feature module.
      if (_is_static_library_provider && _proguard_enabled) {
        _static_library_modules = []
        foreach(_target, invoker.static_library_dependent_targets) {
          _apk_as_module = _target.name
          _module_config_target = "${_apk_as_module}$build_config_target_suffix"
          _module_gen_dir = get_label_info(_apk_as_module, "target_gen_dir")
          _module_name = get_label_info(_apk_as_module, "name")
          _module_config = "$_module_gen_dir/$_module_name.build_config.json"
          _static_library_modules += [
            {
              name = _module_name
              build_config = _module_config
              build_config_target = _module_config_target
            },
          ]
        }

        _static_library_dexsplitter_target = "${_template_name}__dexsplitter"
        dexsplitter(_static_library_dexsplitter_target) {
          input_dex_zip = _static_library_sync_dex_path
          proguard_mapping = _proguard_mapping_path
          deps = [
            ":$_build_config_target",
            "$_final_dex_target_dep",
          ]
          all_modules = [
                          {
                            name = "base"
                            build_config = _build_config
                            build_config_target = ":$_build_config_target"
                          },
                        ] + _static_library_modules
          feature_jars_args = [
            "--feature-jars",
            "@FileArg($_rebased_build_config:deps_info:" +
                "static_library_dependent_classpath_configs:" +
                "$_rebased_build_config)",
          ]
          foreach(_module, _static_library_modules) {
            _rebased_module_config =
                rebase_path(_module.build_config, root_build_dir)
            feature_jars_args += [
              "--feature-jars",
              "@FileArg($_rebased_build_config:deps_info:" +
                  "static_library_dependent_classpath_configs:" +
                  "$_rebased_module_config)",
            ]
          }
        }
        _final_deps += [ ":$_static_library_dexsplitter_target" ]
        _validate_dex_target = "${_template_name}__validate_dex"
        action_with_pydeps(_validate_dex_target) {
          depfile = "$target_gen_dir/$target_name.d"
          script =
              "//build/android/gyp/validate_static_library_dex_references.py"
          inputs = [ _build_config ]
          _stamp = "$target_gen_dir/$target_name.stamp"
          outputs = [ _stamp ]
          deps = [
            ":$_build_config_target",
            ":$_static_library_dexsplitter_target",
          ]
          args = [
            "--depfile",
            rebase_path(depfile, root_build_dir),
            "--stamp",
            rebase_path(_stamp, root_build_dir),
            "--static-library-dex",
            "@FileArg($_rebased_build_config:final_dex:path)",
          ]
          foreach(_module, _static_library_modules) {
            inputs += [ _module.build_config ]
            _rebased_config = rebase_path(_module.build_config, root_build_dir)
            deps += [ _module.build_config_target ]
            args += [
              "--static-library-dependent-dex",
              "@FileArg($_rebased_config:final_dex:path)",
            ]
          }
        }

        # TODO(crbug.com/1032609): Switch to using R8's support for feature
        # aware ProGuard and get rid of "_validate_dex_target" or figure out
        # why some classes aren't properly being kept.
        # _final_deps += [ ":$_validate_dex_target" ]
        _final_dex_target_dep = ":$_static_library_dexsplitter_target"
      }
    }

    _all_native_libs_deps = _native_libs_deps + _secondary_abi_native_libs_deps
    if (_all_native_libs_deps != []) {
      _native_libs_filearg_dep = ":$_build_config_target"
      _all_native_libs_deps += [ _native_libs_filearg_dep ]

      if (!_is_bundle_module) {
        _native_libs_filearg =
            "@FileArg($_rebased_build_config:native:libraries)"
      }
    }

    if (_is_bundle_module) {
      _final_deps += [
                       ":$_merge_manifest_target",
                       ":$_build_config_target",
                       ":$_compile_resources_target",
                     ] + _all_native_libs_deps
      if (_optimize_resources) {
        _final_deps += [ ":$_optimize_resources_target" ]
      }
      if (defined(_final_dex_target_dep)) {
        not_needed([ "_final_dex_target_dep" ])
      }
    } else {
      # Generate size-info/*.jar.info files.
      if (defined(invoker.name)) {
        # Create size info files for targets that care about size
        # (have proguard enabled).
        _include_size_info =
            defined(invoker.include_size_info) && invoker.include_size_info
        if (_include_size_info || _proguard_enabled) {
          _size_info_target = "${target_name}__size_info"
          create_size_info_files(_size_info_target) {
            name = "${invoker.name}.apk"
            build_config = _build_config
            res_size_info_path = _res_size_info_path
            deps = _deps + [
                     ":$_build_config_target",
                     ":$_compile_resources_target",
                     ":$_java_target",
                   ]
          }
          _final_deps += [ ":$_size_info_target" ]
        } else {
          not_needed(invoker, [ "name" ])
        }
      }

      _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
      }

      if (_incremental_apk) {
        _incremental_compiled_resources_path = "${_base_path}_incremental.ap_"
        _incremental_compile_resources_target_name =
            "${target_name}__compile_incremental_resources"

        action_with_pydeps(_incremental_compile_resources_target_name) {
          deps = [
            ":$_build_config_target",
            ":$_compile_resources_target",
            ":$_merge_manifest_target",
          ]
          script =
              "//build/android/incremental_install/generate_android_manifest.py"
          inputs = [
            _android_manifest,
            _build_config,
            _arsc_resources_path,
          ]
          outputs = [ _incremental_compiled_resources_path ]

          args = [
            "--disable-isolated-processes",
            "--src-manifest",
            rebase_path(_android_manifest, root_build_dir),
            "--in-apk",
            rebase_path(_arsc_resources_path, root_build_dir),
            "--out-apk",
            rebase_path(_incremental_compiled_resources_path, root_build_dir),
            "--aapt2-path",
            rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
            "--android-sdk-jars=@FileArg($_rebased_build_config:android:sdk_jars)",
          ]
        }
      }

      _create_apk_target = "${_template_name}__create"
      _final_deps += [ ":$_create_apk_target" ]
      package_apk("$_create_apk_target") {
        forward_variables_from(invoker,
                               [
                                 "expected_libs_and_assets",
                                 "expected_libs_and_assets_base",
                                 "native_lib_placeholders",
                                 "secondary_abi_loadable_modules",
                                 "secondary_native_lib_placeholders",
                                 "uncompress_dex",
                                 "uncompress_shared_libraries",
                                 "library_always_compress",
                                 "library_renames",
                               ])

        if (defined(expected_libs_and_assets)) {
          build_config_dep = ":$_build_config_target"
          top_target_name = _template_name
        }

        build_config = _build_config
        keystore_name = _keystore_name
        keystore_path = _keystore_path
        keystore_password = _keystore_password
        min_sdk_version = _min_sdk_version
        uncompress_shared_libraries = _uncompress_shared_libraries

        deps = _deps + [ ":$_build_config_target" ]

        if ((!_proguard_enabled || _incremental_apk) &&
            enable_jdk_library_desugaring) {
          _all_jdk_libs = "//build/android:all_jdk_libs"
          deps += [ _all_jdk_libs ]
          jdk_libs_dex = get_label_info(_all_jdk_libs, "target_out_dir") +
                         "/all_jdk_libs.l8.dex"
        }

        if (_incremental_apk) {
          _dex_target = "//build/android/incremental_install:apk_dex"

          deps += [
            ":${_incremental_compile_resources_target_name}",
            _dex_target,
          ]

          dex_path = get_label_info(_dex_target, "target_out_dir") + "/apk.dex"

          # All native libraries are side-loaded, so use a placeholder to force
          # the proper bitness for the app.
          _has_native_libs =
              defined(_native_libs_filearg) || _loadable_modules != []
          if (_has_native_libs && !defined(native_lib_placeholders)) {
            native_lib_placeholders = [ "libfix.crbug.384638.so" ]
          }

          packaged_resources_path = _incremental_compiled_resources_path
          output_apk_path = _incremental_apk_path
        } else {
          loadable_modules = _loadable_modules
          deps += _all_native_libs_deps + [
                    ":$_merge_manifest_target",
                    ":$_compile_resources_target",
                  ]

          if (defined(_final_dex_path)) {
            dex_path = _final_dex_path
            deps += [ _final_dex_target_dep ]
          }

          packaged_resources_path = _arsc_resources_path

          if (defined(_native_libs_filearg)) {
            native_libs_filearg = _native_libs_filearg
            secondary_abi_native_libs_filearg = "@FileArg($_rebased_build_config:native:secondary_abi_libraries)"
          }
          output_apk_path = _final_apk_path
        }
      }
    }

    if (_incremental_apk) {
      _write_installer_json_rule_name = "${_template_name}__incremental_json"
      action_with_pydeps(_write_installer_json_rule_name) {
        script = "//build/android/incremental_install/write_installer_json.py"
        deps = [ ":$_build_config_target" ] + _all_native_libs_deps

        data = [ _incremental_install_json_path ]
        inputs = [ _build_config ]
        outputs = [ _incremental_install_json_path ]

        _rebased_incremental_apk_path =
            rebase_path(_incremental_apk_path, root_build_dir)
        _rebased_incremental_install_json_path =
            rebase_path(_incremental_install_json_path, root_build_dir)
        args = [
          "--apk-path=$_rebased_incremental_apk_path",
          "--output-path=$_rebased_incremental_install_json_path",
          "--dex-file=@FileArg($_rebased_build_config:final_dex:all_dex_files)",
        ]
        if (_proguard_enabled) {
          args += [ "--show-proguard-warning" ]
        }
        if (defined(_native_libs_filearg)) {
          args += [ "--native-libs=$_native_libs_filearg" ]
          deps += [ _native_libs_filearg_dep ]
        }
        if (_loadable_modules != []) {
          _rebased_loadable_modules =
              rebase_path(_loadable_modules, root_build_dir)
          args += [ "--native-libs=$_rebased_loadable_modules" ]
        }
      }
      _final_deps += [
        ":$_java_target",
        ":$_write_installer_json_rule_name",
      ]
    }

    # Generate apk operation related script.
    if (!_is_bundle_module &&
        (!defined(invoker.create_apk_script) || invoker.create_apk_script)) {
      if (_uses_static_library) {
        _install_artifacts_target = "${target_name}__install_artifacts"
        _install_artifacts_json =
            "${target_gen_dir}/${target_name}.install_artifacts"
        generated_file(_install_artifacts_target) {
          output_conversion = "json"
          deps = [ invoker.static_library_provider ]
          outputs = [ _install_artifacts_json ]
          data_keys = [ "install_artifacts" ]
          rebase = root_build_dir
        }
      }
      _apk_operations_target_name = "${target_name}__apk_operations"
      action_with_pydeps(_apk_operations_target_name) {
        _generated_script = "$root_build_dir/bin/${invoker.target_name}"
        script = "//build/android/gyp/create_apk_operations_script.py"
        outputs = [ _generated_script ]
        args = [
          "--script-output-path",
          rebase_path(_generated_script, root_build_dir),
          "--target-cpu=$target_cpu",
        ]
        if (defined(invoker.command_line_flags_file)) {
          args += [
            "--command-line-flags-file",
            invoker.command_line_flags_file,
          ]
        }
        if (_incremental_apk) {
          args += [
            "--incremental-install-json-path",
            rebase_path(_incremental_install_json_path, root_build_dir),
          ]
        } else {
          args += [
            "--apk-path",
            rebase_path(_final_apk_path, root_build_dir),
          ]
        }
        if (_uses_static_library) {
          deps = [ ":$_install_artifacts_target" ]
          _rebased_install_artifacts_json =
              rebase_path(_install_artifacts_json, root_build_dir)
          _static_library_apk_path =
              "@FileArg($_rebased_install_artifacts_json[])"
          args += [
            "--additional-apk",
            _static_library_apk_path,
          ]
        }
        data = []
        data_deps = [
          "//build/android:apk_operations_py",
          "//build/android:stack_tools",
        ]

        if (_proguard_enabled && !_incremental_apk) {
          # Required by logcat command.
          data_deps += [ "//build/android/stacktrace:java_deobfuscate" ]
          data += [ "$_final_apk_path.mapping" ]
          args += [
            "--proguard-mapping-path",
            rebase_path("$_final_apk_path.mapping", root_build_dir),
          ]
        }
      }
      _final_deps += [ ":$_apk_operations_target_name" ]
    }

    _enable_lint = defined(invoker.enable_lint) && invoker.enable_lint &&
                   !disable_android_lint
    if (_enable_lint) {
      android_lint("${target_name}__lint") {
        forward_variables_from(invoker,
                               [
                                 "lint_baseline_file",
                                 "lint_suppressions_file",
                                 "min_sdk_version",
                               ])
        build_config = _build_config
        build_config_dep = ":$_build_config_target"
        deps = [ ":$_java_target" ]
        if (defined(invoker.lint_suppressions_dep)) {
          deps += [ invoker.lint_suppressions_dep ]
        }
        if (defined(invoker.lint_min_sdk_version)) {
          min_sdk_version = invoker.lint_min_sdk_version
        }
      }
    } else {
      not_needed(invoker,
                 [
                   "lint_baseline_file",
                   "lint_min_sdk_version",
                   "lint_suppressions_dep",
                   "lint_suppressions_file",
                 ])
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "assert_no_deps",
                               "data",
                               "data_deps",
                               "metadata",
                             ])

      # Generate apk related operations at runtime.
      public_deps = _final_deps

      if (!defined(data_deps)) {
        data_deps = []
      }

      # Include unstripped native libraries so tests can symbolize stacks.
      data_deps += _all_native_libs_deps

      if (_enable_lint) {
        data_deps += [ ":${target_name}__lint" ]
      }

      if (_uses_static_library) {
        data_deps += [ invoker.static_library_provider ]
      }
    }
  }

  # Declare an Android APK target
  #
  # This target creates an Android APK containing java code, resources, assets,
  # and (possibly) native libraries.
  #
  # Supports all variables of android_apk_or_module(), plus:
  #   apk_name: Name for final apk.
  #   final_apk_path: (Optional) path to output APK.
  #
  # Example
  #   android_apk("foo_apk") {
  #     android_manifest = "AndroidManifest.xml"
  #     sources = [
  #       "android/org/chromium/foo/FooApplication.java",
  #       "android/org/chromium/foo/FooActivity.java",
  #     ]
  #     deps = [
  #       ":foo_support_java"
  #       ":foo_resources"
  #     ]
  #     srcjar_deps = [
  #       ":foo_generated_enum"
  #     ]
  #     shared_libraries = [
  #       ":my_shared_lib",
  #     ]
  #   }
  template("android_apk") {
    # TODO(crbug.com/1042017): Remove.
    not_needed(invoker, [ "no_build_hooks" ])
    android_apk_or_module(target_name) {
      forward_variables_from(invoker,
                             [
                               "aapt_locale_allowlist",
                               "additional_jar_files",
                               "alternative_android_sdk_dep",
                               "android_manifest",
                               "android_manifest_dep",
                               "annotation_processor_deps",
                               "apk_under_test",
                               "app_as_shared_lib",
                               "assert_no_deps",
                               "bundles_supported",
                               "chromium_code",
                               "command_line_flags_file",
                               "create_apk_script",
                               "data",
                               "data_deps",
                               "deps",
                               "dexlayout_profile",
                               "disable_r8_outlining",
                               "dist_ijar_path",
                               "enable_lint",
                               "enable_multidex",
                               "enable_native_mocks",
                               "enable_proguard_checks",
                               "enforce_resource_overlays_in_tests",
                               "expected_android_manifest",
                               "expected_android_manifest_base",
                               "expected_libs_and_assets",
                               "expected_libs_and_assets_base",
                               "generate_buildconfig_java",
                               "generate_final_jni",
                               "include_size_info",
                               "input_jars_paths",
                               "use_modern_linker",
                               "jacoco_never_instrument",
                               "javac_args",
                               "jni_registration_header",
                               "jni_sources_exclusions",
                               "keystore_name",
                               "keystore_password",
                               "keystore_path",
                               "lint_baseline_file",
                               "lint_min_sdk_version",
                               "lint_suppressions_dep",
                               "lint_suppressions_file",
                               "load_library_from_apk",
                               "loadable_modules",
                               "manifest_package",
                               "max_sdk_version",
                               "product_config_java_packages",
                               "main_component_library",
                               "min_sdk_version",
                               "native_lib_placeholders",
                               "never_incremental",
                               "no_xml_namespaces",
                               "png_to_webp",
                               "post_process_package_resources_script",
                               "processor_args_javac",
                               "product_version_resources_dep",
                               "proguard_configs",
                               "proguard_enabled",
                               "proguard_enable_obfuscation",
                               "r_java_root_package_name",
                               "resource_exclusion_exceptions",
                               "resource_exclusion_regex",
                               "resource_ids_provider_dep",
                               "resource_values_filter_rules",
                               "require_native_mocks",
                               "secondary_abi_loadable_modules",
                               "secondary_abi_shared_libraries",
                               "secondary_native_lib_placeholders",
                               "shared_libraries",
                               "shared_resources",
                               "shared_resources_allowlist_locales",
                               "shared_resources_allowlist_target",
                               "sources",
                               "srcjar_deps",
                               "static_library_dependent_targets",
                               "static_library_provider",
                               "static_library_synchronized_proguard",
                               "target_sdk_version",
                               "testonly",
                               "uncompress_dex",
                               "uncompress_shared_libraries",
                               "use_classic_desugar",
                               "library_always_compress",
                               "library_renames",
                               "use_chromium_linker",
                               "version_code",
                               "version_name",
                               "visibility",
                             ])
      is_bundle_module = false
      name = invoker.apk_name
      if (defined(invoker.final_apk_path)) {
        final_apk_path = invoker.final_apk_path
      } else {
        final_apk_path = "$root_build_dir/apks/${invoker.apk_name}.apk"
      }
      metadata = {
        install_artifacts = [ final_apk_path ]
      }
      if (defined(invoker.static_library_provider)) {
        metadata.install_artifacts_barrier = []
      }
    }
  }

  # Declare an Android app bundle module target.
  #
  # The module can be used for an android_apk_or_module().
  #
  # Supports all variables of android_library(), plus:
  #   module_name: Name of the module.
  #   is_base_module: If defined and true, indicates that this is the bundle's
  #     base module (optional).
  #   base_module_target: Base module target of the bundle this module will be
  #     added to (optional). Can only be specified for non-base modules.
  #   bundle_target: Bundle target that this module belongs to (optional).
  #     Can only be specified for base modules.
  template("android_app_bundle_module") {
    _is_base_module = defined(invoker.is_base_module) && invoker.is_base_module

    if (_is_base_module) {
      assert(!defined(invoker.base_module_target))
    } else {
      assert(!defined(invoker.app_as_shared_lib))
      assert(!defined(invoker.shared_resources))
      assert(!defined(invoker.shared_resources_allowlist_target))
      assert(!defined(invoker.shared_resources_allowlist_locales))
      assert(defined(invoker.base_module_target))
      assert(!defined(invoker.bundle_target))
    }

    # TODO(tiborg): We have several flags that are necessary for workarounds
    # that come from the fact that the resources get compiled in the bundle
    # module target, but bundle modules have to have certain flags in
    # common or bundle modules have to know information about the base module.
    # Those flags include version_code, version_name, and base_module_target.
    # It would be better to move the resource compile target into the bundle
    # target. Doing so would keep the bundle modules independent from the bundle
    # and potentially reuse the same bundle modules for multiple bundles.
    android_apk_or_module(target_name) {
      forward_variables_from(invoker,
                             [
                               "aapt_locale_allowlist",
                               "additional_jar_files",
                               "alternative_android_sdk_dep",
                               "android_manifest",
                               "android_manifest_dep",
                               "annotation_processor_deps",
                               "app_as_shared_lib",
                               "assert_no_deps",
                               "base_module_target",
                               "bundle_target",
                               "chromium_code",
                               "data",
                               "data_deps",
                               "deps",
                               "enable_multidex",
                               "expected_android_manifest",
                               "expected_android_manifest_base",
                               "extra_verification_manifest",
                               "extra_verification_manifest_dep",
                               "generate_buildconfig_java",
                               "generate_final_jni",
                               "input_jars_paths",
                               "isolated_splits_enabled",
                               "is_base_module",
                               "jacoco_never_instrument",
                               "jar_excluded_patterns",
                               "javac_args",
                               "jni_registration_header",
                               "jni_sources_exclusions",
                               "load_library_from_apk",
                               "loadable_modules",
                               "product_config_java_packages",
                               "manifest_package",
                               "max_sdk_version",
                               "min_sdk_version",
                               "native_lib_placeholders",
                               "no_xml_namespaces",
                               "package_id",
                               "package_name",
                               "png_to_webp",
                               "processor_args_javac",
                               "product_version_resources_dep",
                               "proguard_configs",
                               "proguard_enabled",
                               "proguard_enable_obfuscation",
                               "resource_exclusion_exceptions",
                               "resource_exclusion_regex",
                               "resource_ids_provider_dep",
                               "resource_values_filter_rules",
                               "resources_config_paths",
                               "secondary_abi_loadable_modules",
                               "secondary_abi_shared_libraries",
                               "secondary_native_lib_placeholders",
                               "shared_libraries",
                               "shared_resources",
                               "shared_resources_allowlist_locales",
                               "shared_resources_allowlist_target",
                               "short_resource_paths",
                               "srcjar_deps",
                               "static_library_provider",
                               "static_library_synchronized_proguard",
                               "strip_resource_names",
                               "strip_unused_resources",
                               "target_sdk_version",
                               "testonly",
                               "uncompress_shared_libraries",
                               "library_always_compress",
                               "library_renames",
                               "use_chromium_linker",
                               "use_modern_linker",
                               "uses_split",
                               "version_code",
                               "version_name",
                               "visibility",
                             ])
      is_bundle_module = true
      generate_buildconfig_java = _is_base_module
    }
  }

  # Declare an Android instrumentation test runner.
  #
  # This target creates a wrapper script to run Android instrumentation tests.
  #
  # Arguments:
  #   android_test_apk: The target containing the tests.
  #   android_test_apk_name: The apk_name in android_test_apk
  #
  #   The following args are optional:
  #   apk_under_test: The target being tested.
  #   additional_apks: Additional targets to install on device.
  #   data: List of runtime data file dependencies.
  #   data_deps: List of non-linked dependencies.
  #   deps: List of private dependencies.
  #   extra_args: Extra arguments set for test runner.
  #   ignore_all_data_deps: Don't build data_deps and additional_apks.
  #   modules: Extra dynamic feature modules to install for test target. Can
  #     only be used if |apk_under_test| is an Android app bundle.
  #   fake_modules: Similar to |modules| but fake installed instead.
  #   never_incremental: Disable incremental builds.
  #   proguard_enabled: Enable proguard
  #   public_deps: List of public dependencies
  #
  # Example
  #   instrumentation_test_runner("foo_test_for_bar") {
  #     android_test_apk: ":foo"
  #     android_test_apk_name: "Foo"
  #     apk_under_test: ":bar"
  #   }
  template("instrumentation_test_runner") {
    if (use_rts) {
      action("${invoker.target_name}__rts_filters") {
        script = "//build/add_rts_filters.py"
        rts_file = "${root_build_dir}/gen/rts/${invoker.target_name}.filter"
        args = [ rebase_path(rts_file, root_build_dir) ]
        outputs = [ rts_file ]
      }
    }
    test_runner_script(target_name) {
      forward_variables_from(invoker,
                             [
                               "additional_apks",
                               "additional_locales",
                               "apk_under_test",
                               "data",
                               "data_deps",
                               "deps",
                               "extra_args",
                               "fake_modules",
                               "ignore_all_data_deps",
                               "modules",
                               "proguard_enabled",
                               "public_deps",
                               "use_webview_provider",
                             ])
      test_name = invoker.target_name
      test_type = "instrumentation"
      _apk_target_name = get_label_info(invoker.android_test_apk, "name")
      apk_target = ":$_apk_target_name"
      test_jar = "$root_build_dir/test.lib.java/" +
                 invoker.android_test_apk_name + ".jar"
      incremental_apk = !(defined(invoker.never_incremental) &&
                          invoker.never_incremental) && incremental_install

      public_deps = [
        ":$_apk_target_name",

        # Required by test runner to enumerate test list.
        ":${_apk_target_name}_dist_ijar",
      ]
      if (defined(invoker.apk_under_test)) {
        public_deps += [ invoker.apk_under_test ]
      }
      if (defined(invoker.additional_apks)) {
        public_deps += invoker.additional_apks
      }
      if (use_rts) {
        if (!defined(data_deps)) {
          data_deps = []
        }
        data_deps += [ ":${invoker.target_name}__rts_filters" ]
      }
    }
  }

  # Declare an Android instrumentation test apk
  #
  # This target creates an Android instrumentation test apk.
  #
  # Supports all variables of android_apk(), plus:
  #   apk_under_test: The apk being tested (optional).
  #
  # Example
  #   android_test_apk("foo_test_apk") {
  #     android_manifest = "AndroidManifest.xml"
  #     apk_name = "FooTest"
  #     apk_under_test = "Foo"
  #     sources = [
  #       "android/org/chromium/foo/FooTestCase.java",
  #       "android/org/chromium/foo/FooExampleTest.java",
  #     ]
  #     deps = [
  #       ":foo_test_support_java"
  #     ]
  #   }
  template("android_test_apk") {
    android_apk(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      testonly = true

      # The size info enables the test_runner to find the source file location
      # of a test after it is ran.
      include_size_info = true
      data = [ "$root_build_dir/size-info/${invoker.apk_name}.apk.jar.info" ]
      if (defined(invoker.data)) {
        data += invoker.data
      }

      deps = [ "//testing/android/broker:broker_java" ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      data_deps = [
        # Ensure unstripped libraries are included in runtime deps so that
        # symbolization can be done.
        ":${target_name}__secondary_abi_shared_library_list",
        ":${target_name}__shared_library_list",
      ]
      if (defined(invoker.data_deps)) {
        data_deps += invoker.data_deps
      }
      if (defined(invoker.apk_under_test)) {
        data_deps += [ invoker.apk_under_test ]
      } else {
        enable_native_mocks = true
      }

      if (defined(invoker.apk_under_test)) {
        _under_test_label =
            get_label_info(invoker.apk_under_test, "label_no_toolchain")
        data_deps += [
          "${_under_test_label}__secondary_abi_shared_library_list",
          "${_under_test_label}__shared_library_list",
        ]
      }

      if (defined(invoker.additional_apks)) {
        data_deps += invoker.additional_apks
      }
      if (defined(invoker.use_webview_provider)) {
        data_deps += [ invoker.use_webview_provider ]
      }

      if (defined(invoker.proguard_enabled) && invoker.proguard_enabled &&
          !incremental_install) {
        # When ProGuard is on, we use ProGuard to combine the under test java
        # code and the test java code. This is to allow us to apply all ProGuard
        # optimizations that we ship with, but not have them break tests. The
        # apk under test will still have the same resources, assets, and
        # manifest, all of which are the ones used in the tests.
        proguard_configs = [ "//testing/android/proguard_for_test.flags" ]
        if (defined(invoker.proguard_configs)) {
          proguard_configs += invoker.proguard_configs
        }
        enable_proguard_checks = false
        if (defined(invoker.final_apk_path)) {
          _final_apk_path = final_apk_path
        } else {
          _final_apk_path = "$root_build_dir/apks/${invoker.apk_name}.apk"
        }
        data += [ "$_final_apk_path.mapping" ]
      }

      dist_ijar_path = "$root_build_dir/test.lib.java/${invoker.apk_name}.jar"
      create_apk_script = false

      forward_variables_from(invoker,
                             "*",
                             TESTONLY_AND_VISIBILITY + [
                                   "data",
                                   "data_deps",
                                   "deps",
                                   "proguard_configs",
                                 ])
    }
  }

  # Declare an Android instrumentation test apk with wrapper script.
  #
  # This target creates an Android instrumentation test apk with wrapper script
  # to run the test.
  #
  # Supports all variables of android_test_apk.
  template("instrumentation_test_apk") {
    assert(defined(invoker.apk_name))
    _apk_target_name = "${target_name}__test_apk"
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    android_test_apk(_apk_target_name) {
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
    }
    instrumentation_test_runner(target_name) {
      forward_variables_from(invoker,
                             [
                               "additional_apks",
                               "apk_under_test",
                               "data",
                               "data_deps",
                               "deps",
                               "extra_args",
                               "ignore_all_data_deps",
                               "modules",
                               "never_incremental",
                               "proguard_enabled",
                               "proguard_enable_obfuscation",
                               "public_deps",
                               "use_webview_provider",
                             ])
      android_test_apk = ":${_apk_target_name}"
      android_test_apk_name = invoker.apk_name
    }
  }

  # Declare an Android gtest apk
  #
  # This target creates an Android apk for running gtest-based unittests.
  #
  # Variables
  #   deps: Specifies the dependencies of this target. These will be passed to
  #     the underlying android_apk invocation and should include the java and
  #     resource dependencies of the apk.
  #   shared_library: shared_library target that contains the unit tests.
  #   apk_name: The name of the produced apk. If unspecified, it uses the name
  #             of the shared_library target suffixed with "_apk".
  #   use_default_launcher: Whether the default activity (NativeUnitTestActivity)
  #     should be used for launching tests.
  #   allow_cleartext_traffic: (Optional) Whether to allow cleartext network
  #     requests during the test.
  #   use_native_activity: Test implements ANativeActivity_onCreate().
  #
  # Example
  #   unittest_apk("foo_unittests_apk") {
  #     deps = [ ":foo_java", ":foo_resources" ]
  #     shared_library = ":foo_unittests"
  #   }
  template("unittest_apk") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
    _use_native_activity =
        defined(invoker.use_native_activity) && invoker.use_native_activity
    _android_manifest = "$target_gen_dir/$target_name/AndroidManifest.xml"
    assert(invoker.shared_library != "")

    # This trivial assert is needed in case android_manifest is defined,
    # as otherwise _use_native_activity and _android_manifest would not be used.
    assert(_use_native_activity != "" && _android_manifest != "")

    if (!defined(invoker.android_manifest)) {
      _allow_cleartext_traffic = defined(invoker.allow_cleartext_traffic) &&
                                 invoker.allow_cleartext_traffic
      jinja_template("${target_name}_manifest") {
        _native_library_name = get_label_info(invoker.shared_library, "name")
        if (defined(invoker.android_manifest_template)) {
          input = invoker.android_manifest_template
        } else {
          input =
              "//testing/android/native_test/java/AndroidManifest.xml.jinja2"
        }
        output = _android_manifest
        variables = [
          "is_component_build=${is_component_build}",
          "native_library_name=${_native_library_name}",
          "use_native_activity=${_use_native_activity}",
          "allow_cleartext_traffic=${_allow_cleartext_traffic}",
        ]
      }
    }

    android_apk(target_name) {
      data_deps = []
      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
      testonly = true
      create_apk_script = false
      enable_native_mocks = true

      # TODO(crbug.com/1099849): Figure out why angle tests fail to launch
      #     with newer target_sdk_version.
      if (!defined(invoker.target_sdk_version) && _use_native_activity) {
        target_sdk_version = 24
      }

      assert(!defined(invoker.proguard_enabled) || !invoker.proguard_enabled ||
             invoker.proguard_configs != [])

      if (!defined(apk_name)) {
        apk_name = get_label_info(invoker.shared_library, "name")
      }

      if (!defined(android_manifest)) {
        android_manifest_dep = ":${target_name}_manifest"
        android_manifest = _android_manifest
      }

      final_apk_path = "$root_build_dir/${apk_name}_apk/${apk_name}-debug.apk"

      if (!defined(use_default_launcher) || use_default_launcher) {
        deps += [
          "//base:base_java",
          "//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
          "//testing/android/native_test:native_test_java",
        ]
      }
      shared_libraries = [ invoker.shared_library ]
      deps += [
        ":${target_name}__secondary_abi_shared_library_list",
        ":${target_name}__shared_library_list",
      ]
    }
  }

  # Generate .java files from .aidl files.
  #
  # This target will store the .java files in a srcjar and should be included in
  # an android_library or android_apk's srcjar_deps.
  #
  # Variables
  #   sources: Paths to .aidl files to compile.
  #   import_include: Path to directory containing .java files imported by the
  #     .aidl files.
  #   interface_file: Preprocessed aidl file to import.
  #
  # Example
  #   android_aidl("foo_aidl") {
  #     import_include = "java/src"
  #     sources = [
  #       "java/src/com/foo/bar/FooBarService.aidl",
  #       "java/src/com/foo/bar/FooBarServiceCallback.aidl",
  #     ]
  #   }
  template("android_aidl") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

      script = "//build/android/gyp/aidl.py"
      depfile = "$target_gen_dir/$target_name.d"
      sources = invoker.sources

      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
      _aidl_path = "${android_sdk_build_tools}/aidl"
      _framework_aidl = "$android_sdk/framework.aidl"
      _imports = [ _framework_aidl ]
      if (defined(invoker.interface_file)) {
        assert(invoker.interface_file != "")
        _imports += [ invoker.interface_file ]
      }

      inputs = [ _aidl_path ] + _imports

      outputs = [ _srcjar_path ]
      _rebased_imports = rebase_path(_imports, root_build_dir)
      args = [
        "--aidl-path",
        rebase_path(_aidl_path, root_build_dir),
        "--imports=$_rebased_imports",
        "--srcjar",
        rebase_path(_srcjar_path, root_build_dir),
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]
      if (defined(invoker.import_include) && invoker.import_include != []) {
        _rebased_import_paths = []
        foreach(_import_path, invoker.import_include) {
          _rebased_import_path = []
          _rebased_import_path = [ rebase_path(_import_path, root_build_dir) ]
          _rebased_import_paths += _rebased_import_path
        }
        args += [ "--includes=$_rebased_import_paths" ]
      }
      args += rebase_path(sources, root_build_dir)
    }
  }

  # Compile a protocol buffer to java.
  #
  # This generates java files from protocol buffers and creates an Android library
  # containing the classes.
  #
  # Variables
  #   sources (required)
  #       Paths to .proto files to compile.
  #
  #   proto_path (required)
  #       Root directory of .proto files.
  #
  #   deps (optional)
  #       Additional dependencies. Passed through to both the action and the
  #       android_library targets.
  #
  #   import_dirs (optional)
  #       A list of extra import directories to be passed to protoc compiler.
  #       WARNING: This circumvents proto checkdeps, and should only be used
  #       when needed, typically when proto files cannot cleanly import through
  #       absolute paths, such as for third_party or generated .proto files.
  #       http://crbug.com/691451 tracks fixing this.
  #
  # Example:
  #  proto_java_library("foo_proto_java") {
  #    proto_path = "src/foo"
  #    sources = [ "$proto_path/foo.proto" ]
  #  }
  template("proto_java_library") {
    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)

    _template_name = target_name

    action_with_pydeps("${_template_name}__protoc_java") {
      # The suffix "__protoc_java.srcjar" is used by SuperSize to identify
      # protobuf symbols.
      _srcjar_path = "$target_gen_dir/$target_name.srcjar"
      script = "//build/protoc_java.py"

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

      sources = invoker.sources
      depfile = "$target_gen_dir/$target_name.d"
      outputs = [ _srcjar_path ]
      args = [
               "--depfile",
               rebase_path(depfile, root_build_dir),
               "--protoc",
               rebase_path(android_protoc_bin, root_build_dir),
               "--proto-path",
               rebase_path(invoker.proto_path, root_build_dir),
               "--srcjar",
               rebase_path(_srcjar_path, root_build_dir),
             ] + rebase_path(sources, root_build_dir)

      if (defined(invoker.import_dirs)) {
        foreach(_import_dir, invoker.import_dirs) {
          args += [
            "--import-dir",
            rebase_path(_import_dir, root_build_dir),
          ]
        }
      }
    }

    android_library(target_name) {
      chromium_code = false
      sources = []
      srcjar_deps = [ ":${_template_name}__protoc_java" ]
      deps = [ "//third_party/android_deps:protobuf_lite_runtime_java" ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
    }
  }

  # Declare an Android library target for a prebuilt AAR.
  #
  # This target creates an Android library containing java code and Android
  # resources. For libraries without resources, it will not generate
  # corresponding android_resources targets.
  #
  # To avoid slowing down "gn gen", an associated .info file must be committed
  # along with the .aar file. In order to create this file, define the target
  # and then run once with the gn arg "update_android_aar_prebuilts = true".
  #
  # Variables
  #   aar_path: Path to the AAR.
  #   info_path: Path to the .aar.info file (generated via
  #       update_android_aar_prebuilts GN arg).
  #   proguard_configs: List of proguard configs to use in final apk step for
  #       any apk that depends on this library.
  #   ignore_aidl: Whether to ignore .aidl files found with the .aar.
  #   ignore_assets: Whether to ignore assets found in the .aar.
  #   ignore_manifest: Whether to ignore creating manifest.
  #   ignore_native_libraries: Whether to ignore .so files found in the .aar.
  #       See also extract_native_libraries.
  #   ignore_proguard_configs: Whether to ignore proguard configs.
  #   ignore_info_updates: Whether to ignore the info file when
  #       update_android_aar_prebuilts gn arg is true. However, the info file
  #       will still be verified regardless of the value of this flag.
  #   strip_resources: Whether to ignore android resources found in the .aar.
  #   custom_package: Java package for generated R.java files.
  #   extract_native_libraries: Whether to extract .so files found in the .aar.
  #       If the file contains .so, either extract_native_libraries or
  #       ignore_native_libraries must be set.
  #   TODO(jbudorick@): remove this arguments after crbug.com/522043 is fixed.
  #   requires_android: Whether this target can only be used for compiling
  #       Android related targets.
  #
  # Example
  #   android_aar_prebuilt("foo_java") {
  #     aar_path = "foo.aar"
  #   }
  template("android_aar_prebuilt") {
    _info_path = "$target_name.info"
    if (defined(invoker.info_path)) {
      _info_path = invoker.info_path
    }
    _output_path = "${target_out_dir}/${target_name}"

    # Some targets only differ by _java with other targets so _java and _junit
    # need to be replaced by non-empty strings to avoid duplicate targets. (e.g.
    # androidx_window_window_java vs androidx_window_window_java_java).
    _target_name_without_java_or_junit =
        string_replace(string_replace(target_name, "_java", "_J"),
                       "_junit",
                       "_U")

    # This unpack target is a python action, not a valid java target. Since the
    # java targets below depend on it, its name must not match the java patterns
    # in internal_rules.gni.
    _unpack_target_name = "${_target_name_without_java_or_junit}__unpack_aar"
    _ignore_aidl = defined(invoker.ignore_aidl) && invoker.ignore_aidl
    _ignore_assets = defined(invoker.ignore_assets) && invoker.ignore_assets
    _ignore_manifest =
        defined(invoker.ignore_manifest) && invoker.ignore_manifest
    _ignore_native_libraries = defined(invoker.ignore_native_libraries) &&
                               invoker.ignore_native_libraries
    _ignore_proguard_configs = defined(invoker.ignore_proguard_configs) &&
                               invoker.ignore_proguard_configs
    _extract_native_libraries = defined(invoker.extract_native_libraries) &&
                                invoker.extract_native_libraries
    _strip_resources =
        defined(invoker.strip_resources) && invoker.strip_resources

    # Allow 'resource_overlay' parameter even if there are no resources in order
    # to keep the logic for generated 'android_aar_prebuilt' rules simple.
    not_needed(invoker, [ "resource_overlay" ])

    _ignore_info_updates =
        defined(invoker.ignore_info_updates) && invoker.ignore_info_updates

    # Scan the AAR file and determine the resources and jar files.
    # Some libraries might not have resources; others might have two jars.
    if (!_ignore_info_updates && update_android_aar_prebuilts) {
      print("Writing " + rebase_path(_info_path, "//"))
      exec_script("//build/android/gyp/aar.py",
                  [
                    "list",
                    rebase_path(invoker.aar_path, root_build_dir),
                    "--output",
                    rebase_path(_info_path, root_build_dir),
                  ])
    }

    # If "gn gen" is failing on the following line, you need to generate an
    # .info file for your new target by running:
    #   gn gen --args='target_os="android" update_android_aar_prebuilts=true' out/tmp
    #   rm -r out/tmp
    _scanned_files = read_file(_info_path, "scope")

    _use_scanned_assets = !_ignore_assets && _scanned_files.assets != []

    assert(_ignore_aidl || _scanned_files.aidl == [],
           "android_aar_prebuilt() aidl not yet supported." +
               " Implement or use ignore_aidl = true." +
               " http://crbug.com/644439")
    assert(
        !_scanned_files.has_native_libraries ||
            (_ignore_native_libraries || _extract_native_libraries),
        "android_aar_prebuilt() contains .so files." +
            " Please set ignore_native_libraries or extract_native_libraries.")
    assert(
        !(_ignore_native_libraries && _extract_native_libraries),
        "ignore_native_libraries and extract_native_libraries cannot both be set.")
    assert(!_scanned_files.has_native_libraries ||
           _scanned_files.native_libraries != [])
    assert(_scanned_files.has_classes_jar || _scanned_files.subjars == [])

    action_with_pydeps(_unpack_target_name) {
      script = "//build/android/gyp/aar.py"  # Unzips the AAR
      args = [
        "extract",
        rebase_path(invoker.aar_path, root_build_dir),
        "--output-dir",
        rebase_path(_output_path, root_build_dir),
        "--assert-info-file",
        rebase_path(_info_path, root_build_dir),
      ]
      if (_strip_resources) {
        args += [ "--ignore-resources" ]
      }
      inputs = [ invoker.aar_path ]
      outputs = [ "${_output_path}/AndroidManifest.xml" ]
      if (!_strip_resources && _scanned_files.has_r_text_file) {
        # Certain packages, in particular Play Services have no R.txt even
        # though its presence is mandated by AAR spec. Such packages cause
        # spurious rebuilds if this output is specified unconditionally.
        outputs += [ "${_output_path}/R.txt" ]
      }

      if (!_strip_resources && _scanned_files.resources != []) {
        outputs += get_path_info(
                rebase_path(_scanned_files.resources, "", _output_path),
                "abspath")
      }
      if (_scanned_files.has_classes_jar) {
        outputs += [ "${_output_path}/classes.jar" ]
      }
      outputs +=
          get_path_info(rebase_path(_scanned_files.subjars, "", _output_path),
                        "abspath")
      if (!_ignore_proguard_configs) {
        if (_scanned_files.has_proguard_flags) {
          outputs += [ "${_output_path}/proguard.txt" ]
        }
      }

      if (_extract_native_libraries && _scanned_files.has_native_libraries) {
        outputs += get_path_info(
                rebase_path(_scanned_files.native_libraries, "", _output_path),
                "abspath")
      }
      if (_use_scanned_assets) {
        outputs +=
            get_path_info(rebase_path(_scanned_files.assets, "", _output_path),
                          "abspath")
      }
    }

    _has_unignored_resources =
        !_strip_resources &&
        (_scanned_files.resources != [] || _scanned_files.has_r_text_file)

    _should_process_manifest =
        !_ignore_manifest && !_scanned_files.is_manifest_empty

    # Create the android_resources target for resources.
    if (_has_unignored_resources || _should_process_manifest) {
      _res_target_name = "${target_name}__resources"
      android_resources(_res_target_name) {
        forward_variables_from(invoker,
                               [
                                 "custom_package",
                                 "resource_overlay",
                                 "testonly",
                                 "strip_drawables",
                               ])
        deps = [ ":$_unpack_target_name" ]
        if (_should_process_manifest) {
          android_manifest_dep = ":$_unpack_target_name"
          android_manifest = "${_output_path}/AndroidManifest.xml"
        } else if (defined(_scanned_files.manifest_package) &&
                   !defined(custom_package)) {
          custom_package = _scanned_files.manifest_package
        }

        sources = []
        if (!_strip_resources) {
          sources = rebase_path(_scanned_files.resources, "", _output_path)
        }
        if (!_strip_resources && _scanned_files.has_r_text_file) {
          r_text_file = "${_output_path}/R.txt"
        }
      }
    } else if (defined(invoker.strip_drawables)) {
      not_needed(invoker, [ "strip_drawables" ])
    }

    if (_ignore_manifest) {
      # Having this available can be useful for DFMs that depend on AARs. It
      # provides a way to have manifest entries go into the base split while
      # the code goes into a DFM.
      java_group("${target_name}__ignored_manifest") {
        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
        deps = [ ":$_unpack_target_name" ]
        mergeable_android_manifests = [ "${_output_path}/AndroidManifest.xml" ]
      }
    }

    # Create the android_assets target for assets
    if (_use_scanned_assets) {
      _assets_target_name = "${target_name}__assets"
      android_assets(_assets_target_name) {
        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
        renaming_sources = []
        renaming_destinations = []
        foreach(_asset_file, _scanned_files.assets) {
          _original_path =
              get_path_info(rebase_path(_asset_file, "", _output_path),
                            "abspath")
          _updated_path = string_replace(_asset_file, "assets/", "", 1)
          renaming_sources += [ _original_path ]
          renaming_destinations += [ _updated_path ]
        }
      }
    }

    # Create android_java_prebuilt target for classes.jar.
    if (_scanned_files.has_classes_jar) {
      _java_library_vars = [
        "bytecode_rewriter_target",
        "enable_bytecode_checks",
        "jar_excluded_patterns",
        "jar_included_patterns",
        "missing_classes_allowlist",
        "requires_android",
        "testonly",
        "use_classic_desugar",
      ]

      # Create android_java_prebuilt target for extra jars within jars/.
      _subjar_targets = []
      foreach(_tuple, _scanned_files.subjar_tuples) {
        _current_target = "${target_name}__subjar_${_tuple[0]}"
        _subjar_targets += [ ":$_current_target" ]
        java_prebuilt(_current_target) {
          forward_variables_from(invoker, _java_library_vars)
          deps = [ ":$_unpack_target_name" ]
          if (!defined(requires_android)) {
            requires_android = true
          }
          supports_android = true
          jar_path = "$_output_path/${_tuple[1]}"
          _base_output_name = get_path_info(jar_path, "name")
          output_name = "${invoker.target_name}-$_base_output_name"
          public_target_label = invoker.target_name
        }
      }

      _jar_target_name = "${target_name}__classes"
      java_prebuilt(_jar_target_name) {
        forward_variables_from(invoker, _java_library_vars)
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "input_jars_paths",
                                 "proguard_configs",
                               ])
        if (!defined(deps)) {
          deps = []
        }
        deps += _subjar_targets + [ ":$_unpack_target_name" ]
        if (defined(_res_target_name)) {
          deps += [ ":$_res_target_name" ]
        }
        if (!defined(requires_android)) {
          requires_android = true
        }
        supports_android = true
        jar_path = "$_output_path/classes.jar"
        aar_path = invoker.aar_path
        output_name = invoker.target_name

        if (!_ignore_proguard_configs) {
          if (!defined(proguard_configs)) {
            proguard_configs = []
          }
          if (_scanned_files.has_proguard_flags) {
            proguard_configs += [ "$_output_path/proguard.txt" ]
          }
        }
        public_target_label = invoker.target_name
      }
    }

    java_group(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
      public_deps = [ ":$_unpack_target_name" ]
      deps = []
      if (defined(_jar_target_name)) {
        deps += [ ":$_jar_target_name" ]

        # Although subjars are meant to be private, we add them as deps here
        # because in practice they seem to contain classes required to be in the
        # classpath.
        deps += _subjar_targets
      }
      if (defined(_res_target_name)) {
        deps += [ ":$_res_target_name" ]
      }
      if (defined(_assets_target_name)) {
        deps += [ ":$_assets_target_name" ]
      }
    }
  }

  # Create an Android application bundle from one base android_apk target,
  # and zero or more associated android_apk.
  #
  # Variables:
  #    base_module_target: Name of the android_app_bundle_module target
  #      corresponding to the base module for this application bundle. The
  #      bundle file will include the same content in its base module, though in
  #      a slightly different format.
  #
  #    bundle_base_path: Optional. If set, the bundle will be output to this
  #      directory. Defaults to "$root_build_dir/apks".
  #
  #    bundle_name: Optional. If set, the bundle will be output to the
  #      filename "${bundle_name}.aab".
  #
  #    extra_modules: Optional list of scopes, one per extra module used by
  #      this bundle. Each scope must have a 'name' field that specifies the
  #      module name (which cannot be 'base', since this is reserved for the
  #      base module), and an 'apk_target' field that specified the
  #      corresponding android_apk target name the module is modeled on.
  #
  #    enable_language_splits: Optional. If true, enable APK splits based
  #      on languages.
  #
  #    keystore_path: optional keystore path, used only when generating APKs.
  #    keystore_name: optional keystore name, used only when generating APKs.
  #    keystore_password: optional keystore password, used only when
  #      generating APKs.
  #
  #    command_line_flags_file: Optional. If provided, named of the on-device
  #      file that will be used to store command-line arguments. The default
  #      is 'command_line_flags_file', but this is typically redefined to
  #      something more specific for certain bundles (e.g. the Chromium based
  #      APKs use 'chrome-command-line', the WebView one uses
  #      'webview-command-line').
  #
  #    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.
  #
  #    proguard_enable_obfuscation: Whether to enable obfuscation (default=true)
  #
  #    enable_multidex: Optional. Enable multidexing of optimized modules jars
  #      when using synchronized proguarding. Only applies to base module.
  #
  #    proguard_android_sdk_dep: Optional. android_system_java_prebuilt() target
  #      used as a library jar for synchronized proguarding.
  #
  #    compress_shared_libraries: Optional. Whether to compress shared libraries
  #      such that they are extracted upon install. Libraries prefixed with
  #      "crazy." are never compressed.
  #
  #    system_image_locale_allowlist: List of locales that should be included
  #      on system APKs generated from this bundle.
  #
  #    static_library_provider: Specifies a single target that this target will
  #      use as a static library APK.
  #      Additionally, when allotting libraries to be packaged into modules, the
  #      libraries packaged into the static library will be accounted for to
  #      avoid library duplication. Effectively, the static library will be
  #      treated as the parent of the base module.
  #
  #    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.
  #    expected_proguard_config: Checks that the merged set of proguard flags
  #      matches the given config.
  #    expected_proguard_config_base: Treat expected_proguard_config as a diff
  #      with this file as the base.
  #
  #    version_code: Optional. Version code of the target.
  #
  #    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.
  #
  #    default_modules_for_testing: (optional): A list of DFM that the wrapper
  #      script should install. This is for local testing only, and does not
  #      affect the actual DFM in production.
  # Example:
  #   android_app_bundle("chrome_public_bundle") {
  #      base_module_target = "//chrome/android:chrome_public_apk"
  #      extra_modules = [
  #        { # NOTE: Scopes require one field per line, and no comma separators.
  #          name = "my_module"
  #          module_target = ":my_module"
  #        },
  #      ]
  #   }
  #
  template("android_app_bundle") {
    _target_name = target_name
    _uses_static_library = defined(invoker.static_library_provider)
    _proguard_enabled =
        defined(invoker.proguard_enabled) && invoker.proguard_enabled

    if (defined(invoker.version_code)) {
      _version_code = invoker.version_code
    } else {
      _version_code = android_default_version_code
    }

    if (android_override_version_code != "") {
      _version_code = android_override_version_code
    }

    # Prevent "unused variable".
    not_needed([ "_version_code" ])

    _bundle_base_path = "$root_build_dir/apks"
    if (defined(invoker.bundle_base_path)) {
      _bundle_base_path = invoker.bundle_base_path
    }

    _bundle_name = _target_name
    if (defined(invoker.bundle_name)) {
      _bundle_name = invoker.bundle_name
    }
    _bundle_path = "$_bundle_base_path/${_bundle_name}.aab"
    _rebased_bundle_path = rebase_path(_bundle_path, root_build_dir)

    _base_target_name = get_label_info(invoker.base_module_target, "name")
    _base_target_gen_dir =
        get_label_info(invoker.base_module_target, "target_gen_dir")
    _base_module_build_config =
        "$_base_target_gen_dir/${_base_target_name}.build_config.json"
    _base_module_build_config_target =
        "${invoker.base_module_target}$build_config_target_suffix"
    _rebased_base_module_build_config =
        rebase_path(_base_module_build_config, root_build_dir)

    _modules = [
      {
        name = "base"
        module_target = invoker.base_module_target
        build_config = _base_module_build_config
        build_config_target = _base_module_build_config_target
        if (_uses_static_library) {
          parent = "lib"
        }
      },
    ]

    _enable_multidex =
        !defined(invoker.enable_multidex) || invoker.enable_multidex

    # Prevent "unused variable".
    not_needed([ "_enable_multidex" ])

    if (_proguard_enabled) {
      _uses_static_library_synchronized_proguard =
          defined(invoker.static_library_synchronized_proguard) &&
          invoker.static_library_synchronized_proguard

      # TODO(crbug.com/1032609): Remove dexsplitter from Trichrome Proguard.
      _dex_target = "${_target_name}__dex"
      _proguard_mapping_path = "${_bundle_path}.mapping"
    }

    assert(_proguard_enabled || !defined(invoker.enable_multidex),
           "Bundle only adds dexing step if proguarding is enabled.")

    if (defined(invoker.extra_modules)) {
      _module_count = 0
      not_needed([ "_module_count" ])

      foreach(_module, invoker.extra_modules) {
        _module_count += 1
        assert(defined(_module.name),
               "Missing 'name' field for extra module #${_module_count}.")
        assert(_module.name != "base",
               "Module name 'base' is reserved for the main bundle module")
        assert(
            defined(_module.module_target),
            "Missing 'module_target' field for extra module ${_module.name}.")
        _module_target = _module.module_target
        _module_target_name = get_label_info(_module_target, "name")
        _module_target_gen_dir =
            get_label_info(_module_target, "target_gen_dir")
        _module.build_config =
            "$_module_target_gen_dir/${_module_target_name}.build_config.json"
        _module.build_config_target =
            "$_module_target$build_config_target_suffix"
        _module.parent = "base"
        _modules += [ _module ]
      }
    }

    # Make build config, which is required for synchronized proguarding.
    _module_java_targets = []
    _module_build_configs = []
    _module_targets = []
    foreach(_module, _modules) {
      _module_targets += [ _module.module_target ]
      _module_java_targets += [ "${_module.module_target}__java" ]
      _module_build_configs += [ _module.build_config ]
    }

    if (_uses_static_library) {
      _lib_proxy_module = {
        name = "lib"
      }
      _static_library_target_name =
          get_label_info(invoker.static_library_provider, "name")
      _static_library_gen_dir =
          get_label_info(invoker.static_library_provider, "target_gen_dir")
      _lib_proxy_module.build_config = "$_static_library_gen_dir/$_static_library_target_name.build_config.json"
      _lib_proxy_module.build_config_target =
          "${invoker.static_library_provider}$build_config_target_suffix"
    }

    # Allot native libraries to modules they should be packaged into. This is
    # necessary since all libraries that are depended on by multiple modules
    # have to go into base or the static shared library if it exists.
    # TODO(crbug.com/1021565): It would be nice if this lived outside the
    # android_app_bundle template and the static shared library would pull in
    # the libs as allotted by this step.
    _native_libraries_config =
        "$target_gen_dir/$_target_name.native_libraries_config"
    _native_libraries_config_target = "${_target_name}__allot_native_libraries"
    allot_native_libraries(_native_libraries_config_target) {
      modules = _modules
      native_libraries_filearg_keys = [
        "native:libraries",
        "native:loadable_modules",
      ]
      output = _native_libraries_config
      if (_uses_static_library) {
        modules += [ _lib_proxy_module ]
      }
    }
    if (defined(android_app_secondary_abi)) {
      _secondary_abi_native_libraries_config =
          "$target_gen_dir/$_target_name.secondary_abi_native_libraries_config"
      _secondary_abi_native_libraries_config_target =
          "${_target_name}__allot_secondary_abi_native_libraries"
      allot_native_libraries(_secondary_abi_native_libraries_config_target) {
        modules = _modules
        native_libraries_filearg_keys = [
          "native:secondary_abi_libraries",
          "native:secondary_abi_loadable_modules",
        ]
        output = _secondary_abi_native_libraries_config
        if (_uses_static_library) {
          modules += [ _lib_proxy_module ]
        }
      }
    }

    # Used to expose the module Java targets of the bundle.
    group("${_target_name}__java") {
      deps = _module_java_targets
    }
    group("${_target_name}__compile_resources") {
      deps = [ "${invoker.base_module_target}__compile_resources" ]
    }

    _build_config = "$target_gen_dir/${_target_name}.build_config.json"
    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    _build_config_target = "$_target_name$build_config_target_suffix"
    if (defined(invoker.proguard_android_sdk_dep)) {
      proguard_android_sdk_dep_ = invoker.proguard_android_sdk_dep
    } else {
      proguard_android_sdk_dep_ = "//third_party/android_sdk:android_sdk_java"
    }

    if (_proguard_enabled) {
      _proguard_mapping_path = "${_bundle_path}.mapping"
    }

    write_build_config(_build_config_target) {
      type = "android_app_bundle"
      possible_config_deps = _module_targets + [ proguard_android_sdk_dep_ ]
      build_config = _build_config
      proguard_enabled = _proguard_enabled
      module_build_configs = _module_build_configs

      if (_proguard_enabled) {
        proguard_mapping_path = _proguard_mapping_path
      }
    }

    if (_proguard_enabled) {
      # If this Bundle uses a static library, the static library APK will
      # create the synchronized dex file path.
      if (!_uses_static_library_synchronized_proguard) {
        dex(_dex_target) {
          forward_variables_from(invoker,
                                 [
                                   "expected_proguard_config",
                                   "expected_proguard_config_base",
                                   "min_sdk_version",
                                   "proguard_enable_obfuscation",
                                 ])
          if (defined(expected_proguard_config)) {
            top_target_name = _target_name
          }
          enable_multidex = _enable_multidex
          proguard_enabled = true
          proguard_mapping_path = _proguard_mapping_path
          proguard_sourcefile_suffix = "$android_channel-$_version_code"
          build_config = _build_config

          deps = _module_java_targets + [ ":$_build_config_target" ]
          modules = _modules
        }
      }
    }

    _all_create_module_targets = []
    _all_module_zip_paths = []
    _all_module_build_configs = []
    _all_module_unused_resources_deps = []
    foreach(_module, _modules) {
      _module_target = _module.module_target
      _module_build_config = _module.build_config
      _module_build_config_target = _module.build_config_target

      if (!_proguard_enabled) {
        _dex_target_for_module = "${_module_target}__final_dex"
      } else {
        _dex_target_for_module = ":$_dex_target"
      }

      # Generate one module .zip file per bundle module.
      #
      # Important: the bundle tool uses the module's zip filename as
      # the internal module name inside the final bundle, in other words,
      # this file *must* be named ${_module.name}.zip
      _create_module_target = "${_target_name}__${_module.name}__create"
      _module_zip_path = "$target_gen_dir/$target_name/${_module.name}.zip"
      create_android_app_bundle_module(_create_module_target) {
        forward_variables_from(invoker,
                               [
                                 "is_multi_abi",
                                 "min_sdk_version",
                                 "uncompress_dex",
                                 "proguard_enabled",
                               ])
        module_name = _module.name
        build_config = _module_build_config
        module_zip_path = _module_zip_path
        native_libraries_config = _native_libraries_config

        if (module_name == "base" &&
            defined(invoker.expected_libs_and_assets)) {
          forward_variables_from(invoker,
                                 [
                                   "expected_libs_and_assets",
                                   "expected_libs_and_assets_base",
                                 ])
          top_target_name = _target_name
          build_config_target = _module_build_config_target
          native_libraries_config_target = ":$_native_libraries_config_target"
          if (defined(android_app_secondary_abi)) {
            secondary_abi_native_libraries_config_target =
                ":$_secondary_abi_native_libraries_config_target"
          }
        }

        deps = [
          ":$_native_libraries_config_target",
          _dex_target_for_module,
          _module_build_config_target,
          _module_target,
        ]

        if (defined(android_app_secondary_abi)) {
          secondary_abi_native_libraries_config =
              _secondary_abi_native_libraries_config
          deps += [ ":$_secondary_abi_native_libraries_config_target" ]
        }
      }

      _all_create_module_targets += [
        ":$_create_module_target",
        _module_build_config_target,
        "${_module_target}__compile_resources",
      ]
      _all_module_zip_paths += [ _module_zip_path ]
      _all_module_build_configs += [ _module_build_config ]
      _all_module_unused_resources_deps += [
        "${_module_target}__compile_resources",
        _dex_target_for_module,
        _module_build_config_target,
      ]
    }
    if (defined(invoker.strip_unused_resources) &&
        invoker.strip_unused_resources) {
      # Resources only live in the base module so we define the unused resources
      # target only on the base module target.
      _unused_resources_target = "${_base_target_name}__unused_resources"
      _unused_resources_config =
          "${_base_target_gen_dir}/${_base_target_name}_unused_resources.config"
      unused_resources(_unused_resources_target) {
        deps = _all_module_unused_resources_deps
        all_module_build_configs = _all_module_build_configs
        build_config = _base_module_build_config
        if (_proguard_enabled) {
          proguard_mapping_path = _proguard_mapping_path
        }
        output_config = _unused_resources_config
      }
    }

    _all_rebased_module_zip_paths =
        rebase_path(_all_module_zip_paths, root_build_dir)

    _enable_language_splits = defined(invoker.enable_language_splits) &&
                              invoker.enable_language_splits

    _split_dimensions = []
    if (_enable_language_splits) {
      _split_dimensions += [ "language" ]
    }

    _keystore_path = android_keystore_path
    _keystore_password = android_keystore_password
    _keystore_name = android_keystore_name

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

    _rebased_keystore_path = rebase_path(_keystore_path, root_build_dir)

    _bundle_target_name = "${_target_name}__bundle"
    action_with_pydeps(_bundle_target_name) {
      script = "//build/android/gyp/create_app_bundle.py"
      inputs = _all_module_zip_paths + _all_module_build_configs
      outputs = [ _bundle_path ]
      deps = _all_create_module_targets + [ ":$_build_config_target" ]
      args = [
        "--out-bundle=$_rebased_bundle_path",
        "--rtxt-out-path=$_rebased_bundle_path.R.txt",
        "--pathmap-out-path=$_rebased_bundle_path.pathmap.txt",
        "--module-zips=$_all_rebased_module_zip_paths",
      ]
      if (_split_dimensions != []) {
        args += [ "--split-dimensions=$_split_dimensions" ]
      }
      if (defined(invoker.compress_shared_libraries) &&
          invoker.compress_shared_libraries) {
        args += [ "--compress-shared-libraries" ]
      }
      _min_sdk_version = default_min_sdk_version
      if (defined(invoker.min_sdk_version)) {
        _min_sdk_version = invoker.min_sdk_version
      }

      # Android P+ support loading from stored dex.
      if (_min_sdk_version < 27) {
        args += [ "--compress-dex" ]
      }

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

      if (_enable_language_splits) {
        args += [
          "--base-allowlist-rtxt-path=@FileArg(" + "${_rebased_base_module_build_config}:deps_info:base_allowlist_rtxt_path)",
          "--base-module-rtxt-path=@FileArg(" +
              "${_rebased_base_module_build_config}:deps_info:r_text_path)",
        ]
      }
      if (defined(invoker.validate_services) && invoker.validate_services) {
        args += [ "--validate-services" ]
      }

      foreach(_module, _modules) {
        _rebased_build_config =
            rebase_path(_module.build_config, root_build_dir)
        args += [
          "--uncompressed-assets=@FileArg(" +
              "$_rebased_build_config:uncompressed_assets)",
          "--rtxt-in-paths=@FileArg(" +
              "$_rebased_build_config:deps_info:r_text_path)",
          "--pathmap-in-paths=@FileArg(" +
              "$_rebased_build_config:deps_info:module_pathmap_path)",
          "--module-name=" + _module.name,
        ]
      }

      # 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)"
      }
    }

    # Create size info files for targets that care about size
    # (have proguard enabled).
    if (_proguard_enabled) {
      # Merge all module targets to obtain size info files for all targets.
      _all_module_targets = _module_targets

      _size_info_target = "${_target_name}__size_info"
      create_size_info_files(_size_info_target) {
        name = "$_bundle_name.aab"
        deps = _all_module_targets + [ ":$_build_config_target" ]
        module_build_configs = _all_module_build_configs
      }
    }

    if (_uses_static_library) {
      _install_artifacts_target = "${target_name}__install_artifacts"
      _install_artifacts_json =
          "${target_gen_dir}/${target_name}.install_artifacts"
      generated_file(_install_artifacts_target) {
        output_conversion = "json"
        deps = [ invoker.static_library_provider ]
        outputs = [ _install_artifacts_json ]
        data_keys = [ "install_artifacts" ]
        rebase = root_build_dir
      }
    }

    # Generate a wrapper script for the bundle.
    _android_aapt2_path = android_sdk_tools_bundle_aapt2

    _bundle_apks_path = "$_bundle_base_path/$_bundle_name.apks"
    _bundle_wrapper_script_dir = "$root_build_dir/bin"
    _bundle_wrapper_script_path = "$_bundle_wrapper_script_dir/$_target_name"

    action_with_pydeps("${_target_name}__wrapper_script") {
      script = "//build/android/gyp/create_bundle_wrapper_script.py"
      inputs = [ _base_module_build_config ]
      outputs = [ _bundle_wrapper_script_path ]

      # Telemetry for bundles uses the wrapper script for installation.
      data = [
        _bundle_wrapper_script_path,
        _android_aapt2_path,
        _keystore_path,
        _bundle_path,
      ]
      data_deps = [
        "//build/android:apk_operations_py",
        "//build/android:stack_tools",
      ]

      deps = [ _base_module_build_config_target ]
      args = [
        "--script-output-path",
        rebase_path(_bundle_wrapper_script_path, root_build_dir),
        "--package-name=@FileArg(" +
            "$_rebased_base_module_build_config:deps_info:package_name)",
        "--aapt2",
        rebase_path(_android_aapt2_path, root_build_dir),
        "--bundle-path",
        _rebased_bundle_path,
        "--bundle-apks-path",
        rebase_path(_bundle_apks_path, root_build_dir),
        "--target-cpu=$target_cpu",
        "--keystore-path",
        _rebased_keystore_path,
        "--keystore-password",
        _keystore_password,
        "--key-name",
        _keystore_name,
      ]
      if (defined(invoker.default_modules_for_testing)) {
        args += [ "--default-modules" ] + invoker.default_modules_for_testing
      }
      if (defined(invoker.system_image_locale_allowlist)) {
        args += [
          "--system-image-locales=${invoker.system_image_locale_allowlist}",
        ]
      }
      if (defined(invoker.command_line_flags_file)) {
        args += [
          "--command-line-flags-file",
          invoker.command_line_flags_file,
        ]
      }
      if (_uses_static_library) {
        deps += [ ":$_install_artifacts_target" ]
        _rebased_install_artifacts_json =
            rebase_path(_install_artifacts_json, root_build_dir)
        _static_library_apk_path =
            "@FileArg($_rebased_install_artifacts_json[])"
        args += [
          "--additional-apk",
          _static_library_apk_path,
        ]
      }

      if (_proguard_enabled) {
        args += [
          "--proguard-mapping-path",
          rebase_path(_proguard_mapping_path, root_build_dir),
        ]

        # Required by logcat command.
        data_deps += [ "//build/android/stacktrace:java_deobfuscate" ]
        data += [ _proguard_mapping_path ]
      }
    }

    _enable_lint = defined(invoker.enable_lint) && invoker.enable_lint &&
                   !disable_android_lint
    if (_enable_lint) {
      android_lint("${target_name}__lint") {
        forward_variables_from(invoker,
                               [
                                 "lint_baseline_file",
                                 "lint_suppressions_file",
                                 "min_sdk_version",
                               ])
        build_config = _build_config
        build_config_dep = ":$_build_config_target"
        deps = _module_java_targets
        if (defined(invoker.lint_suppressions_dep)) {
          deps += [ invoker.lint_suppressions_dep ]
        }
        if (defined(invoker.lint_min_sdk_version)) {
          min_sdk_version = invoker.lint_min_sdk_version
        }
      }
    } else {
      not_needed(invoker,
                 [
                   "lint_baseline_file",
                   "lint_min_sdk_version",
                   "lint_suppressions_dep",
                   "lint_suppressions_file",
                 ])
    }

    group(_target_name) {
      public_deps = [
        ":$_bundle_target_name",
        ":${_target_name}__wrapper_script",
      ]
      if (defined(_size_info_target)) {
        public_deps += [ ":$_size_info_target" ]
      }
      if (_enable_lint) {
        if (!defined(data_deps)) {
          data_deps = []
        }
        data_deps += [ ":${target_name}__lint" ]
      }
    }

    _apks_path = "$root_build_dir/apks/$_bundle_name.apks"
    action_with_pydeps("${_target_name}_apks") {
      script = "//build/android/gyp/create_app_bundle_apks.py"
      inputs = [ _bundle_path ]
      outputs = [ _apks_path ]
      data = [ _apks_path ]
      args = [
        "--bundle",
        _rebased_bundle_path,
        "--output",
        rebase_path(_apks_path, root_build_dir),
        "--aapt2-path",
        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
        "--keystore-path",
        rebase_path(android_keystore_path, root_build_dir),
        "--keystore-name",
        android_keystore_name,
        "--keystore-password",
        android_keystore_password,
      ]
      if (debuggable_apks) {
        args += [ "--local-testing" ]
      }
      deps = [ ":$_bundle_target_name" ]
      metadata = {
        install_artifacts = [ _apks_path ]
      }
      if (defined(invoker.static_library_provider)) {
        metadata.install_artifacts_barrier = []
      }

      # 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)"
      }
    }
  }

  # Create an .apks file from an .aab file. The .apks file will contain the
  # minimal set of .apk files needed for tracking binary size.
  # The file will be created at "$bundle_path_without_extension.minimal.apks".
  #
  # Variables:
  #   bundle_path: Path to the input .aab file.
  #
  # Example:
  #   create_app_bundle_minimal_apks("minimal_apks") {
  #     deps = [
  #       ":bundle_target",
  #     ]
  #     bundle_path = "$root_build_dir/apks/Bundle.aab"
  #   }
  template("create_app_bundle_minimal_apks") {
    action_with_pydeps(target_name) {
      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
      script = "//build/android/gyp/create_app_bundle_apks.py"
      _dir = get_path_info(invoker.bundle_path, "dir")
      _name = get_path_info(invoker.bundle_path, "name")
      _output_path = "$_dir/$_name.minimal.apks"
      outputs = [ _output_path ]
      inputs = [ invoker.bundle_path ]
      args = [
        "--bundle",
        rebase_path(invoker.bundle_path, root_build_dir),
        "--output",
        rebase_path(_output_path, root_build_dir),
        "--aapt2-path",
        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
        "--keystore-path",
        rebase_path(android_keystore_path, root_build_dir),
        "--keystore-name",
        android_keystore_name,
        "--keystore-password",
        android_keystore_password,
        "--minimal",
      ]
    }
  }
}

# Generate an Android resources target that contains localized strings
# describing the current locale used by the Android framework to display
# UI strings. These are used by
# org.chromium.chrome.browser.ChromeLocalizationUtils.
#
# Variables:
#    ui_locales: List of Chromium locale names to generate resources for.
#
template("generate_ui_locale_resources") {
  _generating_target_name = "${target_name}__generate"
  _rebased_output_zip_path = rebase_path(target_gen_dir, root_gen_dir)
  _output_zip = "${root_out_dir}/resource_zips/${_rebased_output_zip_path}/" +
                "${target_name}.zip"

  action_with_pydeps(_generating_target_name) {
    script = "//build/android/gyp/create_ui_locale_resources.py"
    outputs = [ _output_zip ]
    args = [
      "--locale-list=${invoker.ui_locales}",
      "--output-zip",
      rebase_path(_output_zip, root_build_dir),
    ]
  }

  android_generated_resources(target_name) {
    generating_target = ":$_generating_target_name"
    generated_resources_zip = _output_zip
  }
}