Android: Kotlin and Jetpack Compose
The android platform turns your tokens into Kotlin source a Jetpack
Compose app compiles: a shared file declaring a DesignTokens class
that names every token as a typed member (colours, spacing, durations,
curves, fonts, shadows) and takes the runtime axes as parameters, and, for
each app, a DesignColorValues object holding that app’s colours for
light and dark, and optionally a Material 3 colour scheme per appearance.
Android has no asset catalog, so colours are data: DesignColors is a data
class with one Color per colour, and each app’s DesignColorValues
supplies the values. One DesignTokens class serves every app; the colours
you hand it decide which app it is.
This guide is for teams with an Android app (or several apps sharing one
design system) who want colours and values generated from the token files
rather than copied by hand. Output is byte-stable, so airframe build --check can guard it in CI.
1. Configure the platform
Section titled “1. Configure the platform”Add an android platform to airframe.yaml. The repository’s
testdata/android directory holds a complete example, with two apps,
notes and tasks:
modes: appearance: {values: [light, dark], binding: runtime, web: {light: ":root", dark: [":root[data-theme='dark']"]}} density: {values: [compact, standard, relaxed], base: standard, binding: runtime, web: {attr: data-density}} app: {values: [notes, tasks], base: notes, binding: build, web: {attr: data-app}} channel: {values: [stable, next], base: stable, binding: build, web: {attr: data-channel}}platforms: android: emit: [kotlin] out: {kotlin: "android/{app}/src/main/kotlin/DesignColorValues.kt"} fanOut: app options: package: com.example.design sharedOut: android/shared/src/main/kotlin/DesignTokens.kt material3Scheme: {primary: accent, onPrimary: button-ink, tertiary: highlight}emit: [kotlin]is the one format.outis where each app’sDesignColorValues.ktis written (repo-relative). Without it, it goes under the content directory, ingenerated/android/.{app}is a placeholder for a value of thefanOutaxis.fanOutnames abinding: buildaxis (here, which app is being built). The platform writes oneDesignColorValues.ktper value of that axis; a colour that differs per app is howaccentisblue.500in notes andgreen.500in tasks.channelis a second build-bound axis that is not fanned out, so Android gets its base value,stable; see Whatemit-axis-fixedmeans.naming(top level) sets spellings. The default for Kotlin isDesignTokens.{camel}: the tokenbutton-bgisbuttonBgonDesignTokens. Change the type part to rename the class; the colour types follow it, less a trailingTokens(AppTokens.{camel}givesAppColorsandAppColorValues). The member part is the token name in a case ({camel}or{Pascal}); a template that yields something that is not a Kotlin identifier, such as{name}(hyphens:button-bg), fails naming the token.
Options
Section titled “Options”| Option | Value | Default |
|---|---|---|
package |
The Kotlin package both files declare, as in com.example.design. Required. |
none |
sharedOut |
Repo-relative path of the shared DesignTokens.kt. Required when out contains {app}. It names one file, written once, so it cannot be a directory (a trailing /) or contain { or }, {app} included. |
none: the shared declarations open the values file, which then must not fan out |
colorSpace |
srgb: Color(0xAARRGGBB). display-p3: Color(red =, green =, blue =, alpha =, colorSpace = ColorSpaces.DisplayP3). |
srgb |
gamut |
How a colour outside colorSpace is brought in. css-color-4: CSS Color 4 gamut mapping (reduce chroma, keep lightness and hue). clip: clip each channel independently (can shift hue). |
css-color-4 |
material3Scheme |
A map from Material 3 ColorScheme roles to colour tokens, as in {primary: accent}. Writes lightScheme and darkScheme into each app’s DesignColorValues. Roles you leave out keep Material’s defaults. |
none: no schemes |
An unknown option, a role that is not a ColorScheme parameter, or a value
of the wrong kind is schema-invalid, naming the key.
Build it:
airframe buildwrote android/notes/src/main/kotlin/DesignColorValues.ktwrote android/shared/src/main/kotlin/DesignTokens.ktwrote android/tasks/src/main/kotlin/DesignColorValues.kt2. Add the outputs to each app
Section titled “2. Add the outputs to each app”The generated files are plain source; Airframe never touches your Gradle build.
- Put the shared
DesignTokens.ktwhere every app can compile it: a shared library module, or a source directory each app adds. - Put each app’s
DesignColorValues.ktin that app’s module only, since every app’s file declares the samecom.example.design.DesignColorValues.
The files use Compose’s ui-graphics, ui-unit, ui-text and
animation-core (and material3 when material3Scheme is set), which a
Compose app already has.
3. Use it
Section titled “3. Use it”DesignTokens takes the colours and each runtime axis:
package com.example.notes
import com.example.design.DesignColorValuesimport com.example.design.DesignTokens
val tokens = DesignTokens(DesignColorValues.light)colors comes first and has no default; every runtime axis is a parameter
defaulted to its base (appearance = Appearance.Light, density = Density.Standard). In a composable, choose dark with
isSystemInDarkTheme(). Pass the matching appearance too: colours come
from colors, but a non-colour token can vary on appearance as well (here
hairline), and that one reads the parameter:
package com.example.notes
import androidx.compose.foundation.isSystemInDarkThemeimport androidx.compose.runtime.Composableimport com.example.design.DesignColorValuesimport com.example.design.DesignTokens
@Composablefun designTokens(density: DesignTokens.Density = DesignTokens.Density.Standard): DesignTokens { val dark = isSystemInDarkTheme() return DesignTokens( colors = if (dark) DesignColorValues.dark else DesignColorValues.light, appearance = if (dark) DesignTokens.Appearance.Dark else DesignTokens.Appearance.Light, density = density, )}To hand the tokens down a tree, provide that value through your own
CompositionLocal, as you would any theme object.
Static and instance members
Section titled “Static and instance members”A non-colour token that does not depend on a runtime axis is a member of
the companion object, so it needs no instance: DesignTokens.space1,
DesignTokens.durationFast. Every colour, and every token that varies on a
runtime axis (here density: gap and surface change with it), is an
instance member, because it depends on the colours and state you hold.
Every static member also has an instance forwarder, so tokens.space1
works too and code can read every token through one value without
remembering which is which:
package com.example.notes
import androidx.compose.ui.graphics.Colorimport androidx.compose.ui.unit.Dpimport com.example.design.DesignColorValuesimport com.example.design.DesignTokensimport kotlin.time.Duration
val fast: Duration = DesignTokens.durationFastval padding: Dp = DesignTokens.space1
val compact = DesignTokens(DesignColorValues.dark, density = DesignTokens.Density.Compact)val sameFast: Duration = compact.durationFastval gap: Dp = compact.gapval surface: Color = compact.surfaceMaterial 3
Section titled “Material 3”With material3Scheme set, each app’s DesignColorValues also holds
lightScheme and darkScheme, built with lightColorScheme and
darkColorScheme from the roles you mapped. Hand one to MaterialTheme:
package com.example.notes
import androidx.compose.foundation.isSystemInDarkThemeimport androidx.compose.material3.MaterialThemeimport androidx.compose.runtime.Composableimport com.example.design.DesignColorValues
@Composablefun NotesTheme(content: @Composable () -> Unit) { val dark = isSystemInDarkTheme() MaterialTheme( colorScheme = if (dark) DesignColorValues.darkScheme else DesignColorValues.lightScheme, content = content, )}With no appearance axis there is lightScheme alone.
What emit-axis-fixed means
Section titled “What emit-axis-fixed means”airframe.yaml: warn emit-axis-fixed: platform android: channel is build-bound but not fanned out; using its base value stableA build-bound axis is decided when the app is built. The platform writes
one output per value of its fanOut axis only; any other build-bound axis
(here channel) has no output of its own, so the tokens that vary on it
are resolved at its base value and the other values never reach Android.
The warning says so once per axis rather than dropping them silently. To
fix it, fan it out, make it binding: runtime (it becomes an enum and a
DesignTokens parameter), or accept the base: the warning is non-fatal.
See emit-axis-fixed.
Limits
Section titled “Limits”The Kotlin emitter fails closed: a value the platform cannot express is an
emit-failed error naming the token, never a guess. The other platforms
still build.
- Non-colour tokens are shared across apps. The
DesignTokensclass is written once, so a spacing, duration or shadow geometry that differs per app fails (token "gutter" varies per app; Kotlin non-colour tokens are shared). Only colours, including a shadow’s colour, may vary per app. Make the token the same everywhere, or keep it out ofkotlin’semit. - Material 3 role tokens must be colour tokens emitted to
kotlin(a token whoseemitlist leaveskotlinout fails, naming the role), and must not vary on a runtime axis other thanappearance: a scheme has one colour per role per appearance, so a role mapped tosurface, which varies on density, fails naming the axis. A role token may vary per app; each app’s scheme gets its own value. The appearance axis must belightanddarkonly. - Dimensions are
dp, notsp. Every dimension token is aDp:pxas is,remtimes 16. Text that should follow the user’s font scale needs anspyou convert yourself. - Package segments are ASCII identifiers. Each dot-separated segment of
packagemust match[A-Za-z_][A-Za-z0-9_]*and not be a Kotlin keyword (com.in.appandcom.2x.appareschema-invalid). Airframe does not backtick segments. - Identifier collisions. Two tokens that map to one Kotlin identifier
or one colour slot fail with both names; rename one of the tokens. A
name starting with a digit is fine (
2xlbecomes_2xl), and a member named like a keyword is backticked.
airframe lint --format gate # the source and the generated filesairframe build --check # exit 1, naming each generated file that is staleCommit the generated outputs; emitted-stale and build --check name any
file that no longer matches the tokens. A colour edit names each app’s
DesignColorValues.kt; a non-colour edit names the shared DesignTokens.kt.