An object such as a Paint or Rect is created in onDraw, onMeasure or onLayout, which run for every frame

tx0/kotlin:allocation-in-drawrecommendedwarningframeworks/androiddepth fastperformance, android

What it finds

Drawing and layout callbacks run many times a second during animations. Creating Paint, Rect, Path and similar objects there makes garbage on every frame and causes collection pauses. The Android performance guidance creates them once, as properties.

From the rule's source, draw_allocation.rs.

Example from the rule's tests
class Chart(context: Context) : View(context) {    private val paint = Paint()     override fun onDraw(canvas: Canvas) {        val bounds = Rect()tx0 reports: a new Rect is created on every frame        canvas.drawRect(bounds, Paint())tx0 reports: a new Paint is created on every frame        canvas.drawRect(bounds, paint)    }     fun setup() {        val p = Paint()    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:allocation-in-draw" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:allocation-in-draw] reason="..."

tx0 explain tx0/kotlin:allocation-in-draw shows the same in the terminal. Found a false positive? Report it.