Skip to content

Android Custom Lint not covering files inside assets folder #7

Open
@pravinkmrr

Description

@pravinkmrr

I have project, in that properties files are taken from assets folder. But when I wrote a custom lint to validate the values in properties files, it is not showing up with the following code.

public class PropertyFileValidator extends Detector implements Detector.OtherFileScanner {
	/**
	 * Using HTTP instead of HTTPS for the wrapper
	 */
	public static final Issue ISSUE = Issue.create(LintConstants.KOUPON_PROPERTY_VALIDATOR, //$NON-NLS-1$
			"Using HTTP instead of HTTPS", "The Gradle Wrapper is available both via HTTP and HTTPS. HTTPS is more " +
					"secure since it protects against man-in-the-middle attacks etc. Older " +
					"projects created in Android Studio used HTTP but we now default to HTTPS " +
					"and recommend upgrading existing projects.", LintConstants.DEV_CHECKLIST, 6, Severity.WARNING, new Implementation(PropertyFileValidator.class, Scope.OTHER_SCOPE));

	/**
	 * Constructs a new {@link PropertyFileDetector}
	 */
	public PropertyFileValidator() {
	}

	@Override
	public boolean appliesTo(@NonNull Context context, @NonNull File file) {
		return true;
	}

	@Override
	public EnumSet<Scope> getApplicableFiles() {
		return Scope.OTHER_SCOPE;
	}

	@Override
	public void run(@NonNull Context context) {
		if(context.file.getPath().contains(DOT_PROPERTIES)) {
			try {
				InputStream comp = new FileInputStream(context.file);
				Properties properties = new Properties();
				properties.load(comp);
				for(Object line : properties.keySet()) {
					context.report(ISSUE, Location.create(context.file), properties.getProperty(line.toString()));
				}
			} catch(IOException e) {
				e.printStackTrace();
			}
			context.report(ISSUE, Location.create(context.file), "line");
		}
	}
}

Correct me if anything else need to be added to the code to include assets also into the scope.

More info:

I have two falvors - dev and prod.

assets folder is available in both dev and prod folder.

Folder Structure

  • dev
    • assets
      • properties
        • x.properties
  • prod
    • assets
      • properties
        • x.properties

Activity

Luziie

Luziie commented on Sep 3, 2023

@Luziie

I have project, in that properties files are taken from assets folder. But when I wrote a custom lint to validate the values in properties files, it is not showing up with the following code.

public class PropertyFileValidator extends Detector implements Detector.OtherFileScanner {
	/**
	 * Using HTTP instead of HTTPS for the wrapper
	 */
	public static final Issue ISSUE = Issue.create(LintConstants.KOUPON_PROPERTY_VALIDATOR, //$NON-NLS-1$
			"Using HTTP instead of HTTPS", "The Gradle Wrapper is available both via HTTP and HTTPS. HTTPS is more " +
					"secure since it protects against man-in-the-middle attacks etc. Older " +
					"projects created in Android Studio used HTTP but we now default to HTTPS " +
					"and recommend upgrading existing projects.", LintConstants.DEV_CHECKLIST, 6, Severity.WARNING, new Implementation(PropertyFileValidator.class, Scope.OTHER_SCOPE));

	/**
	 * Constructs a new {@link PropertyFileDetector}
	 */
	public PropertyFileValidator() {
	}

	@Override
	public boolean appliesTo(@NonNull Context context, @NonNull File file) {
		return true;
	}

	@Override
	public EnumSet<Scope> getApplicableFiles() {
		return Scope.OTHER_SCOPE;
	}

	@Override
	public void run(@NonNull Context context) {
		if(context.file.getPath().contains(DOT_PROPERTIES)) {
			try {
				InputStream comp = new FileInputStream(context.file);
				Properties properties = new Properties();
				properties.load(comp);
				for(Object line : properties.keySet()) {
					context.report(ISSUE, Location.create(context.file), properties.getProperty(line.toString()));
				}
			} catch(IOException e) {
				e.printStackTrace();
			}
			context.report(ISSUE, Location.create(context.file), "line");
		}
	}
}

Correct me if anything else need to be added to the code to include assets also into the scope.

More info:

I have two falvors - dev and prod.

assets folder is available in both dev and prod folder.

Folder Structure

  • dev

    • assets

      • properties

        • x.properties
  • prod

    • assets

      • properties

        • x.properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pravinkmrr@Luziie

        Issue actions

          Android Custom Lint not covering files inside assets folder · Issue #7 · googlesamples/android-custom-lint-rules