In the Android project. How to create a custom Detector
that reports issues of using PNG format.
I have implemented a PngDetector
but it does not work
class PngDetector : Detector(), Detector.XmlScanner {
override fun getApplicableElements(): Collection<String>? =
listOf("bitmap")
override fun visitElement(context: XmlContext, element: Element) {
val srcAttribute = element.getAttribute("src")
if (srcAttribute.endsWith(".png")) {
context.report(
PNG_ISSUE,
element,
context.getLocation(element),
"Use WebP format instead of PNG for bitmap resources"
)
}
}
}
val PNG_ISSUE = Issue.create(
id = "UseWebPInsteadOfPng",
briefDescription = "Use WebP format instead of PNG",
explanation = "WebP provides better compression and smaller file sizes compared to PNG.",
category = Category.CORRECTNESS,
priority = 5,
severity = Severity.WARNING,
implementation = Implementation(
PngDetector::class.java,
Scope.ALL_RESOURCES_SCOPE
)
)
My code is:
<ImageView
android:src="@drawable/wolf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The wolf
image is png format but PngDetector can not detect