summaryrefslogtreecommitdiff
path: root/lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/Project.java
blob: 9349094bcdad268d4706c761b177221dc2f84b04 (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
/*
 * Copyright (C) 2011 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.tools.lint.detector.api;

import static com.android.SdkConstants.ANDROIDX_APPCOMPAT_LIB_ARTIFACT;
import static com.android.SdkConstants.ANDROID_LIBRARY;
import static com.android.SdkConstants.ANDROID_LIBRARY_REFERENCE_FORMAT;
import static com.android.SdkConstants.ANDROID_MANIFEST_XML;
import static com.android.SdkConstants.ANDROID_URI;
import static com.android.SdkConstants.APPCOMPAT_LIB_ARTIFACT;
import static com.android.SdkConstants.ATTR_MIN_SDK_VERSION;
import static com.android.SdkConstants.ATTR_PACKAGE;
import static com.android.SdkConstants.ATTR_TARGET_SDK_VERSION;
import static com.android.SdkConstants.DOT_VERSIONS_DOT_TOML;
import static com.android.SdkConstants.FD_GRADLE;
import static com.android.SdkConstants.FD_GRADLE_WRAPPER;
import static com.android.SdkConstants.FN_BUILD_GRADLE;
import static com.android.SdkConstants.FN_BUILD_GRADLE_KTS;
import static com.android.SdkConstants.FN_GRADLE_PROPERTIES;
import static com.android.SdkConstants.FN_GRADLE_WRAPPER_PROPERTIES;
import static com.android.SdkConstants.FN_LOCAL_PROPERTIES;
import static com.android.SdkConstants.FN_PROJECT_PROGUARD_FILE;
import static com.android.SdkConstants.FN_SETTINGS_GRADLE;
import static com.android.SdkConstants.FN_SETTINGS_GRADLE_KTS;
import static com.android.SdkConstants.OLD_PROGUARD_FILE;
import static com.android.SdkConstants.PROGUARD_CONFIG;
import static com.android.SdkConstants.PROJECT_PROPERTIES;
import static com.android.SdkConstants.TAG_USES_SDK;
import static com.android.SdkConstants.VALUE_FALSE;
import static com.android.SdkConstants.VALUE_TRUE;
import static com.android.sdklib.AndroidTargetHash.PLATFORM_HASH_PREFIX;
import static java.io.File.separator;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.rendering.api.ResourceNamespace;
import com.android.ide.common.repository.AgpVersion;
import com.android.ide.common.repository.ResourceVisibilityLookup;
import com.android.resources.Density;
import com.android.resources.ResourceFolderType;
import com.android.sdklib.AndroidTargetHash;
import com.android.sdklib.AndroidVersion;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.SdkVersionInfo;
import com.android.support.AndroidxNameUtils;
import com.android.tools.lint.client.api.CircularDependencyException;
import com.android.tools.lint.client.api.Configuration;
import com.android.tools.lint.client.api.JavaEvaluator;
import com.android.tools.lint.client.api.LintClient;
import com.android.tools.lint.client.api.LintDriver;
import com.android.tools.lint.client.api.SdkInfo;
import com.android.tools.lint.client.api.UastParser;
import com.android.tools.lint.model.LintModelAndroidLibrary;
import com.android.tools.lint.model.LintModelLibrary;
import com.android.tools.lint.model.LintModelMavenName;
import com.android.tools.lint.model.LintModelModule;
import com.android.tools.lint.model.LintModelModuleType;
import com.android.tools.lint.model.LintModelNamespacingMode;
import com.android.tools.lint.model.LintModelVariant;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.Closeables;
import com.intellij.core.CoreApplicationEnvironment;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiClass;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.jetbrains.kotlin.config.LanguageVersionSettings;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** A project contains information about an Android project being scanned for Lint errors. */
public class Project {
    protected final LintClient client;
    protected final File dir;
    protected File referenceDir;
    protected final File partialResultsDir;
    protected Configuration configuration;
    protected String pkg;
    protected Document dom;
    protected int buildSdk = -1;
    protected String buildTargetHash;
    protected IAndroidTarget target;

    protected ApiConstraint manifestMinSdks = ApiConstraint.ALL;
    protected AndroidVersion manifestMinSdk = AndroidVersion.DEFAULT;
    protected AndroidVersion manifestTargetSdk = AndroidVersion.DEFAULT;
    protected LanguageLevel javaLanguageLevel;
    protected LanguageVersionSettings kotlinLanguageLevel;

    protected boolean library;
    protected boolean externalLibrary;
    protected String name;
    protected String proguardPath;
    protected boolean mergeManifests = true;

    /** The SDK info, if any */
    protected SdkInfo sdkInfo;

    /**
     * If non null, specifies a non-empty list of specific files under this project which should be
     * checked.
     */
    protected List<File> files;

    protected List<File> proguardFiles;
    protected List<File> gradleFiles;
    protected List<File> tomlFiles;
    protected List<File> manifestFiles;
    protected List<File> javaSourceFolders;
    protected List<File> generatedSourceFolders;
    protected List<File> javaClassFolders;
    protected List<File> nonProvidedJavaLibraries;
    protected List<File> javaLibraries;
    protected List<File> testSourceFolders;
    protected List<File> instrumentationTestSourceFolders;
    protected List<File> unitTestSourceFolders;
    protected List<File> testLibraries;
    protected List<File> testFixturesSourceFolders;
    protected List<File> testFixturesLibraries;
    protected List<File> resourceFolders;
    protected List<File> generatedResourceFolders;
    protected List<File> assetFolders;
    protected List<Project> directLibraries;
    protected List<Project> allLibraries;
    protected boolean reportIssues = true;
    public Boolean gradleProject;
    protected Boolean appCompat;
    protected Boolean leanback;
    protected Set<Desugaring> desugaring;
    private Map<String, String> superClassMap;
    private ResourceVisibilityLookup resourceVisibility;
    private Document mergedManifest;
    private com.intellij.openapi.project.Project ideaProject;
    private Map<Object, Object> clientProperties;

    private CoreApplicationEnvironment env;

    /**
     * Creates a new {@link Project} for the given directory.
     *
     * @param client the tool running the lint check
     * @param dir the root directory of the project
     * @param referenceDir See {@link #getReferenceDir()}.
     * @return a new {@link Project}
     */
    @NonNull
    public static Project create(
            @NonNull LintClient client, @NonNull File dir, @NonNull File referenceDir) {
        return new Project(client, dir, referenceDir);
    }

    /**
     * Returns true if this project is a Gradle-based Android project
     *
     * @return true if this is a Gradle-based project
     */
    public boolean isGradleProject() {
        if (gradleProject == null) {
            gradleProject = client.isGradleProject(this);
        }

        return gradleProject;
    }

    /**
     * Returns true if this project is an Android project.
     *
     * @return true if this project is an Android project.
     */
    public boolean isAndroidProject() {
        return true;
    }

    /**
     * Returns the project model for this project if supported by the build system.
     *
     * @return the project model, or null
     */
    @Nullable
    public LintModelModule getBuildModule() {
        LintModelVariant variant = getBuildVariant();
        if (variant != null) {
            return variant.getModule();
        }
        return null;
    }

    /**
     * Returns the current selected variant, if any.
     *
     * <p>This can be used by incremental lint rules to warn about problems in the current context.
     * Lint rules should however strive to perform cross variant analysis and warn about problems in
     * any configuration.
     *
     * @return the selected variant, or null
     */
    @Nullable
    public LintModelVariant getBuildVariant() {
        return null;
    }

    /**
     * Returns the project model for this project if it corresponds to a library with a lint model.
     *
     * @return the library model, or null
     */
    @Nullable
    public LintModelAndroidLibrary getBuildLibraryModel() {
        return null;
    }

    /** Returns the set of desugaring operations in effect for this project. */
    public Set<Desugaring> getDesugaring() {
        if (desugaring == null) {
            desugaring = client.getDesugaring(this);
        }
        return desugaring;
    }

    /** Returns true if the given desugaring operation is in effect for this project. */
    public boolean isDesugaring(Desugaring type) {
        return getDesugaring().contains(type);
    }

    public boolean isCoreLibraryDesugaringEnabled() {
        LintModelVariant variant = getBuildVariant();
        return variant != null && variant.getBuildFeatures().getCoreLibraryDesugaringEnabled();
    }

    /**
     * Associate the given key and value data with this project. Used to store project specific
     * state without introducing external caching.
     */
    public void putClientProperty(@NonNull Object key, @Nullable Object value) {
        if (clientProperties == null) {
            clientProperties = new HashMap<>();
        }
        if (value != null) {
            clientProperties.put(key, value);
        } else {
            clientProperties.remove(key);
        }
    }

    /**
     * Retrieve the given key and value data associated with this project. Used to store project
     * specific state without introducing external caching.
     */
    @Nullable
    public <T> T getClientProperty(@NonNull Object key) {
        if (clientProperties == null) {
            return null;
        }
        //noinspection unchecked
        return (T) clientProperties.get(key);
    }

    /** Returns the corresponding IDE project. */
    @Nullable
    public com.intellij.openapi.project.Project getIdeaProject() {
        return ideaProject;
    }

    public void setIdeaProject(@Nullable com.intellij.openapi.project.Project ideaProject) {
        this.ideaProject = ideaProject;
    }

    /**
     * If this is a Gradle project with a valid Gradle model, return the version of the
     * model/plugin.
     *
     * @return the Gradle plugin version, or null if invalid or not a Gradle project
     */
    @Nullable
    public AgpVersion getGradleModelVersion() {
        LintModelModule gradleProjectModel = getBuildModule();
        if (gradleProjectModel != null) {
            return gradleProjectModel.getAgpVersion();
        } else {
            return null;
        }
    }

    /**
     * Returns the merged manifest of this project. This may return null if not called on the main
     * project. Note that the file reference in the merged manifest isn't accurate; the merged
     * manifest accumulates information from a wide variety of locations.
     *
     * @return The merged manifest, if available.
     */
    @Nullable
    public Document getMergedManifest() {
        if (mergedManifest == null) {
            // Don't provide the merged manifest if doing partial analysis on a library
            // project; accessing this here means the detector is not correctly handling
            // libraries
            if (LintClient.isUnitTest()
                    && Context.Companion.checkForbidden("project.getMergedManifest()", dir, null)) {
                return null;
            }
            mergedManifest = client.getMergedManifest(this);
        }

        return mergedManifest;
    }

    /** Creates a new Project. Use one of the factory methods to create. */
    protected Project(
            @NonNull LintClient client,
            @NonNull File dir,
            @NonNull File referenceDir,
            File partialResultsDir) {
        this.client = client;
        this.dir = dir;
        this.referenceDir = referenceDir;
        this.partialResultsDir = partialResultsDir;
        initialize();
    }

    /** Creates a new Project. Use one of the factory methods to create. */
    protected Project(@NonNull LintClient client, @NonNull File dir, @NonNull File referenceDir) {
        this(client, dir, referenceDir, null);
    }

    protected void initialize() {
        // Default initialization: Use ADT/ant style project.properties file
        try {
            // Read properties file and initialize library state
            Properties properties = new Properties();
            File propFile = new File(dir, PROJECT_PROPERTIES);
            if (propFile.exists()) {
                BufferedInputStream is = new BufferedInputStream(new FileInputStream(propFile));
                try {
                    properties.load(is);
                    String value = properties.getProperty(ANDROID_LIBRARY);
                    library = VALUE_TRUE.equals(value);
                    String proguardPath = properties.getProperty(PROGUARD_CONFIG);
                    if (proguardPath != null) {
                        this.proguardPath = proguardPath;
                    }
                    mergeManifests =
                            !VALUE_FALSE.equals(properties.getProperty("manifestmerger.enabled"));
                    String target = properties.getProperty("target");
                    if (target != null) {
                        setBuildTargetHash(target);
                    }

                    if (VALUE_TRUE.equals(properties.getProperty("android_java8_libs"))) {
                        desugaring = Desugaring.FULL;
                    }

                    for (int i = 1; i < 1000; i++) {
                        String key = String.format(Locale.US, ANDROID_LIBRARY_REFERENCE_FORMAT, i);
                        String library = properties.getProperty(key);
                        if (library == null || library.isEmpty()) {
                            // No holes in the numbering sequence is allowed
                            break;
                        }

                        File libraryDir = new File(dir, library).getCanonicalFile();

                        if (directLibraries == null) {
                            directLibraries = new ArrayList<>();
                        }

                        // Adjust the reference dir to be a proper prefix path of the
                        // library dir
                        File libraryReferenceDir = referenceDir;
                        if (!libraryDir.getPath().startsWith(referenceDir.getPath())) {
                            // Symlinks etc might have been resolved, so do those to
                            // the reference dir as well
                            libraryReferenceDir = libraryReferenceDir.getCanonicalFile();
                            if (!libraryDir.getPath().startsWith(referenceDir.getPath())) {
                                File file = libraryReferenceDir;
                                while (file != null && !file.getPath().isEmpty()) {
                                    if (libraryDir.getPath().startsWith(file.getPath())) {
                                        libraryReferenceDir = file;
                                        break;
                                    }
                                    file = file.getParentFile();
                                }
                            }
                        }

                        try {
                            Project libraryPrj = client.getProject(libraryDir, libraryReferenceDir);
                            directLibraries.add(libraryPrj);
                            // By default, we don't report issues in inferred library projects.
                            // The driver will set report = true for those library explicitly
                            // requested.
                            libraryPrj.setReportIssues(false);
                        } catch (CircularDependencyException e) {
                            e.setProject(this);
                            e.setLocation(Location.create(propFile));
                            throw e;
                        }
                    }
                } finally {
                    try {
                        Closeables.close(is, true /* swallowIOException */);
                    } catch (IOException e) {
                        // cannot happen
                    }
                }
            }
        } catch (IOException ioe) {
            client.log(ioe, "Initializing project state");
        }

        if (directLibraries != null) {
            directLibraries = Collections.unmodifiableList(directLibraries);
        } else {
            directLibraries = Collections.emptyList();
        }
    }

    /** Gets the namespacing mode used for this project */
    @NonNull
    private LintModelNamespacingMode getNamespacingMode() {
        LintModelVariant variant = getBuildVariant();
        if (variant != null) {
            return variant.getBuildFeatures().getNamespacingMode();
        } else {
            return LintModelNamespacingMode.DISABLED;
        }
    }

    private ResourceNamespace namespace;

    /** Returns the namespace for resources in this module/project */
    @NonNull
    public ResourceNamespace getResourceNamespace() {
        if (namespace == null) {
            String packageName = getPackage();
            if (packageName == null || getNamespacingMode() == LintModelNamespacingMode.DISABLED) {
                namespace = ResourceNamespace.RES_AUTO;
            } else {
                namespace = ResourceNamespace.fromPackageName(packageName);
            }
        }

        return namespace;
    }

    @Override
    public String toString() {
        return "Project [dir=" + dir + ']';
    }

    @Override
    public int hashCode() {
        return dir.hashCode();
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Project other = (Project) obj;
        //noinspection FileComparisons
        return dir.equals(other.dir);
    }

    /**
     * Adds the given file to the list of files which should be checked in this project. If no files
     * are added, the whole project will be checked.
     *
     * @param file the file to be checked
     */
    public void addFile(@NonNull File file) {
        if (files == null) {
            files = new ArrayList<>();
        }
        files.add(file);
    }

    /**
     * The list of files to be checked in this project. If null, the whole project should be
     * checked.
     *
     * @return the subset of files to be checked, or null for the whole project
     */
    @Nullable
    public List<File> getSubset() {
        return files;
    }

    /**
     * Returns the {@link UastParser.UastSourceList} for this project if the project provides a
     * specific definition for it; if not, lint will iterate the project folders on its own to
     * discover the source list.
     */
    @Nullable
    public UastParser.UastSourceList getUastSourceList(
            @NonNull LintDriver driver, @Nullable Project main) {
        return null;
    }

    /**
     * Returns the list of source folders for Java and Kotlin source files
     *
     * @return a list of source folders to search for .java and .kt files
     */
    @NonNull
    public List<File> getJavaSourceFolders() {
        if (javaSourceFolders == null) {
            javaSourceFolders = client.getJavaSourceFolders(this);
        }

        return javaSourceFolders;
    }

    @NonNull
    public List<File> getGeneratedSourceFolders() {
        if (generatedSourceFolders == null) {
            generatedSourceFolders = client.getGeneratedSourceFolders(this);
        }

        return generatedSourceFolders;
    }

    /**
     * Returns the list of output folders or jars for class files.
     *
     * @return a list of output folders or jars to search for .class files
     */
    @NonNull
    public List<File> getJavaClassFolders() {
        if (javaClassFolders == null) {
            javaClassFolders = client.getJavaClassFolders(this);
        }
        return javaClassFolders;
    }

    /**
     * Returns the list of Java libraries (typically .jar files) that this project depends on. Note
     * that this refers to jar libraries, not Android library projects which are processed in a
     * separate pass with their own source and class folders.
     *
     * @param includeProvided If true, included provided libraries too (libraries that are not
     *     packaged with the app, but are provided for compilation purposes and are assumed to be
     *     present in the running environment)
     * @return a list of .jar files (or class folders) that this project depends on.
     */
    @NonNull
    public List<File> getJavaLibraries(boolean includeProvided) {
        if (includeProvided) {
            if (javaLibraries == null) {
                javaLibraries = client.getJavaLibraries(this, true);
            }
            return javaLibraries;
        } else {
            if (nonProvidedJavaLibraries == null) {
                nonProvidedJavaLibraries = client.getJavaLibraries(this, false);
            }
            return nonProvidedJavaLibraries;
        }
    }

    /**
     * Returns the list of source folders for Java test source files
     *
     * @return a list of source folders to search for .java files
     */
    @NonNull
    public List<File> getTestSourceFolders() {
        if (testSourceFolders == null) {
            testSourceFolders = client.getTestSourceFolders(this);
        }

        return testSourceFolders;
    }

    @NonNull
    public List<File> getUnitTestSourceFolders() {
        return Collections.emptyList();
    }

    @NonNull
    public List<File> getInstrumentationTestSourceFolders() {
        return Collections.emptyList();
    }

    /**
     * Returns the list of Java libraries (typically .jar files) that the tests depend on.
     *
     * @return a list of .jar files (or class folders) that the tests depends on.
     */
    @NonNull
    public List<File> getTestLibraries() {
        if (testLibraries == null) {
            testLibraries = client.getTestLibraries(this);
        }

        return testLibraries;
    }

    @NonNull
    public List<File> getTestFixturesSourceFolders() {
        return Collections.emptyList();
    }

    @NonNull
    public List<File> getTestFixturesLibraries() {
        return Collections.emptyList();
    }

    /**
     * Returns the resource folders.
     *
     * @return a list of files pointing to the resource folders, which might be empty if the project
     *     does not provide any resources.
     */
    @NonNull
    public List<File> getResourceFolders() {
        if (resourceFolders == null) {
            resourceFolders = client.getResourceFolders(this);
        }

        return resourceFolders;
    }

    @NonNull
    public List<File> getGeneratedResourceFolders() {
        if (generatedResourceFolders == null) {
            generatedResourceFolders = client.getGeneratedResourceFolders(this);
        }

        return generatedResourceFolders;
    }

    /**
     * Returns the asset folders.
     *
     * @return a list of files pointing to the asset folders, which might be empty if the project
     *     does not provide any resources.
     */
    @NonNull
    public List<File> getAssetFolders() {
        if (assetFolders == null) {
            assetFolders = client.getAssetFolders(this);
        }

        return assetFolders;
    }

    /**
     * Returns the relative path of a given file relative to the user specified directory (which is
     * often the project directory but sometimes a higher up directory when a directory tree is
     * being scanned
     *
     * @param file the file under this project to check
     * @return the path relative to the reference directory (often the project directory)
     */
    @NonNull
    public String getDisplayPath(@NonNull File file) {
        return client.getDisplayPath(file, this, TextFormat.TEXT);
    }

    /**
     * Returns the relative path of a given file within the current project.
     *
     * @param file the file under this project to check
     * @return the path relative to the project
     */
    @NonNull
    public String getRelativePath(@NonNull File file) {
        return getRelativePath(dir, file);
    }

    /**
     * Returns the relative path of a given file within the given root directory.
     *
     * @param root the root/base folder
     * @param file the file under the root folder to check
     * @return the path relative to the project
     */
    @NonNull
    public static String getRelativePath(@Nullable File root, @NonNull File file) {
        String path = file.getPath();
        if (root == null) {
            return path;
        }
        String referencePath = root.getPath();
        if (path.startsWith(referencePath)) {
            int length = referencePath.length();
            if (path.length() > length && path.charAt(length) == File.separatorChar) {
                length++;
            }

            return path.substring(length);
        }

        return path;
    }

    /**
     * Returns the project root directory
     *
     * @return the dir
     */
    @NonNull
    public File getDir() {
        return dir;
    }

    /**
     * Returns the original user supplied directory where the lint search started. For example, if
     * you run lint against {@code /tmp/foo}, and it finds a project to lint in {@code
     * /tmp/foo/dev/src/project1}, then the {@code dir} is {@code /tmp/foo/dev/src/project1} and the
     * {@code referenceDir} is {@code /tmp/foo/}.
     *
     * @return the reference directory, never null
     */
    @NonNull
    public File getReferenceDir() {
        return referenceDir;
    }

    /**
     * Returns the partial results directory for the project, or null if unset. All per-project
     * serialized files will be written to this directory, if set (not just the partial results
     * file). Note that Gradle build variants for a project may provide a more specific partial
     * results directory.
     *
     * @return the partial results directory for the project, or null if unset
     */
    public File getPartialResultsDir() {
        return partialResultsDir;
    }

    /**
     * Gets the configuration associated with this project
     *
     * @param driver the current driver, if any
     * @return the configuration associated with this project
     */
    @NonNull
    public Configuration getConfiguration(@Nullable LintDriver driver) {
        if (configuration == null) {
            configuration = client.getConfiguration(this, driver);
            configuration.setFileLevel(false);
        }
        return configuration;
    }

    /**
     * Returns the application package specified by the manifest
     *
     * @return the application package, or null if unknown
     */
    @Nullable
    public String getPackage() {
        return pkg;
    }

    /**
     * Returns the application id, if known
     *
     * @return the application id, if known
     */
    @Nullable
    public String getApplicationId() {
        LintModelVariant variant = getBuildVariant();
        if (variant != null) {
            return variant.getMainArtifact().getApplicationId();
        }

        return getPackage();
    }

    /**
     * Returns the minimum API level for the project. This does not include any SDK extensions the
     * user has declared; for that, use {@link #getMinSdkVersions()}} instead.
     *
     * @return the minimum API level or {@link AndroidVersion#DEFAULT} if unknown
     * @see #getMinSdkVersions() to get a vector including SDK extensions
     */
    @NonNull
    public AndroidVersion getMinSdkVersion() {
        return manifestMinSdk == null ? AndroidVersion.DEFAULT : manifestMinSdk;
    }

    /**
     * Returns the minimum API levels for the project (the `minSdkVersion`, along with any SDK
     * extensions required to be present.)
     *
     * @return the minimum API level or {@link ApiConstraint#UNKNOWN} if unknown
     */
    @NonNull
    public ApiConstraint getMinSdkVersions() {
        return manifestMinSdks;
    }

    /**
     * Returns the minimum API <b>level</b> requested by the manifest, or -1 if not specified. Use
     * {@link #getMinSdkVersion()} to get a full version if you need to check if the platform is a
     * preview platform etc.
     *
     * @return the minimum API level or -1 if unknown
     */
    public int getMinSdk() {
        AndroidVersion version = getMinSdkVersion();
        return version == AndroidVersion.DEFAULT ? -1 : version.getApiLevel();
    }

    /**
     * Returns the target API level for the project
     *
     * @return the target API level or {@link AndroidVersion#DEFAULT} if unknown
     */
    @NonNull
    public AndroidVersion getTargetSdkVersion() {
        return manifestTargetSdk == AndroidVersion.DEFAULT ? getMinSdkVersion() : manifestTargetSdk;
    }

    /** Returns the expected language level for Java source files in this project */
    @NonNull
    public LanguageLevel getJavaLanguageLevel() {
        if (javaLanguageLevel == null) {
            javaLanguageLevel = client.getJavaLanguageLevel(this);
        }

        return javaLanguageLevel;
    }

    /** Returns the expected language level for Kotlin source files in this project */
    @NonNull
    public LanguageVersionSettings getKotlinLanguageLevel() {
        if (kotlinLanguageLevel == null) {
            kotlinLanguageLevel = client.getKotlinLanguageLevel(this);
        }

        return kotlinLanguageLevel;
    }

    /**
     * Returns the target API <b>level</b> specified by the manifest, or -1 if not specified. Use
     * {@link #getTargetSdkVersion()} to get a full version if you need to check if the platform is
     * a preview platform etc.
     *
     * @return the target API level or -1 if unknown
     */
    public int getTargetSdk() {
        AndroidVersion version = getTargetSdkVersion();
        return version == AndroidVersion.DEFAULT ? -1 : version.getApiLevel();
    }

    /**
     * Returns the compile SDK version used to build the project, or null if not known. This is the
     * string name of the compileSdkVersion. If you want the numeric API level, use {@link
     * #getBuildTargetHash()} instead, or to get the actual
     *
     * @return the compileSdkVersion or -1 if unknown
     */
    public int getBuildSdk() {
        return buildSdk;
    }

    /**
     * Returns the target API used to build the project, or null if not known. Note that this is
     * returning a String rather than a {@link AndroidVersion} since it may refer to either a {@link
     * AndroidTargetHash} for a platform or for an add-on, and {@link AndroidVersion} can only
     * express platform versions.
     *
     * @return the build target API or null if unknown
     */
    @Nullable
    public String getBuildTargetHash() {
        return buildTargetHash;
    }

    /**
     * Sets the build target hash to be used for this project. This is only intended for lint
     * internal usage.
     *
     * @param buildTargetHash the target hash
     */
    public void setBuildTargetHash(String buildTargetHash) {
        this.buildTargetHash = buildTargetHash;

        AndroidVersion version = AndroidTargetHash.getPlatformVersion(buildTargetHash);
        if (version != null) {
            buildSdk = version.getFeatureLevel();
        } else {
            // The platform sometimes passes in the wrong target hash; try to account for that
            if (buildTargetHash.indexOf('-') == -1) {
                version =
                        AndroidTargetHash.getPlatformVersion(
                                PLATFORM_HASH_PREFIX + buildTargetHash);
            }
            if (version == null) {
                client.log(
                        Severity.WARNING,
                        null,
                        "Unexpected build target format: %1$s",
                        buildTargetHash);
            }
        }
    }

    /**
     * Returns the target used to build the project, or null if not known
     *
     * @return the build target, or null
     */
    @Nullable
    public IAndroidTarget getBuildTarget() {
        if (target == null) {
            target = client.getCompileTarget(this);
        }

        return target;
    }

    /**
     * Returns the manifest document that the project metadata like {@link #manifestMinSdk} was
     * initially read from.
     */
    @Nullable
    public Document getManifestDom() {
        return dom;
    }

    /**
     * Initialized the manifest state from the given manifest model
     *
     * @param document the DOM document for the manifest XML document
     */
    public void readManifest(@NonNull Document document) {
        dom = document;
        Element root = document.getDocumentElement();
        if (root == null) {
            return;
        }

        if (pkg == null) {
            // if we read multiple manifests for a project, only look at the first one.
            // (This helps when there are feature modules inlined into the main project
            // with their own packages.)
            String packageAttribute = root.getAttribute(ATTR_PACKAGE);
            if (!"".equals(packageAttribute)) {
                pkg = packageAttribute;
            }
        }

        // Initialize minSdk and targetSdk
        NodeList usesSdks = root.getElementsByTagName(TAG_USES_SDK);
        if (usesSdks.getLength() > 0) {
            Element element = (Element) usesSdks.item(0);

            String minSdk = null;
            if (element.hasAttributeNS(ANDROID_URI, ATTR_MIN_SDK_VERSION)) {
                minSdk = element.getAttributeNS(ANDROID_URI, ATTR_MIN_SDK_VERSION);
            }
            if (minSdk != null && !minSdk.isEmpty()) {
                // If the minSdk version is a number we don't need to look up
                // SDK targets to resolve code names (and computing the target list
                // is expensive)
                manifestMinSdk = SdkVersionInfo.getVersion(minSdk, null);
            }

            if (element.hasAttributeNS(ANDROID_URI, ATTR_TARGET_SDK_VERSION)) {
                String targetSdk = element.getAttributeNS(ANDROID_URI, ATTR_TARGET_SDK_VERSION);
                if (!targetSdk.isEmpty()) {
                    manifestTargetSdk = SdkVersionInfo.getVersion(targetSdk, null);
                }
            } else {
                manifestTargetSdk = manifestMinSdk;
            }

            manifestMinSdks = ApiConstraint.Companion.getFromUsesSdk(element);
            if (manifestMinSdks == null) {
                manifestMinSdks = ApiConstraint.ALL;
            }
        }
    }

    /**
     * Returns true if this project is an Android library project
     *
     * @return true if this project is an Android library project
     */
    public boolean isLibrary() {
        return library;
    }

    /**
     * Returns true if this project is an external library (typically an AAR library), as opposed to
     * a local library we have source for
     *
     * @return true if this is an external library
     */
    public boolean isExternalLibrary() {
        return externalLibrary;
    }

    protected LintModelMavenName mavenCoordinates = null;

    /**
     * Returns the Maven coordinate of this project, if known.
     *
     * @return the maven coordinate, or null
     */
    @Nullable
    public LintModelMavenName getMavenCoordinate() {
        if (mavenCoordinates == null) {
            LintModelModule model = getBuildModule();
            if (model != null) {
                mavenCoordinates = model.getMavenName();
            }
        }
        return mavenCoordinates;
    }

    /**
     * Returns the list of library projects referenced by this project
     *
     * @return the list of library projects referenced by this project, never null
     */
    @NonNull
    public List<Project> getDirectLibraries() {
        return directLibraries != null ? directLibraries : Collections.emptyList();
    }

    /**
     * Sets the list of library projects referenced by this project. This should only be set during
     * project initialization.
     *
     * @param libraries the new library list.
     */
    public void setDirectLibraries(@NonNull List<Project> libraries) {
        directLibraries = libraries;
    }

    /**
     * Returns the transitive closure of the library projects for this project
     *
     * @return the transitive closure of the library projects for this project
     */
    @NonNull
    public List<Project> getAllLibraries() {
        if (allLibraries == null) {
            if (directLibraries.isEmpty()) {
                allLibraries = Collections.emptyList();
                return allLibraries;
            }

            List<Project> all = new ArrayList<>();
            Set<Project> seen = Sets.newHashSet();
            Set<Project> path = Sets.newHashSet();
            seen.add(this);
            path.add(this);
            addLibraryProjects(all, seen, path);
            allLibraries = all;
        }

        return allLibraries;
    }

    /**
     * Adds this project's library project and their library projects recursively into the given
     * collection of projects
     *
     * @param collection the collection to add the projects into
     * @param seen full set of projects we've processed
     * @param path the current path of library dependencies followed
     */
    private void addLibraryProjects(
            @NonNull Collection<Project> collection,
            @NonNull Set<Project> seen,
            @NonNull Set<Project> path) {
        for (Project library : directLibraries) {
            if (seen.contains(library)) {
                if (path.contains(library)) {
                    client.log(
                            Severity.WARNING,
                            null,
                            "Internal lint error: cyclic library dependency for %1$s",
                            library);
                }
                continue;
            }
            collection.add(library);
            seen.add(library);
            path.add(library);
            // Recurse
            library.addLibraryProjects(collection, seen, path);
            path.remove(library);
        }
    }

    /** The type of artifact produced by this Android project. */
    @NonNull
    public LintModelModuleType getType() {
        LintModelModule buildModule = getBuildModule();
        if (buildModule != null) {
            return buildModule.getType();
        }
        if (isLibrary()) {
            return LintModelModuleType.LIBRARY;
        } else {
            return LintModelModuleType.APP;
        }
    }

    /**
     * Whether this project is a base application with dynamic features.
     *
     * @return true if this is an application project that has any dynamic features, false in all
     *     other cases.
     */
    public boolean hasDynamicFeatures() {
        return false;
    }

    /**
     * Gets the SDK info for the current project.
     *
     * @return the SDK info for the current project, never null
     */
    @NonNull
    public SdkInfo getSdkInfo() {
        if (sdkInfo == null) {
            sdkInfo = client.getSdkInfo(this);
        }

        return sdkInfo;
    }

    /**
     * Gets the paths to the manifest files in this project, if any exists. The manifests should be
     * provided such that the main manifest comes first, then any flavor versions, then any build
     * types.
     *
     * @return the path to the manifest file, or null if it does not exist
     */
    @NonNull
    public List<File> getManifestFiles() {
        if (manifestFiles == null) {
            File manifestFile = new File(dir, ANDROID_MANIFEST_XML);
            if (manifestFile.exists()) {
                manifestFiles = Collections.singletonList(manifestFile);
            } else {
                manifestFiles = Collections.emptyList();
            }
        }

        return manifestFiles;
    }

    /**
     * Returns the proguard files configured for this project, if any
     *
     * @return the proguard files, if any
     */
    @NonNull
    public List<File> getProguardFiles() {
        if (proguardFiles == null) {
            List<File> files = new ArrayList<>();
            if (proguardPath != null) {
                Splitter splitter = Splitter.on(CharMatcher.anyOf(":;"));
                for (String path : splitter.split(proguardPath)) {
                    if (path.contains("${")) {
                        // Don't analyze the global/user proguard files
                        continue;
                    }
                    File file = new File(path);
                    if (!file.isAbsolute()) {
                        file = new File(getDir(), path);
                    }
                    if (file.exists()) {
                        files.add(file);
                    }
                }
            }
            if (files.isEmpty()) {
                File file = new File(getDir(), OLD_PROGUARD_FILE);
                if (file.exists()) {
                    files.add(file);
                }
                file = new File(getDir(), FN_PROJECT_PROGUARD_FILE);
                if (file.exists()) {
                    files.add(file);
                }
            }
            proguardFiles = files;
        }
        return proguardFiles;
    }

    /** Returns the .properties files to be analyzed in the project */
    @NonNull
    public List<File> getPropertyFiles() {
        List<File> propertyFiles = new ArrayList<>(2);
        File local = new File(dir, FN_LOCAL_PROPERTIES);
        if (local.isFile()) {
            propertyFiles.add(local);
        }
        File gradle = new File(dir, FN_GRADLE_PROPERTIES);
        if (gradle.isFile()) {
            propertyFiles.add(gradle);
        }
        File wrapper = new File(dir, FD_GRADLE_WRAPPER + separator + FN_GRADLE_WRAPPER_PROPERTIES);
        if (wrapper.isFile()) {
            propertyFiles.add(wrapper);
        }
        return propertyFiles;
    }

    /**
     * Returns the Gradle build script files configured for this project, if any
     *
     * @return the Gradle files, if any
     */
    @NonNull
    public List<File> getGradleBuildScripts() {
        if (gradleFiles == null) {
            if (isGradleProject()) {
                gradleFiles = Lists.newArrayListWithExpectedSize(2);
                File build = new File(dir, FN_BUILD_GRADLE);
                if (build.exists()) {
                    gradleFiles.add(build);
                }
                build = new File(dir, FN_BUILD_GRADLE_KTS);
                if (build.exists()) {
                    gradleFiles.add(build);
                }
                File settings = new File(dir, FN_SETTINGS_GRADLE);
                if (settings.exists()) {
                    gradleFiles.add(settings);
                }
                settings = new File(dir, FN_SETTINGS_GRADLE_KTS);
                if (settings.exists()) {
                    gradleFiles.add(settings);
                }
            } else {
                gradleFiles = Collections.emptyList();
            }
        }

        return gradleFiles;
    }

    @NonNull
    public List<File> getTomlFiles() {
        if (tomlFiles == null) {
            if (isGradleProject()) {
                // Gradle version catalogs? These files don't belong to any one module (in fact
                // they sit outside the individual modules), and we don't want to just go
                // and include ../gradle from the projects since that means we'd repeat the
                // same TOML warnings for each project. Instead, we process them here, but just
                // once.
                tomlFiles = new ArrayList<>(3);
                File rootDir = client.getRootDir();
                if (rootDir != null && isDesignatedTomlModule(rootDir)) {
                    File gradle = new File(rootDir, FD_GRADLE);
                    if (gradle.isDirectory()) {
                        File[] catalogs = gradle.listFiles();
                        if (catalogs != null) {
                            for (File catalog : catalogs) {
                                if (catalog.getPath().endsWith(DOT_VERSIONS_DOT_TOML)) {
                                    tomlFiles.add(catalog);
                                }
                            }
                        }
                    }
                }
            } else {
                tomlFiles = Collections.emptyList();
            }
        }

        return tomlFiles;
    }

    /**
     * TOML files aren't actually part of this project; we went looking outside (from the root
     * project). This means that we'd potentially end up repeating analysis (and reporting) on the
     * same files over and over, from each project. We should only do this once. For now, we're
     * assigning the responsibility to the first module alphabetically in the root directory.
     *
     * <p>(**This is a workaround until the catalog files are provided from the lint model
     * instead.**)
     */
    private boolean isDesignatedTomlModule(File root) {
        File[] moduleDirs = root.listFiles();
        if (moduleDirs != null) {
            Arrays.sort(moduleDirs);
            for (File moduleDir : moduleDirs) {
                if (new File(moduleDir, FN_BUILD_GRADLE).exists()
                        || new File(moduleDir, FN_BUILD_GRADLE_KTS).exists()) {
                    return dir.getPath().equalsIgnoreCase(moduleDir.getPath());
                }
            }
        }
        // No directories match, just fall back to assigning the responsibility to the app module;
        // there's usually exactly one.
        return getType() == LintModelModuleType.APP;
    }

    /**
     * Returns the name of the project
     *
     * @return the name of the project, never null
     */
    @NonNull
    public String getName() {
        if (name == null) {
            name = client.getProjectName(this);
        }

        return name;
    }

    /**
     * Sets the name of the project
     *
     * @param name the name of the project, never null
     */
    public void setName(@NonNull String name) {
        assert !name.isEmpty();
        this.name = name;
    }

    /**
     * Sets whether lint should report issues in this project. See {@link #getReportIssues()} for a
     * full description of what that means.
     *
     * @param reportIssues whether lint should report issues in this project
     */
    public void setReportIssues(boolean reportIssues) {
        this.reportIssues = reportIssues;
    }

    /**
     * Returns whether lint should report issues in this project.
     *
     * <p>If a user specifies a project and its library projects for analysis, then those library
     * projects are all "included", and all errors found in all the projects are reported. But if
     * the user is only running lint on the main project, we shouldn't report errors in any of the
     * library projects. We still need to <b>consider</b> them for certain types of checks, such as
     * determining whether resources found in the main project are unused, so the detectors must
     * still get a chance to look at these projects. The {@code #getReportIssues()} attribute is
     * used for this purpose.
     *
     * @return whether lint should report issues in this project
     */
    public boolean getReportIssues() {
        return reportIssues;
    }

    /**
     * Returns whether manifest merging is in effect
     *
     * @return true if manifests in library projects should be merged into main projects
     */
    public boolean isMergingManifests() {
        return mergeManifests;
    }

    /**
     * Returns true if this project depends on the given artifact. Note that the project doesn't
     * have to be a Gradle project; the artifact is just an identifier for name a specific library,
     * such as com.android.support:support-v4 to identify the support library.
     *
     * <p>Note that for an AndroidX library, this method will treat both the pre-AndroidX and the
     * post-AndroidX package migration as the same, so this method cannot be used to distinguish
     * between depending on an old support library artifact or the corresponding new AndroidX
     * artifact.
     *
     * @param artifact the Gradle/Maven name of a library
     * @return true if the library is installed, false if it is not, and null if we're not sure
     */
    @Nullable
    public Boolean dependsOn(@NonNull String artifact) {
        artifact = AndroidxNameUtils.getCoordinateMapping(artifact);

        if (ANDROIDX_APPCOMPAT_LIB_ARTIFACT.equals(artifact)
                || APPCOMPAT_LIB_ARTIFACT.equals(artifact)) {
            if (appCompat == null) {
                UastParser parser = client.getUastParser(this);
                // Using classpath is most accurate, but isn't available until parsing services have
                // been initialized
                if (parser.getPrepared()) {
                    JavaEvaluator evaluator = parser.getEvaluator();
                    PsiClass activity =
                            evaluator.findClass("androidx.appcompat.app.AppCompatActivity");
                    if (activity != null) {
                        appCompat = true;
                    } else {
                        activity = evaluator.findClass("android.support.v7.app.AppCompatActivity");
                        appCompat = activity != null;
                    }
                } else {
                    for (File file : getJavaLibraries(true)) {
                        String name = file.getName();
                        if (name.startsWith("appcompat")
                                || name.equals("android-support-v4.jar")
                                || name.startsWith("support-v4-")
                                || name.startsWith("legacy-support-")) {
                            // Not cached; we want accurate result from code lookup once it's
                            // available: appCompat = true; break;
                            return true;
                        }
                    }
                    for (Project dependency : getDirectLibraries()) {
                        Boolean b = dependency.dependsOn(artifact);
                        if (b != null && b) {
                            return true;
                        }
                    }
                    return false;
                }
            }

            return appCompat;
        }

        return null;
    }

    private List<String> mCachedApplicableDensities;

    /**
     * Returns the set of applicable densities for this project. If null, there are no density
     * restrictions and all densities apply.
     *
     * @return the list of specific densities that apply in this project, or null if all densities
     *     apply
     */
    @Nullable
    public List<String> getApplicableDensities() {
        if (mCachedApplicableDensities == null) {
            // Use the gradle API to set up relevant densities. For example, if the
            // build.gradle file contains this:
            // android {
            //     defaultConfig {
            //         resConfigs "nodpi", "hdpi"
            //     }
            // }
            // ...then we should only enforce hdpi densities, not all these others!
            LintModelVariant variant = getBuildVariant();
            if (variant != null) {
                Set<String> relevantDensities = Sets.newHashSet();
                for (String densityName : variant.getResourceConfigurations()) {
                    Density density = Density.getEnum(densityName);
                    if (density != null
                            && density.isRecommended()
                            && density != Density.NODPI
                            && density != Density.ANYDPI) {
                        relevantDensities.add(densityName);
                    }
                }
                if (!relevantDensities.isEmpty()) {
                    mCachedApplicableDensities =
                            Lists.newArrayListWithExpectedSize(relevantDensities.size());
                    for (String density : relevantDensities) {
                        String folder = ResourceFolderType.DRAWABLE.getName() + '-' + density;
                        mCachedApplicableDensities.add(folder);
                    }
                    Collections.sort(mCachedApplicableDensities);
                } else {
                    mCachedApplicableDensities = Collections.emptyList();
                }
            } else {
                mCachedApplicableDensities = Collections.emptyList();
            }
        }

        return mCachedApplicableDensities.isEmpty() ? null : mCachedApplicableDensities;
    }

    /**
     * Returns a super class map for this project. The keys and values are internal class names
     * (e.g. java/lang/Integer, not java.lang.Integer).
     *
     * @return a map, possibly empty but never null
     */
    @NonNull
    public Map<String, String> getSuperClassMap() {
        if (superClassMap == null) {
            superClassMap = client.createSuperClassMap(this);
        }

        return superClassMap;
    }

    /**
     * Returns a shared {@link ResourceVisibilityLookup}
     *
     * @return a shared provider for looking up resource visibility
     */
    @NonNull
    public ResourceVisibilityLookup getResourceVisibility() {
        if (resourceVisibility == null) {
            LintModelVariant variant = getBuildVariant();
            if (variant != null) {
                Collection<LintModelLibrary> libraries =
                        variant.getMainArtifact().getDependencies().getAll();
                List<ResourceVisibilityLookup> list = new ArrayList<>(libraries.size());
                for (LintModelLibrary library : libraries) {
                    if (library instanceof LintModelAndroidLibrary) {
                        LintModelAndroidLibrary l = (LintModelAndroidLibrary) library;
                        ResourceVisibilityLookup lookup = createLibraryVisibilityLookup(l);
                        list.add(lookup);
                    }
                }
                resourceVisibility = ResourceVisibilityLookup.create(list);
            } else if (getBuildLibraryModel() != null) {
                LintModelAndroidLibrary library = getBuildLibraryModel();
                resourceVisibility = createLibraryVisibilityLookup(library);
            } else {
                resourceVisibility = ResourceVisibilityLookup.NONE;
            }
        }

        return resourceVisibility;
    }

    @NonNull
    private static ResourceVisibilityLookup createLibraryVisibilityLookup(
            LintModelAndroidLibrary androidLibrary) {
        File publicResources = androidLibrary.getPublicResources();
        File symbolFile = androidLibrary.getSymbolFile();
        LintModelMavenName c = androidLibrary.getResolvedCoordinates();
        String key = c.getGroupId() + ":" + c.getArtifactId() + ":" + c.getVersion();
        return ResourceVisibilityLookup.create(publicResources, symbolFile, key);
    }

    /**
     * Returns the associated client
     *
     * @return the client
     */
    @NonNull
    public LintClient getClient() {
        return client;
    }

    public void setEnv(CoreApplicationEnvironment env) {
      this.env = env;
    }

    public CoreApplicationEnvironment getEnv() {
      return env;
    }
}