Raw Dimension

ID

kotlin.android_raw_dimen

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

android, maintainability

Description

Flags Android XML attribute values that hard-code a dimension literal such as 12dp, 16sp, 8px, 2mm or 1in instead of referencing a dimension resource declared in res/values/dimens.xml.

Rationale

Hard-coded dimensions defeat density adaptation, accessibility text scaling (sp), and global spacing refactors. Spacing values copied into every layout drift over time and quickly diverge. Defining dimensions once as resources and referencing them via @dimen/…​ keeps the visual language consistent across screens.

<!-- Bad: literal dimension -->
<TextView android:layout_margin="16dp" android:text="Item" />

Two exceptions:

  • Attributes in the tools: namespace are skipped because they are design-time only.

  • The value 0dp is intentionally allowed: ConstraintLayout children use it as the "match constraints" sentinel.

Remediation

Declare the dimension in res/values/dimens.xml and reference it by name.

<!-- res/values/dimens.xml -->
<resources>
  <dimen name="margin_medium">16dp</dimen>
</resources>

<!-- Good: dimension resource reference -->
<TextView android:layout_margin="@dimen/margin_medium" android:text="Item" />