@Override public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) { // 判断资源文件夹是否存在 Project project = context.getProject(); List<File> resourceFolder = project.getResourceFolders(); if (resourceFolder.isEmpty()) { return; } // 获取项目资源文件夹路径 String resourcePath = resourceFolder.get(0).getAbsolutePath(); // 获取 drawable 名字 String drawableName = attribute.getValue().replace("@drawable/", ""); try { // 若 drawable 为 Vector Drawable,则文件后缀为 xml,根据 resource 路径,drawable 名字,文件后缀拼接出完整路径 FileInputStream fileInputStream = new FileInputStream(resourcePath + "/drawable/" + drawableName + ".xml"); BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream)); String line = reader.readLine(); if (line.contains("vector")) { // 若文件存在,并且包含首行包含 vector,则为 Vector Drawable,抛出警告 context.report(ISSUE_XML_VECTOR_DRAWABLE, attribute, context.getLocation(attribute), attribute.getValue() + " 为 Vector Drawable,请使用 Vector 属性进行设置,避免 4.0 及以下版本的系统产生 Crash"); } fileInputStream.close(); } catch (Exception ignored) { } } }
if (sIncludeInactiveReferences && project.isGradleProject() && !project.isLibrary()) { AndroidProject model = project.getGradleProjectModel(); Variant variant = project.getCurrentVariant(); if (model != null && variant != null) { addInactiveReferences(model, variant); List<File> resourceFolders = context.getProject().getResourceFolders(); for (File res : resourceFolders) { File[] f = res.listFiles(); skippedLibraries = false; for (Project project : context.getDriver().getProjects()) { if (!project.getReportIssues()) { skippedLibraries = true; break; resource.getField()); if (location == null) { location = Location.create(context.getProject().getDir());
if (sSingleton == null) { Collection<Project> projects = client.getKnownProjects(); if (Project.isAospBuildEnvironment()) { for (Project project : projects) { if (Project.isAospFrameworksProject(project.getDir())) { return sSingleton = new ExternalAnnotationRepository( Collections.emptyList()); List<File> files = Lists.newArrayListWithExpectedSize(2); for (Project project : projects) { if (project.isGradleProject()) { Variant variant = project.getCurrentVariant(); AndroidProject model = project.getGradleProjectModel(); if (model != null && variant != null) { Dependencies dependencies = variant.getMainArtifact().getDependencies();
if (project.isAndroidProject()) { for (File manifestFile : project.getManifestFiles()) { XmlParser parser = client.getXmlParser(); if (parser != null) { if (context.document != null) { try { project.readManifest(context.document); if ((!project.isLibrary() || (main != null && main.isMergingManifests())) && scope.contains(Scope.MANIFEST)) { List<Detector> detectors = scopeDetectors.get(Scope.MANIFEST); || dirChecks != null && !dirChecks.isEmpty() || binaryChecks != null && !binaryChecks.isEmpty()) { List<File> files = project.getSubset(); if (files != null) { checkIndividualResources(project, main, xmlDetectors, dirChecks, binaryChecks, files); } else { List<File> resourceFolders = project.getResourceFolders(); if (!resourceFolders.isEmpty()) { for (File res : resourceFolders) { scopeDetectors.get(Scope.ALL_JAVA_FILES)); if (checks != null && !checks.isEmpty()) { List<File> files = project.getSubset(); if (files != null) {
@Nullable Project main) { File projectFolder = project.getDir(); List<File> subset = project.getSubset(); for (File res : project.getResourceFolders()) { collectFiles(files, res); for (File srcFolder : project.getJavaSourceFolders()) { collectFiles(files, srcFolder); for (File classFolder : project.getJavaClassFolders()) { collectFiles(files, classFolder); List<File> manifestFiles = project.getManifestFiles(); if (manifestFiles != null) { files.put(Scope.MANIFEST, manifestFiles);
protected DefaultConfiguration(LintClient client, Project project, Configuration parent) { this(client, project, parent, new File(project.getDir(), CONFIG_FILE_NAME)); }
@Nullable private static String computeResourcePrefix(@NonNull Project project) { if (project.isGradleProject()) { return LintUtils.computeResourcePrefix(project.getGradleProjectModel()); } return null; }
@Nullable private static List<String> getResConfigLanguages(@NonNull Project project) { if (project.isGradleProject() && project.getGradleProjectModel() != null && project.getCurrentVariant() != null) { Set<String> relevantDensities = Sets.newHashSet(); Variant variant = project.getCurrentVariant(); List<String> variantFlavors = variant.getProductFlavors(); AndroidProject gradleProjectModel = project.getGradleProjectModel(); addResConfigsFromFlavor(relevantDensities, null, project.getGradleProjectModel().getDefaultConfig()); for (ProductFlavorContainer container : gradleProjectModel.getProductFlavors()) { addResConfigsFromFlavor(relevantDensities, variantFlavors, container); } if (!relevantDensities.isEmpty()) { ArrayList<String> strings = Lists.newArrayList(relevantDensities); Collections.sort(strings); return strings; } } return null; }
String name = nameNode.getValue(); if (!name.isEmpty()) { String pkg = context.getMainProject().getPackage(); if (name.charAt(0) == '.') { name = pkg + name; && context.getMainProject().isGradleProject()) { Attr bindListenerAttr = null; for (Element child : LintUtils.getChildren(element)) { Variant variant = project.getCurrentVariant(); if (variant != null) { Dependencies dependencies = variant.getMainArtifact().getDependencies(); if (project.getBuildSdk() >= 24) { File sdkHome = context.getClient().getSdkHome(); FileOp fileOp = FileOpUtils.create(); if (context.isEnabled(USES_SDK) && !context.getMainProject().isGradleProject()) { context.report(USES_SDK, element, context.getLocation(element), "`<uses-sdk>` tag should specify a minimum API level with " + if (context.isEnabled(USES_SDK) && !context.getMainProject().isGradleProject()) { context.report(USES_SDK, element, context.getLocation(element), "`<uses-sdk>` tag should specify a target API level (the " + Attr name = element.getAttributeNodeNS(ANDROID_URI, ATTR_NAME); if (name != null && name.getValue().equals(MOCK_LOCATION_PERMISSION) && context.getMainProject().isGradleProject()
File manifestFile = new File(project.getDir(), ANDROID_MANIFEST_XML); if (manifestFile.exists()) { Context context = new Context(mClient, project, manifestFile, mScope); context.document = parser.parse(context); if (context.document != null) { project.readManifest(context.document); if (project.getSubset() != null) { checkIndividualResources(project, xmlDetectors, project.getSubset()); } else { File res = new File(projectDir, RES_FOLDER_NAME); mScopeDetectors.get(Scope.ALL_JAVA_FILES)); if (checks.size() > 0) { List<File> sourceFolders = project.getJavaSourceFolders(); checkJava(project, sourceFolders, checks); List<Detector> detectors = mScopeDetectors.get(Scope.CLASS_FILE); if (detectors != null) { List<File> binFolders = project.getJavaClassFolders(); checkClasses(project, binFolders, detectors); List<Detector> detectors = mScopeDetectors.get(Scope.PROGUARD); if (detectors != null) { File file = new File(project.getDir(), PROGUARD_CFG); if (file.exists()) { Context context = new Context(mClient, project, file, mScope);
/** * Returns a shared {@link ResourceVisibilityLookup} * * @return a shared provider for looking up resource visibility */ @NonNull public ResourceVisibilityLookup getResourceVisibility() { if (resourceVisibility == null) { if (isGradleProject()) { AndroidProject project = getGradleProjectModel(); Variant variant = getCurrentVariant(); if (project != null && variant != null) { resourceVisibility = client.getResourceVisibilityProvider().get(project, variant); } else if (getGradleLibraryModel() != null) { try { resourceVisibility = client.getResourceVisibilityProvider() .get(getGradleLibraryModel()); } catch (Exception ignore) { // Handle talking to older Gradle plugins (where we don't // have access to the model version to check up front } } } if (resourceVisibility == null) { resourceVisibility = ResourceVisibilityLookup.NONE; } } return resourceVisibility; }
@Nullable JavaElementVisitor visitor, @NonNull PsiElement node, @NonNull ResourceType resourceType, @NonNull String name, boolean isFramework) { if (context.getProject().isGradleProject() && !isFramework) { Project project = context.getProject(); if (project.getGradleProjectModel() != null && project.getCurrentVariant() != null) { if (isPrivate(context, resourceType, name)) { if (pkg != null) { String pkgName = pkg.getQualifiedName(); if (!(pkgName.equals(context.getProject().getPackage()) || pkgName.equals(context.getMainProject().getPackage()))) { foreignPackage = true;
@Override public void beforeCheckProject(@NonNull Context context) { AndroidProject model = context.getProject().getGradleProjectModel(); if (model == null) { mSkipChecks = true; return; } if (context.getProject().getMinSdk() >= 21) { mSkipChecks = true; return; } GradleVersion version = context.getProject().getGradleModelVersion(); if (version == null || version.getMajor() < 2) { mSkipChecks = true; return; } Variant currentVariant = context.getProject().getCurrentVariant(); if (currentVariant == null) { mSkipChecks = true; return; } if (Boolean.TRUE.equals( currentVariant.getMergedFlavor().getVectorDrawables().getUseSupportLibrary())) { mUseSupportLibrary = true; } }
File file = entry.getKey(); Project project = entry.getValue(); if (!file.equals(project.getDir())) { if (file.isDirectory()) { try { File dir = file.getCanonicalFile(); if (dir.equals(project.getDir())) { continue; project.addFile(file); Set<Project> roots = new HashSet<>(allProjects); for (Project project : allProjects) { roots.removeAll(project.getAllLibraries()); project.setReportIssues(true); for (Project project : roots) { projects.put(project, project); for (Project library : project.getAllLibraries()) { projects.put(library, library); assert !dirs.contains(project.getDir()); dirs.add(project.getDir());
List<String> classPath = Lists.newArrayList(); IAndroidTarget compileTarget = project.getBuildTarget(); if (compileTarget != null) { String androidJar = compileTarget.getPath(IAndroidTarget.ANDROID_JAR); classPath.add(androidJar); } else if (!project.isAndroidProject()) { for (File library : project.getJavaLibraries(true)) { libraries.add(library); names.add(getLibraryName(library)); for (Project libraryProject : project.getAllLibraries()) { if (!PARSE_LIBRARY_SOURCES) { libraries.addAll(libraryProject.getJavaClassFolders()); for (File library : libraryProject.getJavaLibraries(true)) { String name = getLibraryName(library); for (File dir : project.getJavaClassFolders()) { if (dir.exists()) { classPath.add(dir.getPath()); for (File dir : project.getTestLibraries()) { if (dir.exists()) { classPath.add(dir.getPath());
private PermissionHolder getPermissions( @NonNull JavaContext context) { if (mPermissions == null) { Set<String> permissions = Sets.newHashSetWithExpectedSize(30); Set<String> revocable = Sets.newHashSetWithExpectedSize(4); LintClient client = context.getClient(); // Gather permissions from all projects that contribute to the // main project. Project mainProject = context.getMainProject(); for (File manifest : mainProject.getManifestFiles()) { addPermissions(client, permissions, revocable, manifest); } for (Project library : mainProject.getAllLibraries()) { for (File manifest : library.getManifestFiles()) { addPermissions(client, permissions, revocable, manifest); } } AndroidVersion minSdkVersion = mainProject.getMinSdkVersion(); AndroidVersion targetSdkVersion = mainProject.getTargetSdkVersion(); mPermissions = new SetPermissionLookup(permissions, revocable, minSdkVersion, targetSdkVersion); } return mPermissions; }
Project mainProject = context.getMainProject(); if (context.getProject() == mainProject && mHaveClasses && !mainProject.isLibrary() && mReferencedClasses != null && !mReferencedClasses.isEmpty() && context.getDriver().getScope().contains(Scope.CLASS_FILE)) { List<Project> all = Lists.newArrayList(mainProject.getAllLibraries()); all.add(mainProject); for (Project project : all) { for (File root : project.getJavaSourceFolders()) { File source = new File(root, owner.replace('/', File.separatorChar) + DOT_JAVA);
@Override public boolean getReportIssues() { if (projectDescription != null && !projectDescription.report) { return false; } return super.getReportIssues(); }
@Override public void beforeCheckProject(@NonNull Context context) { mHasNativeLibs = false; mApplicationTagHandle = null; if (!context.getMainProject().isGradleProject() || context.getMainProject().getGradleModelVersion() == null) { mSuppress = true; return; } //compileSdkVersion must be >= 23 boolean projectSupportsAttribute = context.getMainProject().getBuildSdk() >= 23; //android gradle plugin must be 2.2.0+ GradleVersion gradleVersion = context.getMainProject().getGradleModelVersion(); boolean gradleSupportsAttribute = MIN_GRADLE_VERSION.compareIgnoringQualifiers(gradleVersion) <= 0; //suppress lint check if the compile SDK or the Gradle plugin are too old mSuppress = !projectSupportsAttribute || !gradleSupportsAttribute; }
private void checkProject(@NonNull Project project, @NonNull Project main) { File projectDir = project.getDir(); List<Project> allLibraries = project.getAllLibraries(); Set<Project> allProjects = new HashSet<>(allLibraries.size() + 1); allProjects.add(project); List<Project> libraries = project.getAllLibraries(); for (Project library : libraries) { Context libraryContext = new Context(this, library, project, projectDir); Location.create(project.getDir()), "Lint canceled by user", TextFormat.RAW);