summaryrefslogtreecommitdiff
path: root/build-system/manifest-merger/src/main/java/com/android/manifmerger/ManifestMerger2.java
blob: db025a4e9daa2486523f1f37657f25d0b46c8075 (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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.manifmerger;

import static com.android.SdkConstants.ATTR_NAME;
import static com.android.SdkConstants.ATTR_SPLIT;
import static com.android.manifmerger.ManifestModel.NodeTypes.USES_SDK;
import static com.android.manifmerger.MergingReport.MergedManifestKind.AAPT_SAFE;
import static com.android.manifmerger.PlaceholderHandler.APPLICATION_ID;
import static com.android.manifmerger.PlaceholderHandler.KeyBasedValueResolver;
import static com.android.manifmerger.PlaceholderHandler.PACKAGE_NAME;

import com.android.AndroidXConstants;
import com.android.SdkConstants;
import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.annotations.concurrency.Immutable;
import com.android.ide.common.xml.XmlFormatPreferences;
import com.android.ide.common.xml.XmlFormatStyle;
import com.android.ide.common.xml.XmlPrettyPrinter;
import com.android.sdklib.SdkVersionInfo;
import com.android.utils.FileUtils;
import com.android.utils.ILogger;
import com.android.utils.Pair;
import com.android.utils.XmlUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * merges android manifest files, idempotent.
 */
@Immutable
public class ManifestMerger2 {

    public static final String COMPATIBLE_SCREENS_SUB_MANIFEST = "Compatible-Screens sub-manifest";
    public static final String WEAR_APP_SUB_MANIFEST = "Wear App sub-manifest";

    private static final String SPLIT_IN_DYNAMIC_FEATURE =
            "https://d.android.com/r/studio-ui/dynamic-delivery/dynamic-feature-manifest";

    @NonNull
    private final File mManifestFile;

    @NonNull
    private final Map<String, Object> mPlaceHolderValues;

    @NonNull
    private final KeyBasedValueResolver<ManifestSystemProperty>
            mSystemPropertyResolver;

    @NonNull
    private final ILogger mLogger;
    @NonNull
    private final ImmutableList<Pair<String, File>> mLibraryFiles;
    @NonNull
    private final ImmutableList<File> mFlavorsAndBuildTypeFiles;
    @NonNull
    private final ImmutableList<Invoker.Feature> mOptionalFeatures;
    @NonNull
    private final MergeType mMergeType;
    @NonNull
    private final XmlDocument.Type mDocumentType;
    @NonNull
    private final Optional<File> mReportFile;
    @NonNull private final String mFeatureName;
    @Nullable private final String mNamespace;
    @NonNull private final FileStreamProvider mFileStreamProvider;
    @Nullable private final ManifestDocumentProvider mManifestDocumentProvider;
    @NonNull private final ProcessCancellationChecker mProcessCancellationChecker;
    @NonNull private final ImmutableList<File> mNavigationFiles;
    @NonNull private final ImmutableList<File> mNavigationJsons;
    @NonNull private final DocumentModel<ManifestModel.NodeTypes> mModel;
    @NonNull private final ImmutableList<String> mDependencyFeatureNames;
    @NonNull private final ImmutableList<String> mAllowedNonUniqueNamespaces;
    @Nullable private final String mGeneratedLocaleConfigAttribute;

    private ManifestMerger2(
            @NonNull ILogger logger,
            @NonNull File mainManifestFile,
            @NonNull ImmutableList<Pair<String, File>> libraryFiles,
            @NonNull ImmutableList<File> flavorsAndBuildTypeFiles,
            @NonNull ImmutableList<Invoker.Feature> optionalFeatures,
            @NonNull Map<String, Object> placeHolderValues,
            @NonNull KeyBasedValueResolver<ManifestSystemProperty> systemPropertiesResolver,
            @NonNull MergeType mergeType,
            @NonNull XmlDocument.Type documentType,
            @NonNull Optional<File> reportFile,
            @NonNull String featureName,
            @Nullable String namespace,
            @NonNull FileStreamProvider fileStreamProvider,
            @Nullable ManifestDocumentProvider manifestDocumentProvider,
            @NonNull ProcessCancellationChecker processCancellationChecker,
            @NonNull ImmutableList<File> navigationFiles,
            @NonNull ImmutableList<File> navigationJsons,
            @NonNull ImmutableList<String> dependencyFeatureNames,
            @NonNull ImmutableList<String> allowedNonUniqueNamespaces,
            @Nullable String generatedLocaleConfigAttribute) {
        this.mSystemPropertyResolver = systemPropertiesResolver;
        this.mPlaceHolderValues = placeHolderValues;
        this.mManifestFile = mainManifestFile;
        this.mLogger = logger;
        this.mLibraryFiles = libraryFiles;
        this.mFlavorsAndBuildTypeFiles = flavorsAndBuildTypeFiles;
        this.mOptionalFeatures = optionalFeatures;
        this.mMergeType = mergeType;
        this.mDocumentType = documentType;
        this.mReportFile = reportFile;
        this.mFeatureName = featureName;
        this.mNamespace = namespace;
        this.mFileStreamProvider = fileStreamProvider;
        this.mManifestDocumentProvider = manifestDocumentProvider;
        this.mProcessCancellationChecker = processCancellationChecker;
        this.mNavigationFiles = navigationFiles;
        this.mNavigationJsons = navigationJsons;
        this.mDependencyFeatureNames = dependencyFeatureNames;
        this.mModel =
                new ManifestModel(
                        mOptionalFeatures.contains(
                                Invoker.Feature.HANDLE_VALUE_CONFLICTS_AUTOMATICALLY));
        this.mAllowedNonUniqueNamespaces = allowedNonUniqueNamespaces;
        this.mGeneratedLocaleConfigAttribute = generatedLocaleConfigAttribute;
    }

    /**
     * Perform high level ordering of files merging and delegates actual merging to {@link
     * XmlDocument#merge}
     *
     * @return the merging activity report.
     * @throws MergeFailureException if the merging cannot be completed (for instance, if xml files
     *     cannot be loaded).
     */
    @NonNull
    private MergingReport merge() throws MergeFailureException {
        // initiate a new merging report
        MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);

        SelectorResolver selectors = new SelectorResolver();

        // load the main manifest file to do some checking along the way.
        LoadedManifestInfo loadedMainManifestInfo =
                load(
                        new ManifestInfo(mManifestFile.getName(), mManifestFile, mDocumentType),
                        selectors,
                        mergingReportBuilder,
                        mNamespace);

        mProcessCancellationChecker.check();

        // perform all top-level verifications.
        if (!loadedMainManifestInfo
                        .getXmlDocument()
                        .checkTopLevelDeclarations(
                                mPlaceHolderValues, mergingReportBuilder, mDocumentType)
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            return mergingReportBuilder.build();
        }

        if (!mFeatureName.isEmpty()) {
            loadedMainManifestInfo =
                    removeDynamicFeatureManifestSplitAttributeIfSpecified(
                            loadedMainManifestInfo, mergingReportBuilder);
        }

        Optional<XmlAttribute> mainPackageAttribute =
                loadedMainManifestInfo.getXmlDocument().getPackage();
        final String originalMainManifestPackageName =
                mainPackageAttribute.map(XmlAttribute::getValue).orElse(null);

        if (mOptionalFeatures.contains(Invoker.Feature.CHECK_IF_PACKAGE_IN_MAIN_MANIFEST)
                && originalMainManifestPackageName != null) {
            if (originalMainManifestPackageName.equals(mNamespace)) {
                String message =
                        String.format(
                                "package=\"%1$s\" found in source AndroidManifest.xml: %2$s.\n"
                                        + "Setting the namespace via the package attribute in the "
                                        + "source AndroidManifest.xml is no longer supported, "
                                        + "and the value is ignored.\n"
                                        + "Recommendation: remove package=\"%1$s\" from the source "
                                        + "AndroidManifest.xml: %2$s.",
                                originalMainManifestPackageName,
                                loadedMainManifestInfo.getLocation().getAbsolutePath());
                mLogger.warning(message);
            } else {
                String message =
                        String.format(
                                "Incorrect package=\"%1$s\" found in source AndroidManifest.xml: "
                                        + "%2$s.\n"
                                        + "Setting the namespace via the package attribute in the "
                                        + "source AndroidManifest.xml is no longer supported.\n"
                                        + "Recommendation: remove package=\"%1$s\" from the source "
                                        + "AndroidManifest.xml: %2$s.",
                                originalMainManifestPackageName,
                                loadedMainManifestInfo.getLocation().getAbsolutePath());
                mLogger.error(null, message);
                throw new RuntimeException(message);
            }
        }

        mProcessCancellationChecker.check();

        // load all the libraries xml files early to have a list of all possible node:selector
        // values.
        List<LoadedManifestInfo> loadedLibraryDocuments =
                loadLibraries(selectors, mergingReportBuilder, originalMainManifestPackageName);

        // make sure each module/library has a unique namespace
        boolean enforceUniquePackageName =
                mOptionalFeatures.contains(Invoker.Feature.ENFORCE_UNIQUE_PACKAGE_NAME);
        boolean disablePackageUniquenessCheck =
                mOptionalFeatures.contains(Invoker.Feature.DISABLE_PACKAGE_NAME_UNIQUENESS_CHECK);
        if (enforceUniquePackageName && disablePackageUniquenessCheck) {
            mLogger.warning(
                    "Both flags ENFORCE_UNIQUE_PACKAGE_NAME and"
                            + " DISABLE_PACKAGE_NAME_UNIQUENESS_CHECK are found to be true."
                            + " Flag ENFORCE_UNIQUE_PACKAGE_NAME will be ignored because package"
                            + " name uniqueness check is disabled.");
        }
        if (!disablePackageUniquenessCheck) {
            checkUniqueNamespaces(
                    loadedMainManifestInfo,
                    loadedLibraryDocuments,
                    mAllowedNonUniqueNamespaces,
                    mergingReportBuilder,
                    enforceUniquePackageName);
        }

        // invariant : xmlDocumentOptional holds the higher priority document and we try to
        // merge in lower priority documents.
        @Nullable XmlDocument xmlDocumentOptional = null;
        for (File inputFile : mFlavorsAndBuildTypeFiles) {
            mProcessCancellationChecker.check();
            mLogger.verbose("Merging flavors and build manifest %s \n", inputFile.getPath());
            LoadedManifestInfo overlayDocument =
                    load(
                            new ManifestInfo(null, inputFile, XmlDocument.Type.OVERLAY),
                            selectors,
                            mergingReportBuilder,
                            mNamespace);

            if (!mFeatureName.isEmpty()) {
                overlayDocument =
                        removeDynamicFeatureManifestSplitAttributeIfSpecified(
                                overlayDocument, mergingReportBuilder);
            }

            // check package declaration.
            Optional<XmlAttribute> packageAttribute =
                    overlayDocument.getXmlDocument().getPackage();
            // if overlay file declares a package name, it should be the same as the namespace
            if (!Strings.isNullOrEmpty(loadedMainManifestInfo.getNamespace())
                    && packageAttribute.isPresent()
                    && !loadedMainManifestInfo
                            .getNamespace()
                            .equals(packageAttribute.get().getValue())) {
                // no suggestion for library since this is actually forbidden to change the
                // the package name per flavor.
                String message =
                        mMergeType == MergeType.APPLICATION
                                ? String.format(
                                        "Overlay manifest:package attribute declared at %1$s value=(%2$s)\n"
                                                + "\tdoes not match the module's namespace (%3$s).\n"
                                                + "\tSuggestion: remove the package=\"%2$s\" declaration at %4$s.\n"
                                                + "\tIf you want to customize the applicationId for a "
                                                + "buildType or productFlavor, consider setting "
                                                + "applicationIdSuffix or using the Variant API.\n"
                                                + "\tFor more information, see "
                                                + "https://developer.android.com/studio/build/build-variants",
                                        packageAttribute.get().printPosition(),
                                        packageAttribute.get().getValue(),
                                        loadedMainManifestInfo.getNamespace(),
                                        packageAttribute.get().getSourceFile().print(false))
                                : String.format(
                                        "Overlay manifest:package attribute declared at %1$s value=(%2$s)\n"
                                                + "\tdoes not match the module's namespace (%3$s).\n"
                                                + "\tSuggestion: remove the package=\"%2$s\" declaration at %4$s.",
                                        packageAttribute.get().printPosition(),
                                        packageAttribute.get().getValue(),
                                        loadedMainManifestInfo.getNamespace(),
                                        packageAttribute.get().getSourceFile().print(false));
                mergingReportBuilder.addMessage(
                        overlayDocument.getXmlDocument().getSourceFile(),
                        MergingReport.Record.Severity.ERROR,
                        message);
                if (!mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
                    return mergingReportBuilder.build();
                }
            }

            if (mainPackageAttribute.isPresent()) {
                overlayDocument
                        .getXmlDocument()
                        .getRootNode()
                        .setAttribute("package", mainPackageAttribute.get().getValue());
            }
            Optional<XmlDocument> newMergedDocument =
                    merge(xmlDocumentOptional, overlayDocument, mergingReportBuilder);

            xmlDocumentOptional = newMergedDocument.orElse(null);

            if (!newMergedDocument.isPresent()
                    && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
                return mergingReportBuilder.build();
            }
        }

        mProcessCancellationChecker.check();

        mLogger.verbose("Merging main manifest %s\n", mManifestFile.getPath());
        Optional<XmlDocument> newMergedDocument =
                merge(xmlDocumentOptional, loadedMainManifestInfo, mergingReportBuilder);

        if (!newMergedDocument.isPresent()
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            return mergingReportBuilder.build();
        }
        xmlDocumentOptional = newMergedDocument.get();

        // perform system property injection
        performSystemPropertiesInjection(mergingReportBuilder, xmlDocumentOptional);

        // force main manifest package into resulting merged file when creating a library manifest.
        if (mMergeType == MergeType.LIBRARY) {
            // extract the package name...
            String mainManifestPackageName = loadedMainManifestInfo.getXmlDocument().getRootNode()
                    .getXml().getAttribute("package");
            // save it in the selector instance.
            if (!Strings.isNullOrEmpty(mainManifestPackageName)) {
                xmlDocumentOptional
                        .getRootNode()
                        .setAttribute("package", mainManifestPackageName);
            }
        }
        for (LoadedManifestInfo libraryDocument : loadedLibraryDocuments) {
            mLogger.verbose("Merging library manifest " + libraryDocument.getLocation());
            newMergedDocument = merge(xmlDocumentOptional, libraryDocument, mergingReportBuilder);
            if (!newMergedDocument.isPresent()
                    && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
                return mergingReportBuilder.build();
            }
            xmlDocumentOptional = newMergedDocument.get();

            mProcessCancellationChecker.check();
        }

        // done with proper merging phase, now we need to expand <nav-graph> elements, trim unwanted
        // elements, perform placeholder substitution and system properties injection.

        if (mMergeType == MergeType.APPLICATION) {
            Map<String, NavigationXmlDocument> loadedNavigationMap = createNavigationMap();
            xmlDocumentOptional =
                    NavGraphExpander.INSTANCE.expandNavGraphs(
                            xmlDocumentOptional, loadedNavigationMap, mergingReportBuilder);
        }

        if (mergingReportBuilder.hasErrors()
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            return mergingReportBuilder.build();
        }

        ElementsTrimmer.trim(xmlDocumentOptional, mergingReportBuilder);
        if (mergingReportBuilder.hasErrors()
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            return mergingReportBuilder.build();
        }

        if (!mOptionalFeatures.contains(Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)) {
            // do one last placeholder substitution, this is useful as we don't stop the build
            // when a library failed a placeholder substitution, but the element might have
            // been overridden so the problem was transient. However, with the final document
            // ready, all placeholders values must have been provided.
            MergingReport.Record.Severity severity =
                    !mMergeType.isFullPlaceholderSubstitutionRequired()
                            ? MergingReport.Record.Severity.INFO
                            : MergingReport.Record.Severity.ERROR;
            performPlaceHolderSubstitution(
                    xmlDocumentOptional,
                    originalMainManifestPackageName,
                    mergingReportBuilder,
                    severity);
            if (mergingReportBuilder.hasErrors()
                    && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
                return mergingReportBuilder.build();
            }
        }

        mProcessCancellationChecker.check();

        // if it's a library of any kind - need to remove targetSdk
        if (!mOptionalFeatures.contains(Invoker.Feature.DISABLE_STRIP_LIBRARY_TARGET_SDK)
                && mMergeType != MergeType.APPLICATION) {
            stripTargetSdk(xmlDocumentOptional);
        }

        XmlDocument finalMergedDocument = xmlDocumentOptional;

        Optional<XmlAttribute> packageAttr = finalMergedDocument.getPackage();
        // We allow single word package name for library and fused libraries, the platform does
        // not allow for single word application package names.
        if (mMergeType == MergeType.APPLICATION && packageAttr.isPresent()) {
            XmlAttribute packageNameAttribute = packageAttr.get();
            String packageName = packageNameAttribute.getValue();
            // We accept absence of dot only if NO_PLACEHOLDER_REPLACEMENT is true and packageName
            // is a placeholder
            if (!(mOptionalFeatures.contains(Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)
                            && PlaceholderHandler.isPlaceHolder(packageName))
                    && !packageName.contains(".")) {
                mergingReportBuilder.addMessage(
                        loadedMainManifestInfo.getXmlDocument().getSourceFile(),
                        MergingReport.Record.Severity.ERROR,
                        String.format(
                                "Package name '%1$s' at position %2$s should contain at "
                                        + "least one '.' (dot) character",
                                packageName, packageNameAttribute.printPosition()));
                if (!mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
                    return mergingReportBuilder.build();
                }
            }
        }

        if (!mOptionalFeatures.contains(Invoker.Feature.REMOVE_TOOLS_DECLARATIONS)) {
            PostValidator.enforceToolsNamespaceDeclaration(finalMergedDocument);
        }

        mProcessCancellationChecker.check();

        // reset the node operations to their original ones if they get changed
        finalMergedDocument.originalNodeOperation.forEach(
                (k, v) -> {
                    k.setAttributeNS(SdkConstants.TOOLS_URI, "tools:node", v.toXmlName());
                });

        PostValidator.validate(finalMergedDocument, mergingReportBuilder);
        if (mergingReportBuilder.hasErrors()
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            mergingReportBuilder.addMessage(
                    finalMergedDocument.getRootNode(),
                    MergingReport.Record.Severity.WARNING,
                    "Post merge validation failed");
        }

        finalMergedDocument.clearNodeNamespaces();

        // extract fully qualified class names before handling other optional features.
        if (mOptionalFeatures.contains(Invoker.Feature.EXTRACT_FQCNS)) {
            @NonNull final String namespace;
            if (mNamespace != null) {
                namespace = mNamespace;
            } else if (originalMainManifestPackageName != null) {
                namespace = originalMainManifestPackageName;
            } else {
                namespace = "";
            }
            extractFqcns(namespace, finalMergedDocument.getRootNode());
        }

        mProcessCancellationChecker.check();

        // handle optional features which don't need access to XmlDocument layer.
        processOptionalFeatures(finalMergedDocument, mergingReportBuilder);

        // android:exported should have an explicit value for S and above with <intent-filter>,
        // output an error message to the user if android:exported is not explicitly specified
        checkExportedDeclaration(finalMergedDocument, mergingReportBuilder);

        if (mergingReportBuilder.hasErrors()
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            return mergingReportBuilder.build();
        }

        mergingReportBuilder.setMergedDocument(
                MergingReport.MergedManifestKind.MERGED, prettyPrint(finalMergedDocument.getXml()));

        mProcessCancellationChecker.check();

        // call blame after other optional features handled.
        if (!mOptionalFeatures.contains(Invoker.Feature.SKIP_BLAME)) {
            try {
                mergingReportBuilder.setMergedDocument(
                        MergingReport.MergedManifestKind.BLAME,
                        mergingReportBuilder.blame(finalMergedDocument));
            } catch (Exception e) {
                mLogger.error(e, "Error while saving blame file, build will continue");
            }
        }

        mergingReportBuilder.setMergedXmlDocument(finalMergedDocument);

        MergingReport mergingReport = mergingReportBuilder.build();

        if (mReportFile.isPresent()) {
            writeReport(mergingReport);
        }

        return mergingReport;
    }

    private Map<String, NavigationXmlDocument> createNavigationMap() throws MergeFailureException {
        Map<String, NavigationXmlDocument> loadedNavigationMap = new HashMap<>();
        for (File navigationFile : mNavigationFiles) {
            String navigationId = navigationFile.getName().replaceAll("\\.xml$", "");
            if (loadedNavigationMap.get(navigationId) != null) {
                continue;
            }
            try (InputStream inputStream = mFileStreamProvider.getInputStream(navigationFile)) {
                loadedNavigationMap.put(
                        navigationId,
                        NavigationXmlLoader.INSTANCE.load(
                                navigationId, navigationFile, inputStream));
            } catch (Exception e) {
                throw new MergeFailureException(e);
            }
        }
        Gson gson = new GsonBuilder().create();
        for (File navigationJson : mNavigationJsons) {
            try {
                String jsonText = FileUtils.loadFileWithUnixLineSeparators(navigationJson);
                NavigationXmlDocumentData[] navDatas =
                        gson.fromJson(jsonText, NavigationXmlDocumentData[].class);
                for (NavigationXmlDocumentData navData : navDatas) {
                    String navigationId = navData.getName();
                    if (loadedNavigationMap.get(navigationId) != null) {
                        mLogger.info(
                                "Navigation file %s from %s is ignored (skipped).",
                                navigationId, navigationJson);
                        continue;
                    }
                    loadedNavigationMap.put(navigationId, new NavigationXmlDocument(navData));
                }
            } catch (IOException e) {
                throw new MergeFailureException(e);
            }
        }
        return loadedNavigationMap;
    }

    private static LoadedManifestInfo removeDynamicFeatureManifestSplitAttributeIfSpecified(
            @NonNull LoadedManifestInfo dynamicFeatureManifest,
            @NonNull MergingReport.Builder mergingReportBuilder) {
        Optional<XmlAttribute> splitAttribute =
                dynamicFeatureManifest
                        .getXmlDocument()
                        .getRootNode()
                        .getAttribute(XmlNode.fromXmlName(ATTR_SPLIT));
        if (splitAttribute.isPresent()) {
            String message =
                    String.format(
                            "Attribute '%1$s' was removed from %2$s.\n"
                                    + "The Android Gradle plugin includes it for you "
                                    + "when building your project.\n"
                                    + "See %3$s for details.",
                            ATTR_SPLIT,
                            splitAttribute.get().printPosition(),
                            SPLIT_IN_DYNAMIC_FEATURE);
            mergingReportBuilder.addMessage(
                    dynamicFeatureManifest.getXmlDocument().getSourceFile(),
                    MergingReport.Record.Severity.WARNING,
                    message);
            dynamicFeatureManifest.getXmlDocument().getRootNode().removeAttribute(ATTR_SPLIT);
            return new LoadedManifestInfo(
                    dynamicFeatureManifest, dynamicFeatureManifest.getXmlDocument());
        }

        return dynamicFeatureManifest;
    }

    /**
     * Processes optional features which are not already handled in merge()
     *
     * @param xmlDocument the resulting document after merging
     * @param mergingReport the merging report builder
     */
    private void processOptionalFeatures(
            @Nullable XmlDocument xmlDocument, @NonNull MergingReport.Builder mergingReport)
            throws MergeFailureException {
        Document document = Optional.ofNullable(xmlDocument).map(XmlDocument::getXml).orElse(null);
        if (document == null) {
            return;
        }

        // perform tools: annotations removal if requested.
        if (mMergeType != MergeType.FUSED_LIBRARY
                && mOptionalFeatures.contains(Invoker.Feature.REMOVE_TOOLS_DECLARATIONS)) {
            ToolsInstructionsCleaner.cleanToolsReferences(mMergeType, xmlDocument, mLogger);
        }

        if (mOptionalFeatures.contains(Invoker.Feature.ADVANCED_PROFILING)) {
            addInternetPermission(xmlDocument);
        }

        if (mOptionalFeatures.contains(Invoker.Feature.TEST_ONLY)) {
            addTestOnlyAttribute(xmlDocument);
        }

        if (mOptionalFeatures.contains(Invoker.Feature.DEBUGGABLE)) {
            addDebuggableAttribute(xmlDocument);
        }

        if (mMergeType == MergeType.APPLICATION) {
            optionalAddApplicationTagIfMissing(xmlDocument);
        }

        if (mOptionalFeatures.contains(
                Invoker.Feature.ADD_ANDROIDX_MULTIDEX_APPLICATION_IF_NO_NAME)) {
            addMultiDexApplicationIfNoName(
                    xmlDocument, AndroidXConstants.MULTI_DEX_APPLICATION.newName());
        } else if (mOptionalFeatures.contains(
                Invoker.Feature.ADD_SUPPORT_MULTIDEX_APPLICATION_IF_NO_NAME)) {
            addMultiDexApplicationIfNoName(
                    xmlDocument, AndroidXConstants.MULTI_DEX_APPLICATION.oldName());
        }

        if (mOptionalFeatures.contains(Invoker.Feature.ADD_DYNAMIC_FEATURE_ATTRIBUTES)) {
            addFeatureSplitAttribute(xmlDocument, mFeatureName);
            adjustInstantAppFeatureSplitInfo(xmlDocument, mFeatureName);
            addUsesSplitTagsForDependencies(xmlDocument, mDependencyFeatureNames);
        }

        if (mOptionalFeatures.contains(Invoker.Feature.MAKE_AAPT_SAFE)) {
            createAaptSafeManifest(xmlDocument, mergingReport);
        }

        // If generated locale config is present set it if user locale config is not present.
        if (mMergeType == MergeType.APPLICATION && mGeneratedLocaleConfigAttribute != null) {
            addLocaleConfig(xmlDocument, mGeneratedLocaleConfigAttribute);
        }
    }

    /**
     * Creates a manifest suitable for use with AAPT by (1) substituting placeholders to an AAPT
     * friendly encoding and (2) removing any <nav-graph> tags. Saves the modified manifest as part
     * of the merging report. Does not mutate the passed in document.
     */
    @VisibleForTesting
    static void createAaptSafeManifest(
            @NonNull XmlDocument document, @NonNull MergingReport.Builder mergingReport)
            throws MergeFailureException {
        Pair<Document, Boolean> clonedDocument =
                document.cloneAndTransform(
                        PlaceholderEncoder::encode, ManifestMerger2::isNavGraphs);
        boolean isUpdated = clonedDocument.getSecond();
        mergingReport.setAaptSafeManifestUnchanged(!isUpdated);
        if (isUpdated) {
            mergingReport.setMergedDocument(AAPT_SAFE, prettyPrint(clonedDocument.getFirst()));
        }
    }

    /**
     * Function checks whether the current node is {@link SdkConstants#TAG_NAV_GRAPH}.
     *
     * @param node the element to check
     */
    private static boolean isNavGraphs(@NonNull Node node) {
        return (node instanceof Element)
                && SdkConstants.TAG_NAV_GRAPH.equals(((Element) node).getTagName());
    }

    /**
     * Set android:testOnly="true" to ensure APK will be rejected by the Play store.
     *
     * @param document the document for which the testOnly attribute should be set to true.
     */
    private static void addTestOnlyAttribute(@NonNull XmlDocument document) {
        XmlElement manifest = document.getRootNode();
        manifest.applyToFirstChildElementOfType(
                ManifestModel.NodeTypes.APPLICATION,
                application ->
                        setAndroidAttribute(
                                application, SdkConstants.ATTR_TEST_ONLY, SdkConstants.VALUE_TRUE));
    }

    /**
     * Set android:debuggable="true"
     *
     * @param document the document for which the debuggable attribute should be set to true.
     */
    private static void addDebuggableAttribute(@NonNull XmlDocument document) {
        XmlElement manifest = document.getRootNode();
        manifest.applyToFirstChildElementOfType(
                ManifestModel.NodeTypes.APPLICATION,
                application ->
                        setAndroidAttribute(
                                application,
                                SdkConstants.ATTR_DEBUGGABLE,
                                SdkConstants.VALUE_TRUE));
    }

    /**
     * Adds android:name="{multiDexApplicationName}" if there is no value specified for that field.
     *
     * @param document the document for which the name attribute might be set.
     * @param multiDexApplicationName the FQCN of MultiDexApplication
     */
    private static void addMultiDexApplicationIfNoName(
            @NonNull XmlDocument document, @NonNull String multiDexApplicationName) {
        XmlElement manifest = document.getRootNode();
        manifest.applyToFirstChildElementOfType(
                ManifestModel.NodeTypes.APPLICATION,
                application ->
                        setAndroidAttributeIfMissing(
                                application, ATTR_NAME, multiDexApplicationName));
    }

    /**
     * Set the {@code featureSplit} attribute to {@code featureName} for the manifest element.
     *
     * @param document the document whose attributes are changed
     * @param featureName the feature name of this feature subproject.
     */
    private static void addFeatureSplitAttribute(
            @NonNull XmlDocument document, @NonNull String featureName) {
        XmlElement manifest = document.getRootNode();
        if (manifest == null) {
            return;
        }

        String attributeName = SdkConstants.ATTR_FEATURE_SPLIT;
        manifest.setAttribute(attributeName, featureName);
    }

    /**
     * Set android:localeConfig to a generated resource. Produces an error if a user set locale
     * config is present.
     *
     * @param document the document for which the localeConfig attribute should be set.
     * @param configLocation the string to set the locale config to.
     */
    private void addLocaleConfig(@NonNull XmlDocument document, @NonNull String configLocation) {
        XmlElement manifest = document.getRootNode();
        manifest.applyToFirstChildElementOfType(
                ManifestModel.NodeTypes.APPLICATION,
                application -> {
                    if (application
                            .getXml()
                            .hasAttributeNS(
                                    SdkConstants.ANDROID_URI, SdkConstants.ATTR_LOCALE_CONFIG)) {
                        String message =
                                "Locale config generation was requested but user locale config is present in manifest. "
                                        + "See https://developer.android.com/r/studio-ui/build/automatic-per-app-languages";
                        mLogger.error(null, message);
                        throw new RuntimeException(message);
                    } else {
                        setAndroidAttribute(
                                application, SdkConstants.ATTR_LOCALE_CONFIG, configLocation);
                    }
                });
    }

    /**
     * Set the "android:splitName" attribute to {@code featureName} for every {@code activity},
     * {@code service} and {@code provider} element.
     *
     * @param document the document whose attributes are changed
     * @param featureName the value all of the changed attributes are set to
     */
    private static void adjustInstantAppFeatureSplitInfo(
            @NonNull XmlDocument document, @NonNull String featureName) {
        XmlElement manifest = document.getRootNode();
        if (manifest == null) {
            return;
        }

        manifest.applyToFirstChildElementOfType(
                ManifestModel.NodeTypes.APPLICATION,
                application -> {
                    List<ManifestModel.NodeTypes> elementNamesToUpdate =
                            Arrays.asList(
                                    ManifestModel.NodeTypes.ACTIVITY,
                                    ManifestModel.NodeTypes.SERVICE,
                                    ManifestModel.NodeTypes.PROVIDER);
                    for (ManifestModel.NodeTypes nodeType : elementNamesToUpdate) {
                        for (XmlElement elementToUpdate : application.getAllNodesByType(nodeType)) {
                            setAndroidAttribute(
                                    elementToUpdate, SdkConstants.ATTR_SPLIT_NAME, featureName);
                        }
                    }
                });
    }

    /**
     * Set an android namespaced attribute for the manifest element.
     *
     * @param document the document whose attributes will be modified
     * @param attribute the new attribute to be set
     * @param value the new value of the attribute
     * @return the previous value of the attribute or null if the attribute was not set.
     */
    public static String setManifestAndroidAttribute(
            @NonNull XmlDocument document, @NonNull String attribute, @NonNull String value) {
        XmlElement manifest = document.getRootNode();
        if (manifest == null) {
            return null;
        }
        String previousValue =
                manifest.getXml().hasAttributeNS(SdkConstants.ANDROID_URI, attribute)
                        ? manifest.getXml().getAttributeNS(SdkConstants.ANDROID_URI, attribute)
                        : null;
        setAndroidAttribute(manifest, attribute, value);
        return previousValue;
    }

    /**
     * Adds internet permission to document if not already present.
     *
     * @param document the document which gets edited if necessary.
     */
    private static void addInternetPermission(@NonNull XmlDocument document) {
        String permission = "android.permission.INTERNET";
        XmlElement manifest = document.getRootNode();
        ImmutableList<Element> usesPermissions =
                getChildElementsByName(manifest.getXml(), SdkConstants.TAG_USES_PERMISSION);
        for (Element usesPermission : usesPermissions) {
            if (permission.equals(
                    usesPermission.getAttributeNS(SdkConstants.ANDROID_URI, ATTR_NAME))) {
                return;
            }
        }
        XmlElement uses =
                new XmlElement(
                        document.getXml().createElement(SdkConstants.TAG_USES_PERMISSION),
                        document);
        // Add the node to the document before setting the attribute to make sure
        // the namespace prefix is found correctly.
        document.getRootNode().appendChild(uses);
        setAndroidAttribute(uses, ATTR_NAME, permission);
    }

    /**
     * Adds <uses-split> tags for feature-on-feature dependencies.
     *
     * @param dependencyFeatureNames the names of feature modules on which this depends, if any.
     */
    private static void addUsesSplitTagsForDependencies(
            @NonNull XmlDocument document, ImmutableList<String> dependencyFeatureNames) {
        XmlElement manifest = document.getRootNode();

        for (String usedSplitName : dependencyFeatureNames) {
            XmlElement usesSplit =
                    new XmlElement(
                            document.getXml().createElement(SdkConstants.TAG_USES_SPLIT), document);
            setAndroidAttribute(usesSplit, ATTR_NAME, usedSplitName);
            manifest.appendChild(usesSplit.getXml());
        }
    }

    /**
     * Adds <application> tag if missing as it required by package manager in R and above..
     *
     * @param document the loaded manifest file
     */
    private static void optionalAddApplicationTagIfMissing(@NonNull XmlDocument document) {
        XmlElement manifest = document.getRootNode();

        if (manifest.getXml().getElementsByTagName(SdkConstants.TAG_APPLICATION).getLength() > 0)
            return;

        Element application = document.getXml().createElement(SdkConstants.TAG_APPLICATION);
        manifest.appendChild(application);
    }

    /**
     * Set an Android-namespaced XML attribute on the given node.
     *
     * @param node Node in which to set the attribute; must be part of a document
     * @param localName Non-prefixed attribute name
     * @param value value of the attribute
     */
    public static void setAndroidAttribute(XmlElement node, String localName, String value) {
        String prefix =
                XmlUtils.lookupNamespacePrefix(
                        node.getXml(),
                        SdkConstants.ANDROID_URI,
                        SdkConstants.ANDROID_NS_NAME,
                        true);
        node.setAttributeNS(SdkConstants.ANDROID_URI, prefix + ":" + localName, value);
    }

    /**
     * Set an Android-namespaced XML attribute on the given node, if that attribute is missing.
     *
     * @param node Node in which to set the attribute; must be part of a document
     * @param localName Non-prefixed attribute name
     * @param value value of the attribute
     * @return whether the attribute was set (i.e., whether it was missing previously)
     */
    static boolean setAndroidAttributeIfMissing(XmlElement node, String localName, String value) {
        if (!node.getXml().hasAttributeNS(SdkConstants.ANDROID_URI, localName)) {
            setAndroidAttribute(node, localName, value);
            return true;
        }
        return false;
    }

    /**
     * Returns a list of elements which are the immediate children of the given element and have the
     * given name.
     *
     * @param element the immediate parent of any elements in the returned list
     * @param name the name of any elements in the returned list
     * @return the list (possibly empty) of children elements with the given name
     */
    @NonNull
    public static ImmutableList<Element> getChildElementsByName(
            @NonNull Element element, @NonNull String name) {
        ImmutableList.Builder<Element> childListBuilder = ImmutableList.builder();
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode instanceof Element && name.equals(childNode.getNodeName())) {
                childListBuilder.add((Element) childNode);
            }
        }
        return childListBuilder.build();
    }

    /** Returns a pretty string representation of the document. */
    @NonNull
    private static String prettyPrint(Document document) {
        return XmlPrettyPrinter.prettyPrint(
                document,
                XmlFormatPreferences.defaults(),
                XmlFormatStyle.get(document.getDocumentElement()),
                null, /* endOfLineSeparator */
                false /* endWithNewLine */);
    }

    /**
     * Returns the {@link FileStreamProvider} used by this manifest merger. Use this to read files
     * if you need to access the content of a {@link XmlDocument}.
     */
    @SuppressWarnings("unused") // Allow future library usage, if necessary
    @NonNull
    public FileStreamProvider getFileStreamProvider() {
        return mFileStreamProvider;
    }

    /**
     * Creates the merging report file.
     * @param mergingReport the merging activities report to serialize.
     */
    private void writeReport(@NonNull MergingReport mergingReport) {
        FileWriter fileWriter = null;
        try {
            if (!mReportFile.isPresent()
                    || !mReportFile.get().getParentFile().exists()
                            && !mReportFile.get().getParentFile().mkdirs()) {
                mLogger.warning(String.format(
                        "Cannot create %1$s manifest merger report file,"
                                + "build will continue but merging activities "
                                + "will not be documented",
                        mReportFile.get().getAbsolutePath()));
            } else {
                fileWriter = new FileWriter(mReportFile.get());
                mergingReport.getActions().log(fileWriter);
            }
        } catch (IOException e) {
            mLogger.warning(String.format(
                    "Error '%1$s' while writing the merger report file, "
                            + "build can continue but merging activities "
                            + "will not be documented ",
                    e.getMessage()));
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    mLogger.warning(String.format(
                            "Error '%1$s' while closing the merger report file, "
                                    + "build can continue but merging activities "
                                    + "will not be documented ",
                            e.getMessage()));
                }
            }
        }
    }

    /**
     * shorten recursively all attributes that are package dependent of the passed nodes and all its
     * child nodes.
     *
     * @param namespace the namespace to search for and extract.
     * @param xmlElement the xml element to process recursively.
     */
    private static void extractFqcns(@NonNull String namespace, @NonNull XmlElement xmlElement) {
        for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) {
            if (xmlAttribute.getModel() != null && xmlAttribute.getModel().isPackageDependent()) {
                String value = xmlAttribute.getValue();
                if (value.startsWith(namespace + ".")) {
                    xmlAttribute.setValue(value.substring(namespace.length()));
                }
            }
        }
        for (XmlElement child : xmlElement.getMergeableElements()) {
            extractFqcns(namespace, child);
        }
    }

    /**
     * Load an xml file and perform placeholder substitution
     *
     * @param manifestInfo the android manifest information like if it is a library, an overlay or a
     *     main manifest file.
     * @param selectors all the libraries selectors
     * @param mergingReportBuilder the merging report to store events and errors.
     * @param namespace the namespace to use to create or shorten fully qualified class names in the
     *     manifest; if null, we use the manifest's original package attribute, if present.
     * @return a loaded manifest info.
     * @throws MergeFailureException if the merging cannot be completed successfully.
     */
    @NonNull
    private LoadedManifestInfo load(
            @NonNull ManifestInfo manifestInfo,
            @NonNull KeyResolver<String> selectors,
            @NonNull MergingReport.Builder mergingReportBuilder,
            @Nullable String namespace)
            throws MergeFailureException {

        boolean rewriteNamespaces =
                mOptionalFeatures.contains(Invoker.Feature.FULLY_NAMESPACE_LOCAL_RESOURCES);

        File xmlFile = manifestInfo.mLocation;
        XmlDocument xmlDocument;
        try {
            InputStream inputStream = mFileStreamProvider.getInputStream(xmlFile);
            xmlDocument =
                    XmlLoader.load(
                            selectors,
                            mSystemPropertyResolver,
                            manifestInfo.mName,
                            xmlFile,
                            inputStream,
                            manifestInfo.getType(),
                            namespace,
                            mModel,
                            rewriteNamespaces);
        } catch (Exception e) {
            throw new MergeFailureException("Error parsing " + xmlFile.getAbsolutePath(), e);
        }

        MergingReport.Builder builder =
                manifestInfo.getType() == XmlDocument.Type.MAIN
                        ? mergingReportBuilder
                        : new MergingReport.Builder(mergingReportBuilder.getLogger());

        // perform place holder substitution, this is necessary to do so early in case placeholders
        // are used in key attributes.
        MergingReport.Record.Severity severity =
                !mMergeType.isFullPlaceholderSubstitutionRequired()
                        ? MergingReport.Record.Severity.INFO
                        : MergingReport.Record.Severity.ERROR;
        performPlaceHolderSubstitution(
                xmlDocument,
                xmlDocument.getPackage().map(XmlAttribute::getValue).orElse(null),
                builder,
                severity);

        builder.getActionRecorder().recordAddedNodeAction(xmlDocument.getRootNode(), false);

        mProcessCancellationChecker.check();

        return new LoadedManifestInfo(manifestInfo, xmlDocument);
    }

    private void performPlaceHolderSubstitution(
            @NonNull XmlDocument xmlDocument,
            @Nullable String originalMainManifestPackageName,
            @NonNull MergingReport.Builder mergingReportBuilder,
            @NonNull MergingReport.Record.Severity severity) {

        if (mOptionalFeatures.contains(Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)) {
            return;
        }

        // check for placeholders presence, switch first the packageName and applicationId if
        // it is not explicitly set, unless dealing with a LIBRARY MergeType.
        // In case of a LIBRARY MergeType, we don't replace packageName or applicationId,
        // unless they're already specified in mPlaceHolderValues.
        Map<String, Object> finalPlaceHolderValues = mPlaceHolderValues;
        if (!mPlaceHolderValues.containsKey(APPLICATION_ID)
                && mMergeType != MergeType.LIBRARY
                && originalMainManifestPackageName != null) {
            // add all existing placeholders except package name that will be swapped.
            ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
            for (Map.Entry<String, Object> entry : mPlaceHolderValues.entrySet()) {
                if (!entry.getKey().equals(PACKAGE_NAME)) {
                    builder.put(entry);
                }
            }
            builder.put(PACKAGE_NAME, originalMainManifestPackageName);
            builder.put(APPLICATION_ID, originalMainManifestPackageName);
            finalPlaceHolderValues = builder.build();
        }

        KeyBasedValueResolver<String> placeHolderValueResolver =
                new MapBasedKeyBasedValueResolver<>(finalPlaceHolderValues);
        PlaceholderHandler.visit(
                severity, xmlDocument, placeHolderValueResolver, mergingReportBuilder);
    }

    // merge the optionally existing xmlDocument with a lower priority xml file.
    private Optional<XmlDocument> merge(
            @Nullable XmlDocument xmlDocument,
            @NonNull LoadedManifestInfo lowerPriorityDocument,
            @NonNull MergingReport.Builder mergingReportBuilder) {

        boolean validateExtractNativeLibsFromSources =
                mSystemPropertyResolver.getValue(
                                ManifestSystemProperty.Application.EXTRACT_NATIVE_LIBS)
                        != null;

        Boolean higherPriorityExtractNativeLibsValue =
                PreValidator.getExtractNativeLibsValue(xmlDocument);
        boolean validateExtractNativeLibsFromDependencies =
                mOptionalFeatures.contains(
                                Invoker.Feature.VALIDATE_EXTRACT_NATIVE_LIBS_FROM_DEPENDENCIES)
                        && !Boolean.TRUE.equals(higherPriorityExtractNativeLibsValue);

        MergingReport.Result validationResult =
                PreValidator.validate(
                        mergingReportBuilder,
                        lowerPriorityDocument.getXmlDocument(),
                        validateExtractNativeLibsFromSources,
                        validateExtractNativeLibsFromDependencies);

        if (validationResult == MergingReport.Result.ERROR
                && !mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS)) {
            mergingReportBuilder.addMessage(
                    lowerPriorityDocument.getXmlDocument().getSourceFile(),
                    MergingReport.Record.Severity.ERROR,
                    "Validation failed, exiting");
            return Optional.empty();
        }

        Optional<XmlDocument> result;
        if (xmlDocument != null) {
            result =
                    xmlDocument.merge(
                            lowerPriorityDocument.getXmlDocument(),
                            mergingReportBuilder,
                            !mOptionalFeatures.contains(
                                    Invoker.Feature.NO_IMPLICIT_PERMISSION_ADDITION),
                            mOptionalFeatures.contains(Invoker.Feature.DISABLE_MINSDKLIBRARY_CHECK),
                            mOptionalFeatures.contains(Invoker.Feature.KEEP_GOING_AFTER_ERRORS));
        } else {
            // exhaustiveSearch is true in recordAddedNodeAction() below because some of this
            // manifest's nodes might have already been recorded from the loading of
            // the main manifest, but we want to record any unrecorded descendants.
            // e.g., if the main manifest did not contain any meta-data nodes below its
            // application node, we still want to record the addition of any such
            // meta-data nodes this manifest contains.
            mergingReportBuilder
                    .getActionRecorder()
                    .recordAddedNodeAction(
                            lowerPriorityDocument.getXmlDocument().getRootNode(), true);
            result = Optional.of(lowerPriorityDocument.getXmlDocument());
        }

        // if requested, dump each intermediary merging stage into the report.
        if (mOptionalFeatures.contains(Invoker.Feature.KEEP_INTERMEDIARY_STAGES)
                && result.isPresent()) {
            mergingReportBuilder.addMergingStage(result.get().prettyPrint());
        }

        return result;
    }

    private List<LoadedManifestInfo> loadLibraries(
            @NonNull SelectorResolver selectors,
            @NonNull MergingReport.Builder mergingReportBuilder,
            @Nullable String originalMainManifestPackageName)
            throws MergeFailureException {

        ImmutableList.Builder<LoadedManifestInfo> loadedLibraryDocuments = ImmutableList.builder();

        for (Pair<String, File> libraryFile : Sets.newLinkedHashSet(mLibraryFiles)) {
            mLogger.verbose("Loading library manifest " + libraryFile.getSecond().getPath());
            ManifestInfo manifestInfo =
                    new ManifestInfo(
                            libraryFile.getFirst(),
                            libraryFile.getSecond(),
                            XmlDocument.Type.LIBRARY);
            File xmlFile = manifestInfo.mLocation;
            XmlDocument libraryDocument;
            try {
                Optional<Document> document =
                        Optional.ofNullable(mManifestDocumentProvider)
                                .flatMap(provider -> provider.getManifestDocument(xmlFile));
                if (document.isPresent()) {
                    libraryDocument =
                            XmlLoader.load(
                                    document.get(),
                                    selectors,
                                    mSystemPropertyResolver,
                                    manifestInfo.mName,
                                    xmlFile,
                                    XmlDocument.Type.LIBRARY,
                                    null, /* namespace */
                                    mModel,
                                    false);
                } else {
                    libraryDocument =
                            XmlLoader.load(
                                    selectors,
                                    mSystemPropertyResolver,
                                    manifestInfo.mName,
                                    xmlFile,
                                    mFileStreamProvider.getInputStream(xmlFile),
                                    XmlDocument.Type.LIBRARY,
                                    null, /* namespace */
                                    mModel,
                                    false);
                }
            } catch (Exception e) {
                throw new MergeFailureException(e);
            }
            // extract the package name...
            String libraryNamespace = libraryDocument.getNamespace();
            // save it in the selector instance.
            if (!Strings.isNullOrEmpty(libraryNamespace)) {
                selectors.addSelector(libraryNamespace, libraryFile.getFirst());
            }

            // perform placeholder substitution, this is useful when the library is using
            // a placeholder in a key element, we however do not need to record these
            // substitutions so feed it with a fake merging report.
            MergingReport.Builder builder =
                    new MergingReport.Builder(mergingReportBuilder.getLogger());
            builder.getActionRecorder().recordAddedNodeAction(libraryDocument.getRootNode(), false);
            performPlaceHolderSubstitution(
                    libraryDocument,
                    originalMainManifestPackageName,
                    builder,
                    MergingReport.Record.Severity.INFO);
            if (builder.hasErrors()) {
                // we log the errors but continue, in case the error is of no consequence
                // to the application consuming the library.
                builder.build().log(mLogger);
            }

            LoadedManifestInfo info = new LoadedManifestInfo(manifestInfo, libraryDocument);

            loadedLibraryDocuments.add(info);

            mProcessCancellationChecker.check();
        }

        return loadedLibraryDocuments.build();
    }

    /**
     * Checks whether all manifests have unique namespaces. If the strict mode is enabled it will
     * result in an error for namespace collisions, otherwise it will result in a warning.
     */
    private static void checkUniqueNamespaces(
            @NonNull LoadedManifestInfo loadedMainManifestInfo,
            @NonNull List<LoadedManifestInfo> libraries,
            @NonNull List<String> allowedNonUniqueNamespaces,
            @NonNull MergingReport.Builder mergingReportBuilder,
            boolean strictUniqueNamespaceCheck) {
        Multimap<String, LoadedManifestInfo> uniqueNamespaceMap = ArrayListMultimap.create();

        if (!Strings.isNullOrEmpty(loadedMainManifestInfo.getNamespace())) {
            uniqueNamespaceMap.put(loadedMainManifestInfo.getNamespace(), loadedMainManifestInfo);
        }

        libraries.stream()
                .filter(l -> !Strings.isNullOrEmpty(l.getNamespace()))
                .forEach(l -> uniqueNamespaceMap.put(l.getNamespace(), l));

        uniqueNamespaceMap.asMap().entrySet().stream()
                .filter(e -> e.getValue().size() > 1)
                .forEach(
                        e -> {
                            Collection<String> offendingTargets =
                                    e.getValue().stream()
                                            .map(ManifestInfo::getName)
                                            .collect(Collectors.toList());
                            String repeatedNamespaceMessage =
                                    "Namespace '"
                                            + e.getKey()
                                            + "' is used in multiple modules and/or libraries: "
                                            + Joiner.on(", ").join(offendingTargets)
                                            + ". Please ensure that all modules and libraries have a "
                                            + "unique namespace."
                                            + " For more information, See https://developer.android.com/studio/build/configure-app-module#set-namespace";
                            // We know that there is at least one because of the
                            // filter check.
                            LoadedManifestInfo info = e.getValue().stream().findFirst().get();
                            // Report only once per error, since the error message contain the path
                            // to all manifests with the repeated namespace.

                            mergingReportBuilder.addMessage(
                                    info.getXmlDocument().getSourceFile(),
                                    getNonUniqueNamespaceSeverity(
                                            allowedNonUniqueNamespaces,
                                            e.getKey(),
                                            strictUniqueNamespaceCheck),
                                    repeatedNamespaceMessage);
                        });
    }

    /** Returns the correct logging severity for a clashing namespace. */
    private static MergingReport.Record.Severity getNonUniqueNamespaceSeverity(
            @NonNull List<String> allowedNonUniqueNamespaces,
            String namespace,
            boolean strictMode) {
        // If we've allowed a library namespace to be non-unique, only report in info.
        if (allowedNonUniqueNamespaces.contains(namespace))
            return MergingReport.Record.Severity.INFO;

        return strictMode
                ? MergingReport.Record.Severity.ERROR
                : MergingReport.Record.Severity.WARNING;
    }

    /**
     * Creates a new {@link Invoker} instance to invoke the merging tool to merge manifest files for
     * an application.
     *
     * @param mainManifestFile application main manifest file.
     * @param logger the logger interface to use.
     * @return an {@link Invoker} instance that will allow further customization and trigger the
     *     merging tool.
     */
    @NonNull
    public static Invoker newMerger(
            @NonNull File mainManifestFile, @NonNull ILogger logger, @NonNull MergeType mergeType) {
        return new Invoker(mainManifestFile, logger, mergeType, XmlDocument.Type.MAIN);
    }

    /**
     * Defines the merging type expected from the tool.
     */
    public enum MergeType {
        /**
         * Application merging type is used when packaging an application with a set of imported
         * libraries. The resulting merged android manifest is final and is not expected to be
         * imported in another application.
         */
        APPLICATION(true, true),

        /**
         * Library merging type is used when packaging a library. The resulting android manifest
         * file will not merge in all the imported libraries this library depends on. Also the tools
         * annotations will not be removed as they can be useful when later importing the resulting
         * merged android manifest into an application.
         */
        LIBRARY(false, false),

        /**
         * Fused library merging type is similar to application manifest merging as library
         * manifests are merged, however tools annotations are kept for application merging,
         * placeholder replacements are non-exhaustive and final validation is not performed.
         * merging.
         */
        FUSED_LIBRARY(false, false),

        /**
         * Privacy sandbox library merging similar to fused library merging except that resulting
         * manifest is expected to be processed by aapt2 and shipped into an .asb file to the Play
         * Store.
         */
        PRIVACY_SANDOX_LIBRARY(true, true);

        private final boolean isKeepToolsAttributeRequired;
        private final boolean isFullPlaceholderSubstitutionRequired;

        /**
         * Return true if the localName attribute should be removed from the node declaring it.
         *
         * @param localName the XML no namespace local name
         */
        public boolean isKeepToolsAttributeRequired(String localName, String value) {
            return isKeepToolsAttributeRequired
                    && !(NodeOperationType.REQUIRED_BY_PRIVACY_SANDBOX_SDK_ATTRIBUTE_NAME.equals(
                                    localName)
                            && value.equals(SdkConstants.VALUE_TRUE));
        }

        public boolean isFullPlaceholderSubstitutionRequired() {
            return isFullPlaceholderSubstitutionRequired;
        }

        MergeType(boolean isKeepToolsAttributeRequired, boolean isFullPlaceholderSubstitutionRequired) {
            this.isKeepToolsAttributeRequired = isKeepToolsAttributeRequired;
            this.isFullPlaceholderSubstitutionRequired = isFullPlaceholderSubstitutionRequired;
        }
    }

    /**
     * Defines a property that can add or override itself into an XML document.
     */
    public interface AutoAddingProperty {

        /**
         * Add itself (possibly just override the current value) with the passed value
         * @param actionRecorder to record actions.
         * @param document the xml document to add itself to.
         * @param value the value to set of this property.
         */
        void addTo(@NonNull ActionRecorder actionRecorder,
                @NonNull XmlDocument document,
                @NonNull String value);
    }

    /**
     * Perform {@link ManifestSystemProperty} injection.
     *
     * @param mergingReport to log actions and errors.
     * @param xmlDocument the xml document to inject into.
     */
    protected void performSystemPropertiesInjection(
            @NonNull MergingReport.Builder mergingReport, @NonNull XmlDocument xmlDocument) {
        for (ManifestSystemProperty manifestSystemProperty : ManifestSystemProperty.getValues()) {
            String propertyOverride = mSystemPropertyResolver.getValue(manifestSystemProperty);
            if (propertyOverride != null) {
                manifestSystemProperty.addTo(
                        mergingReport.getActionRecorder(), xmlDocument, propertyOverride);
            }
        }
    }

    /**
     * A {@linkplain ManifestDocumentProvider} provides the merged manifest XML {@link Document}
     * instance for the project a given {@link File} belongs to.
     */
    public interface ManifestDocumentProvider {
        /**
         * Gets a Merged manifest XML document for the given file's project. Returns
         * Optional.empty() when the document is unavailable. ManifestMerger process falls back to
         * the {@link FileStreamProvider} when the document is not available.
         *
         * @param file the file handle
         * @return the contents of the file
         */
        Optional<Document> getManifestDocument(@NonNull File file);
    }

    /**
     * A {@linkplain FileStreamProvider} provides (buffered, if necessary) {@link InputStream}
     * instances for a given {@link File} handle. Consider providing a {@link
     * ManifestDocumentProvider} when manifest DOM is available.
     */
    public static class FileStreamProvider {
        /**
         * Creates a reader for the given file -- which may not necessarily read the contents of the
         * file on disk. For example, in the IDE, the client will map the file handle to a document in
         * the editor, and read the current contents of that editor whether or not it has been saved.
         * <p>
         * This method is responsible for providing its own buffering, if necessary (e.g. when
         * reading from disk, make sure you wrap the file stream in a buffering input stream.)
         *
         * @param file the file handle
         * @return the contents of the file
         * @throws FileNotFoundException if the file handle is invalid
         */
        protected InputStream getInputStream(@NonNull File file) throws IOException {
            return new BufferedInputStream(new FileInputStream(file));
        }
    }

    public interface ProcessCancellationChecker {
        /**
         * @throws com.intellij.openapi.progress.ProcessCanceledException if the progress indicator
         * associated with the current thread has been canceled.
         * @see com.intellij.openapi.progress.ProgressManager#checkCanceled()
         */
        void check();
    }

    private void checkExportedDeclaration(
            XmlDocument finalMergedDocument, MergingReport.Builder mergingReportBuilder) {
        String targetSdkVersion = finalMergedDocument.getTargetSdkVersion(mergingReportBuilder);
        int targetSdkApi =
                Character.isDigit(targetSdkVersion.charAt(0))
                        ? Integer.parseInt(targetSdkVersion)
                        : SdkVersionInfo.getApiByPreviewName(targetSdkVersion, true);
        if (targetSdkApi > 30) {
            Optional<XmlElement> element =
                    finalMergedDocument
                            .getRootNode()
                            .getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null);
            if (!element.isPresent()) {
                return;
            }
            XmlElement applicationElement = element.get();

            checkIfExportedIsNeeded(
                    applicationElement.getAllNodesByType(ManifestModel.NodeTypes.ACTIVITY),
                    mergingReportBuilder);

            checkIfExportedIsNeeded(
                    applicationElement.getAllNodesByType(ManifestModel.NodeTypes.SERVICE),
                    mergingReportBuilder);

            checkIfExportedIsNeeded(
                    applicationElement.getAllNodesByType(ManifestModel.NodeTypes.RECEIVER),
                    mergingReportBuilder);
        }
    }

    private void checkIfExportedIsNeeded(
            List<XmlElement> list, MergingReport.Builder mergingReportBuilder) {
        for (XmlElement element : list) {
            if (element.getAllNodesByType(ManifestModel.NodeTypes.INTENT_FILTER).size() > 0
                    && !element.getXml()
                            .hasAttributeNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_EXPORTED)) {
                mergingReportBuilder.addMessage(
                        element,
                        MergingReport.Record.Severity.ERROR,
                        String.format(
                                "android:exported needs to be explicitly specified for element <%s>. Apps targeting Android 12 and higher are required to specify an explicit value "
                                        + "for `android:exported` when the corresponding component has an intent filter defined. "
                                        + "See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.",
                                element.getId()));
            }
        }
    }

    private void stripTargetSdk(@NonNull XmlDocument xmlDocument) {
        Optional<XmlElement> usesSdk = xmlDocument.getByTypeAndKey(USES_SDK, null);
        usesSdk.ifPresent(
                xmlElement ->
                        xmlElement
                                .removeAttributeNS(
                                        SdkConstants.ANDROID_URI,
                                        SdkConstants.ATTR_TARGET_SDK_VERSION));
    }

    /**
     * This class will hold all invocation parameters for the manifest merging tool.
     *
     * <p>There are broadly three types of input to the merging tool :
     *
     * <ul>
     *   <li>Build types and flavors overriding manifests
     *   <li>Application main manifest
     *   <li>Library manifest files
     * </ul>
     *
     * Only the main manifest file is a mandatory parameter.
     *
     * <p>High level description of the merging will be as follow :
     *
     * <ol>
     *   <li>Build type and flavors will be merged first in the order they were added. Highest
     *       priority file added first, lowest added last.
     *   <li>Resulting document is merged with lower priority application main manifest file.
     *   <li>Resulting document is merged with each library file manifest file in the order they
     *       were added. Highest priority added first, lowest added last.
     *   <li>Resulting document is returned as results of the merging process.
     * </ol>
     */
    public static class Invoker {

        protected final File mMainManifestFile;

        protected final ImmutableMap.Builder<ManifestSystemProperty, Object>
                mSystemProperties = new ImmutableMap.Builder<>();

        @NonNull
        protected final ILogger mLogger;

        @NonNull
        protected final ImmutableMap.Builder<String, Object> mPlaceholders =
                new ImmutableMap.Builder<>();

        @NonNull
        private final ImmutableList.Builder<Pair<String, File>> mLibraryFilesBuilder =
                new ImmutableList.Builder<>();

        @NonNull
        private final ImmutableList.Builder<File> mFlavorsAndBuildTypeFiles =
                new ImmutableList.Builder<>();

        @NonNull
        private final ImmutableList.Builder<Feature> mFeaturesBuilder =
                new ImmutableList.Builder<>();

        @NonNull
        private final MergeType mMergeType;
        @NonNull private  XmlDocument.Type mDocumentType;
        @Nullable private File mReportFile;

        @Nullable
        private FileStreamProvider mFileStreamProvider;

        @Nullable private ManifestDocumentProvider mManifestDocumentProvider;

        @Nullable
        private ProcessCancellationChecker mProcessCancellationChecker;

        @NonNull private String mFeatureName;

        @Nullable private String mNamespace;

        @NonNull
        private final ImmutableList.Builder<File> mNavigationFilesBuilder =
                new ImmutableList.Builder<>();

        @NonNull
        private final ImmutableList.Builder<File> mNavigationJsonsBuilder =
                new ImmutableList.Builder<>();

        @NonNull
        private final ImmutableList.Builder<String> mDependencyFetureNamesBuilder =
                new ImmutableList.Builder<>();

        @NonNull
        private final ImmutableList.Builder<String> mAllowedNonUniqueNamespaces =
                new ImmutableList.Builder<>();

        @Nullable private String mGeneratedLocaleConfigAttribute = null;

        /**
         * Sets a value for a {@link ManifestSystemProperty}
         *
         * @param override the property to set
         * @param value the value for the property
         * @return itself.
         */
        @NonNull
        public Invoker setOverride(
                @NonNull ManifestSystemProperty override, @NonNull String value) {
            mSystemProperties.put(override, value);
            return this;
        }

        /**
         * Adds placeholders names and associated values for substitution.
         * @return itself.
         */
        @NonNull
        public Invoker setPlaceHolderValues(@NonNull Map<String, Object> keyValuePairs) {
            mPlaceholders.putAll(keyValuePairs);
            return this;
        }

        /**
         * Adds a new placeholder name and value for substitution.
         * @return itself.
         */
        @NonNull
        public Invoker setPlaceHolderValue(@NonNull String placeHolderName, @NonNull String value) {
            mPlaceholders.put(placeHolderName, value);
            return this;
        }

        /**
         * Optional behavior of the merging tool can be turned on by setting these Feature.
         */
        public enum Feature {

            /**
             * Keep all intermediary merged files during the merging process. This is particularly
             * useful for debugging/tracing purposes.
             */
            KEEP_INTERMEDIARY_STAGES,

            /**
             * When logging file names, use {@link File#getName()} rather than {@link
             * File#getPath()}
             */
            PRINT_SIMPLE_FILENAMES,

            /**
             * Perform a sweep after all merging activities to remove all fully qualified class
             * names and replace them with the equivalent short version.
             */
            EXTRACT_FQCNS,

            /**
             * Perform a sweep after all merging activities to remove all tools: decorations.
             */
            REMOVE_TOOLS_DECLARATIONS,

            /**
             * Do no perform placeholders replacement.
             */
            NO_PLACEHOLDER_REPLACEMENT,

            /**
             * Encode unresolved placeholders to be AAPT friendly.
             */
            MAKE_AAPT_SAFE,

            /**
             * Clients will not request the blame history
             */
            SKIP_BLAME,

            /**
             * Clients will only request the merged XML documents, not XML pretty printed documents
             */
            SKIP_XML_STRING,

            /**
             * Add android:testOnly="true" attribute to prevent APK from being uploaded to Play
             * store.
             */
            TEST_ONLY,

            /**
             * Do not perform implicit permission addition.
             */
            NO_IMPLICIT_PERMISSION_ADDITION,

            /** Perform Studio advanced profiling manifest modifications */
            ADVANCED_PROFILING,

            /** Mark this application as a feature split */
            ADD_DYNAMIC_FEATURE_ATTRIBUTES,

            /** Set the android:debuggable flag to the application. */
            DEBUGGABLE,

            /**
             * When there are attribute value conflicts, automatically pick the higher priority
             * value.
             *
             * <p>This is for example used in the IDE when we need to merge a new manifest template
             * into an existing one and we don't want to abort the merge.
             *
             * <p>(This will log a warning.)
             */
            HANDLE_VALUE_CONFLICTS_AUTOMATICALLY,

            /**
             * Adds the AndroidX name of {@link AndroidXConstants#MULTI_DEX_APPLICATION} as
             * application name if none is specified. Used for legacy multidex.
             */
            ADD_ANDROIDX_MULTIDEX_APPLICATION_IF_NO_NAME,

            /**
             * Adds the pre-AndroidX name of {@link AndroidXConstants#MULTI_DEX_APPLICATION} as
             * application name if none is specified. Used for legacy multidex.
             */
            ADD_SUPPORT_MULTIDEX_APPLICATION_IF_NO_NAME,

            /** Rewrite local resource references with fully qualified namespace */
            FULLY_NAMESPACE_LOCAL_RESOURCES,

            /* Disables check for uniqueness of package names in dependencies manifests */
            DISABLE_PACKAGE_NAME_UNIQUENESS_CHECK,

            /** Enforce that dependencies manifests don't have duplicated package names. */
            ENFORCE_UNIQUE_PACKAGE_NAME,

            /** Unsafely disables minSdkVersion check in libraries. */
            DISABLE_MINSDKLIBRARY_CHECK,

            /**
             * Warn if the package attribute is present in the main manifest, or throw an exception
             * if it's present and not equal to the component's namespace.
             *
             * <p>This is used in AGP because users must migrate to the new namespace DSL.
             */
            CHECK_IF_PACKAGE_IN_MAIN_MANIFEST,

            /** Removes target SDK for library manifest */
            DISABLE_STRIP_LIBRARY_TARGET_SDK,

            /**
             * If set, merger will continue merging after any errors, allowing to surface errors in
             * the "merged manifest" editor view.
             */
            KEEP_GOING_AFTER_ERRORS,

            /**
             * Warn if the {@link SdkConstants#ATTR_EXTRACT_NATIVE_LIBS} attribute is set to true in
             * a dependency manifest but not set to true in the merged manifest.
             */
            VALIDATE_EXTRACT_NATIVE_LIBS_FROM_DEPENDENCIES
        }

        /**
         * Creates a new builder with the mandatory main manifest file.
         * @param mainManifestFile application main manifest file.
         * @param logger the logger interface to use.
         */
        private Invoker(
                @NonNull File mainManifestFile,
                @NonNull ILogger logger,
                @NonNull MergeType mergeType,
                @NonNull XmlDocument.Type documentType) {
            this.mMainManifestFile = Preconditions.checkNotNull(mainManifestFile);
            this.mLogger = logger;
            this.mMergeType = mergeType;
            this.mDocumentType = documentType;
            this.mFeatureName = "";
        }

        /**
         * Sets the file to use to write the merging report. If not called,
         * the merging process will not write a report.
         * @param mergeReport the file to write the report in.
         * @return itself.
         */
        @NonNull
        public Invoker setMergeReportFile(@Nullable File mergeReport) {
            mReportFile = mergeReport;
            return this;
        }

        /**
         * Add one library file manifest, will be added last in the list of library files which will
         * make the parameter the lowest priority library manifest file.
         * @param file the library manifest file to add.
         * @return itself.
         */
        @NonNull
        public Invoker addLibraryManifest(@NonNull File file) {
            addLibraryManifest(file.getName(), file);
            return this;
        }

        /**
         * Add one library file manifest, will be added last in the list of library files which will
         * make the parameter the lowest priority library manifest file.
         * @param file the library manifest file to add.
         * @param name the library name.
         * @return itself.
         */
        @NonNull
        public Invoker addLibraryManifest(@NonNull String name, @NonNull File file) {
            if (mMergeType == MergeType.LIBRARY) {
                throw new IllegalStateException(
                  "Cannot add library dependencies manifests when creating a library");
            }
            mLibraryFilesBuilder.add(Pair.of(name, file));
            return this;
        }

        /**
         * Sets library dependencies for this merging activity.
         * @param namesAndFiles the list of library dependencies.
         * @return itself.
         *
         * @deprecated use addLibraryManifest or addAndroidBundleManifests
         */
        @NonNull
        @Deprecated
        public Invoker addBundleManifests(@NonNull List<Pair<String, File>> namesAndFiles) {
            if (mMergeType == MergeType.LIBRARY && !namesAndFiles.isEmpty()) {
                throw new IllegalStateException(
                        "Cannot add library dependencies manifests when creating a library");
            }
            mLibraryFilesBuilder.addAll(namesAndFiles);
            return this;
        }

        /**
         * Sets manifest providers for this merging activity.
         * @param providers the list of manifest providers.
         * @return itself.
         */
        @NonNull
        public Invoker addManifestProviders(@NonNull Iterable<? extends ManifestProvider> providers) {
            for (ManifestProvider provider : providers) {
                mLibraryFilesBuilder.add(Pair.of(provider.getName(), provider.getManifest()));
            }
            return this;
        }

        /**
         * Add several library file manifests at then end of the list which will make them the
         * lowest priority manifest files. The relative priority between all the files passed as
         * parameters will be respected.
         * @param files library manifest files to add last.
         * @return itself.
         */
        @NonNull
        public Invoker addLibraryManifests(@NonNull File... files) {
            for (File file : files) {
                addLibraryManifest(file);
            }
            return this;
        }

        /**
         * Add a flavor or build type manifest file last in the list.
         * @param file build type or flavor manifest file
         * @return itself.
         */
        @NonNull
        public Invoker addFlavorAndBuildTypeManifest(@NonNull File file) {
            this.mFlavorsAndBuildTypeFiles.add(file);
            return this;
        }

        /**
         * Add several flavor or build type manifest files last in the list. Relative priorities
         * between the passed files as parameters will be respected.
         * @param files build type of flavor manifest files to add.
         * @return itself.
         */
        @NonNull
        public Invoker addFlavorAndBuildTypeManifests(File... files) {
            this.mFlavorsAndBuildTypeFiles.add(files);
            return this;
        }

        /**
         * Sets some optional features for the merge tool.
         *
         * @param features one to many features to set.
         * @return itself.
         */
        @NonNull
        public Invoker withFeatures(Feature...features) {
            mFeaturesBuilder.add(features);
            return this;
        }

        /**
         * Sets a file stream provider which allows the client of the manifest merger to provide
         * arbitrary content lookup for files. <p> NOTE: There should only be one.
         *
         * @param provider the provider to use
         * @return itself.
         */
        @NonNull
        public Invoker withFileStreamProvider(@Nullable FileStreamProvider provider) {
            assert mFileStreamProvider == null || provider == null;
            mFileStreamProvider = provider;
            return this;
        }

        /**
         * Sets a manifest XML document provider which allows the client of the manifest merger to
         * provide manifest DOM Document object lookup for given files.
         *
         * <p>NOTE: There should only be one.
         *
         * @param provider the provider to use
         * @return itself.
         */
        @NonNull
        public Invoker withManifestDocumentProvider(@Nullable ManifestDocumentProvider provider) {
            assert mManifestDocumentProvider == null || provider == null;
            mManifestDocumentProvider = provider;
            return this;
        }

        @NonNull
        public Invoker withProcessCancellationChecker(@NonNull ProcessCancellationChecker checker) {
            assert mProcessCancellationChecker == null;
            mProcessCancellationChecker = checker;
            return this;
        }

        /** Regular expression defining legal feature split name. */
        private static final Pattern FEATURE_NAME_PATTERN =
                Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9_]*");

        /**
         * Specify the feature name for feature merging.
         *
         * @param featureName the feature name to use.
         * @return itself.
         */
        @NonNull
        public Invoker setFeatureName(@Nullable String featureName) {
            if (featureName != null) {
                mFeatureName = featureName;
                if (!FEATURE_NAME_PATTERN.matcher(mFeatureName).matches()) {
                    throw new IllegalArgumentException(
                            "FeatureName must follow "
                                    + FEATURE_NAME_PATTERN.pattern()
                                    + " regex, found "
                                    + featureName);
                }
            }
            return this;
        }

        /**
         * Specify the module namespace.
         *
         * @param namespace the namespace, used to create or shorten fully qualified class names. If
         *     specified, this namespace will override the package name in the source manifest.
         * @return itself.
         */
        @NonNull
        public Invoker setNamespace(@NonNull String namespace) {
            mNamespace = namespace;
            return this;
        }

        /**
         * Add several navigation files last in the list. Relative priorities between the passed
         * files as parameters will be respected.
         *
         * @param files the navigation files to add.
         * @return itself.
         */
        @NonNull
        public Invoker addNavigationFiles(@NonNull Iterable<File> files) {
            this.mNavigationFilesBuilder.addAll(files);
            return this;
        }

        /**
         * Add several navigation.json files in the list.
         *
         * @param files the navigation.json files to add.
         * @return itself.
         */
        @NonNull
        public Invoker addNavigationJsons(@NonNull Iterable<File> files) {
            this.mNavigationJsonsBuilder.addAll(files);
            return this;
        }

        /**
         * Specify if the file being merged is an overlay (flavor). If not called, the merging
         * process will assume a master manifest merge. The master manifest needs to have a package
         * and some other mandatory fields like "uses-sdk", etc.
         *
         * @return itself.
         */
        @NonNull
        public Invoker asType(XmlDocument.Type type) {
            mDocumentType = type;
            return this;
        }

        /**
         * Specifies a list of feature modules on which this module will depend. This is only valid
         * for feature manifests.
         *
         * @param names the names of the dynamic features.
         * @return itself.
         */
        public Invoker addDependencyFeatureNames(@NonNull Iterable<String> names) {
            this.mDependencyFetureNamesBuilder.addAll(names);
            return this;
        }

        /**
         * Add a namespace that is allowed to appear in more than one library.
         *
         * @param namespace the namespace
         * @return itself
         */
        public Invoker addAllowedNonUniqueNamespace(String namespace) {
            this.mAllowedNonUniqueNamespaces.add(namespace);
            return this;
        }

        /**
         * Set the location of the generated locale config to add to the manifest.
         *
         * @param generatedLocaleConfigAttribute the attribute pointing to the generated locale
         *     config.
         * @return itself
         */
        public Invoker setGeneratedLocaleConfigAttribute(String generatedLocaleConfigAttribute) {
            this.mGeneratedLocaleConfigAttribute = generatedLocaleConfigAttribute;
            return this;
        }

        /**
         * Perform the merging and return the result.
         *
         * @return an instance of {@link MergingReport} that will give access to all the logging and
         *     merging records.
         *     <p>This method can be invoked several time and will re-do the file merges.
         * @throws MergeFailureException if the merging cannot be completed successfully.
         */
        @NonNull
        public MergingReport merge() throws MergeFailureException {

            // provide some free placeholders values.
            ImmutableMap<ManifestSystemProperty, Object> systemProperties =
                    mSystemProperties.build();
            if (systemProperties.containsKey(ManifestSystemProperty.Document.PACKAGE)) {
                // if the package is provided, make it available for placeholder replacement.
                mPlaceholders.put(
                        PACKAGE_NAME,
                        systemProperties.get(ManifestSystemProperty.Document.PACKAGE));
                // as well as applicationId since package system property overrides everything
                // but not when output is a library since only the final (application)
                // application Id should be used to replace libraries "applicationId" placeholders.
                if (mMergeType != MergeType.LIBRARY) {
                    mPlaceholders.put(
                            APPLICATION_ID,
                            systemProperties.get(ManifestSystemProperty.Document.PACKAGE));
                }
            }

            FileStreamProvider fileStreamProvider = mFileStreamProvider != null
                    ? mFileStreamProvider : new FileStreamProvider();
            ProcessCancellationChecker processCancellationChecker =
                    mProcessCancellationChecker != null ? mProcessCancellationChecker : () -> {};
            addAllowedNonUniqueNamespace("androidx.test"); // TODO(b/151171905)
            ManifestMerger2 manifestMerger =
                    new ManifestMerger2(
                            mLogger,
                            mMainManifestFile,
                            mLibraryFilesBuilder.build(),
                            mFlavorsAndBuildTypeFiles.build(),
                            mFeaturesBuilder.build(),
                            mPlaceholders.build(),
                            new MapBasedKeyBasedValueResolver<>(systemProperties),
                            mMergeType,
                            mDocumentType,
                            Optional.ofNullable(mReportFile),
                            mFeatureName,
                            mNamespace,
                            fileStreamProvider,
                            mManifestDocumentProvider,
                            processCancellationChecker,
                            mNavigationFilesBuilder.build(),
                            mNavigationJsonsBuilder.build(),
                            mDependencyFetureNamesBuilder.build(),
                            mAllowedNonUniqueNamespaces.build(),
                            mGeneratedLocaleConfigAttribute);

            return manifestMerger.merge();
        }
    }

    /**
     * Helper class for map based placeholders key value pairs.
     */
    public static class MapBasedKeyBasedValueResolver<T> implements KeyBasedValueResolver<T> {

        private final ImmutableMap<T, Object> keyValues;

        public MapBasedKeyBasedValueResolver(@NonNull Map<T, Object> keyValues) {
            this.keyValues = ImmutableMap.copyOf(keyValues);
        }

        @Nullable
        @Override
        public String getValue(@NonNull T key) {
            Object value = keyValues.get(key);
            return value == null ? null : value.toString();
        }
    }

    private static class ManifestInfo {

        private ManifestInfo(String name, File location, XmlDocument.Type type) {
            mName = name;
            mLocation = location;
            mType = type;
        }

        private final String mName;
        private final File mLocation;
        private final XmlDocument.Type mType;

        String getName() {
            return mName;
        }

        File getLocation() {
            return mLocation;
        }

        XmlDocument.Type getType() {
            return mType;
        }
    }

    private static class LoadedManifestInfo extends ManifestInfo {

        @NonNull private final XmlDocument mXmlDocument;

        private LoadedManifestInfo(@NonNull ManifestInfo manifestInfo,
                @NonNull XmlDocument xmlDocument) {
            super(manifestInfo.mName, manifestInfo.mLocation, manifestInfo.mType);
            mXmlDocument = xmlDocument;
        }

        @NonNull
        public XmlDocument getXmlDocument() {
            return mXmlDocument;
        }

        @Nullable
        public String getNamespace() {
            return mXmlDocument.getNamespace();
        }
    }

    /**
     * Implementation a {@link KeyResolver} capable of resolving all selectors value in the context
     * of the passed libraries to this merging activities.
     */
    public static class SelectorResolver implements KeyResolver<String> {

        private final Map<String, String> mSelectors = new HashMap<>();

        protected void addSelector(String key, String value) {
            mSelectors.put(key, value);
        }

        @Nullable
        @Override
        public String resolve(String key) {
            return mSelectors.get(key);
        }

        @NonNull
        @Override
        public Iterable<String> getKeys() {
            return mSelectors.keySet();
        }
    }

    // a wrapper exception to all sorts of failure exceptions that can be thrown during merging.
    public static class MergeFailureException extends Exception {

        protected MergeFailureException(String msg, Exception cause) {
            super(msg, cause);
        }

        protected MergeFailureException(Exception cause) {
            super(cause);
        }
    }
}