lint: ban direct compose animation in app modules
- Commit
b3f5bad85873bad3db688702502ba6cdd0053439- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-31 16:15 UTC (3 months ago)
- Parents
2b47a35
Diff
CONTRIBUTING.md
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 7846627ae4..9973114f38 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -73,6 +73,7 @@ not blocking**. | `./gradlew :compileDevKotlin` | **Yes** | The only automated PR gate | | `./gradlew checkMetadata` | **Yes** | First step of the release workflow | | `ToastUsage` lint | **Yes** (`fatal`) | Local `./gradlew lint` only — CI builds with `-x lint` |+| `DirectComposeAnimation` lint | **Yes** (`fatal`) | Local `./gradlew lint` only — CI builds with `-x lint` | | The other 6 custom lint rules | No | Reported by `./gradlew lint` | | `scripts/*.sh` audits | No | Manual only | | Unit tests | No | CI builds with `-x test` |@@ -286,7 +287,7 @@ no suitable glyph, put a 24dp vector in `library`'s res and wrap `painterResourc ### Never use `Toast` -This is the one lint rule that fails the build. Toasts render outside the app's Material+This is one of two lint rules marked `fatal`. Toasts render outside the app's Material surface, ignore the theme, can't carry an action, and are silently suppressed when the app is backgrounded on Android 12+ — so the case people reach for a Toast in, reporting from background work, is the case where it may never appear.@@ -298,6 +299,34 @@ AppMessages.show(...) // from a ViewModel, Worker, or Activity Use a notification when the message has to outlive the current screen. +### Never animate directly with `androidx.compose.animation`++The other `fatal` rule, `DirectComposeAnimation`. Motion written at a call site carries no+intent: the same interaction picks up a different duration and curve on each screen, and a spec+written with no arguments silently takes Compose's own default instead of the motion scheme the+app is themed with. Reach for the helper that names the interaction:++| Instead of | Use | From |+| :--- | :--- | :--- |+| `animateDpAsState` / `animateFloatAsState` / `animateColorAsState` | `animatedDp` / `animatedFloat` / `animatedColor` | `library.ui` |+| `AnimatedVisibility` + `fadeIn`/`scaleIn`/`expandVertically` | `FadeVisibility` / `PopVisibility` / `ExpandVisibility` / `BannerVisibility` | `library.ui` |+| `AnimatedContent` | `SwappedContent` / `SwappedTopBar` | `library.ui` |+| `Modifier.animateItem(...)` | `itemMotion()` | `library.ui` |+| a press-shape `animateIntAsState` | `pressedShape(pressed)` | `library.ui` |+| a hand-rolled entrance | `Modifier.staggeredEntrance(index, arriving)` | `library.ui` |+| a shared element across a nav change | `sharedText` / `sharedContainer` / `SharedEditableText` / `MorphPage()` | `library.util` |++Imperative one-off motion on `androidx.compose.animation.core.Animatable` is **not** flagged — a+game board wave or a camera capture flash is genuinely bespoke, and wrapping it would be a+passthrough that names nothing. `com.vayunmathur.library.*` is exempt because the helpers are+built there, and `com.vayunmathur.launcher.*` is exempt because its home screen is built on+`animateBounds` and `Animatable`. Anything else that genuinely needs the raw API opts out per+file with the exact marker:++```kotlin+// RAW ANIMATION EXCEPTION: <reason>+```+ ### Screens are stateless; a binder wires the ViewModel A `*UiContract.kt` next to the ViewModel in `platform/` declares the state and callbacks. The
build-logic/src/main/kotlin/common-conventions-app.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts b/build-logic/src/main/kotlin/common-conventions-app.gradle.ktsindex e9f77f8142..67bb1767ca 100644--- a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-app.gradle.kts@@ -108,7 +108,9 @@ configure<com.android.build.api.dsl.ApplicationExtension> { disable += listOf("MissingTranslation") // Toast is banned repo-wide; this one fails the build even though // abortOnError is off for everything else. See :lint-rules.- fatal += listOf("ToastUsage")+ // DirectComposeAnimation keeps motion in the shared helpers, so the same+ // interaction cannot pick up a different duration on every screen.+ fatal += listOf("ToastUsage", "DirectComposeAnimation") } // Every app declares the same res/resources.properties (unqualifiedResLocale) for
lint-rules/src/main/java/com/vayunmathur/lint/DirectComposeAnimationDetector.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/DirectComposeAnimationDetector.kt b/lint-rules/src/main/java/com/vayunmathur/lint/DirectComposeAnimationDetector.ktnew file mode 100644index 0000000000..8f184d6159--- /dev/null+++ b/lint-rules/src/main/java/com/vayunmathur/lint/DirectComposeAnimationDetector.kt@@ -0,0 +1,174 @@+package com.vayunmathur.lint++import com.android.tools.lint.client.api.UElementHandler+import com.android.tools.lint.detector.api.Category+import com.android.tools.lint.detector.api.Detector+import com.android.tools.lint.detector.api.Implementation+import com.android.tools.lint.detector.api.Issue+import com.android.tools.lint.detector.api.JavaContext+import com.android.tools.lint.detector.api.Scope+import com.android.tools.lint.detector.api.Severity+import com.android.tools.lint.detector.api.SourceCodeScanner+import org.jetbrains.kotlin.psi.KtFile+import org.jetbrains.uast.UElement+import org.jetbrains.uast.UImportStatement++/**+ * Flags an app module reaching for Compose's declarative animation APIs directly.+ *+ * Motion that is written at the call site carries no intent, so the same interaction ends up with a+ * different duration and curve on every screen and the app stops feeling like one app. The shared+ * helpers name the interaction instead - `animatedDp`, `FadeVisibility`, `itemMotion`,+ * `sharedText`, `MorphPage` - and put the timing in one place where it can be tuned once.+ *+ * Only the declarative layer is flagged: the `AnimatedVisibility` / `fadeIn` / `animate*AsState`+ * family, which is where that drift happens. Imperative motion built on+ * [androidx.compose.animation.core.Animatable] is deliberately allowed - a game board wave or a+ * camera flash is genuinely one-off, and wrapping it in the library would be a passthrough that+ * names nothing.+ *+ * Scope: app modules only. `com.vayunmathur.library.*` is where the helpers live and is exempt, and+ * so is `com.vayunmathur.launcher.*`, which drives its home screen from `animateBounds` and+ * `Animatable` and declares the dependency explicitly for that reason.+ *+ * Per-file opt-out: a file carrying `// RAW ANIMATION EXCEPTION: <reason>` is skipped.+ */+class DirectComposeAnimationDetector : Detector(), SourceCodeScanner {++ override fun getApplicableUastTypes(): List<Class<out UElement>> =+ listOf(UImportStatement::class.java)++ override fun createUastHandler(context: JavaContext): UElementHandler? {+ val packageName = context.uastFile?.packageName ?: return null+ if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return null+ if (isExemptPackage(packageName)) return null++ // Documented per-file opt-out, matching the repo's other escape hatches.+ val fileText = (context.uastFile?.sourcePsi as? KtFile)?.text+ if (fileText != null && fileText.contains(EXCEPTION_MARKER)) return null++ return object : UElementHandler() {+ override fun visitImportStatement(node: UImportStatement) {+ val reference = node.importReference?.asSourceString() ?: return+ val banned = bannedSymbolIn(reference) ?: return+ context.report(+ ISSUE,+ node,+ context.getLocation(node as UElement),+ "`$banned` must not be used directly in an app module. Use the motion helpers " ++ "in `com.vayunmathur.library.ui` (animatedDp / animatedFloat / " ++ "animatedColor / pressedShape / itemMotion / staggeredEntrance, " ++ "FadeVisibility / PopVisibility / ExpandVisibility / SwappedContent) or " ++ "`com.vayunmathur.library.util` for navigation and shared-element motion, " ++ "so the timing is named once instead of re-derived per screen. Imperative " ++ "one-off motion on Animatable is allowed - mark the file with " ++ "`$EXCEPTION_MARKER <reason>` if that is what this is.",+ )+ }+ }+ }++ companion object {+ private const val APP_PACKAGE_PREFIX = "com.vayunmathur."+ private const val LIBRARY_PREFIX = "com.vayunmathur.library"++ /**+ * The launcher's home screen is built on `animateBounds` and `Animatable`, which have no+ * shared equivalent, and it declares `androidx-compose-animation` explicitly for that.+ * Exempted wholesale rather than per file so its drag-and-drop code is not littered with+ * markers.+ */+ private const val LAUNCHER_PREFIX = "com.vayunmathur.launcher"++ const val EXCEPTION_MARKER = "// RAW ANIMATION EXCEPTION:"++ private const val ANIMATION_PACKAGE = "androidx.compose.animation"++ /**+ * The declarative surface. Matched on the imported name rather than the package, because+ * these live across both `androidx.compose.animation` and `androidx.compose.animation.core`+ * while their imperative neighbours - Animatable, tween, spring, the easings - stay allowed.+ */+ private val BANNED_SYMBOLS = listOf(+ "AnimatedVisibility",+ "AnimatedContent",+ "Crossfade",+ "animateColorAsState",+ "animateDpAsState",+ "animateFloatAsState",+ "animateIntAsState",+ "animateIntOffsetAsState",+ "animateOffsetAsState",+ "animateSizeAsState",+ "animateValueAsState",+ "fadeIn",+ "fadeOut",+ "scaleIn",+ "scaleOut",+ "slideIn",+ "slideInHorizontally",+ "slideInVertically",+ "slideOut",+ "slideOutHorizontally",+ "slideOutVertically",+ "expandHorizontally",+ "expandIn",+ "expandVertically",+ "shrinkHorizontally",+ "shrinkOut",+ "shrinkVertically",+ "togetherWith",+ )++ private fun isExemptPackage(packageName: String): Boolean =+ packageName == LIBRARY_PREFIX ||+ packageName.startsWith("$LIBRARY_PREFIX.") ||+ packageName == LAUNCHER_PREFIX ||+ packageName.startsWith("$LAUNCHER_PREFIX.")++ /** The banned symbol this import refers to, or null if the import is fine. */+ private fun bannedSymbolIn(reference: String): String? {+ if (!reference.startsWith(ANIMATION_PACKAGE)) return null+ // A star import pulls the whole declarative surface in with it.+ if (reference == "$ANIMATION_PACKAGE.*") return "$ANIMATION_PACKAGE.*"+ val symbol = reference.substringAfterLast('.')+ return BANNED_SYMBOLS.firstOrNull { it == symbol }?.let { "$ANIMATION_PACKAGE...$it" }+ }++ val ISSUE: Issue = Issue.create(+ id = "DirectComposeAnimation",+ briefDescription = "Compose animation API used directly in an app module",+ explanation = """+ App modules must not use Compose's declarative animation APIs directly — \+ AnimatedVisibility, AnimatedContent, Crossfade, the `animate*AsState` family, or \+ the `fadeIn`/`scaleIn`/`slideIn`/`expandVertically` enter-exit builders.++ Motion written at a call site carries no intent. The same interaction picks up a \+ different duration and curve on each screen, and a spec written with no arguments \+ silently takes Compose's own default rather than the motion scheme the app is \+ themed with. The shared helpers name the interaction instead and hold the timing \+ in one place: `animatedDp`, `animatedFloat`, `animatedColor`, `pressedShape`, \+ `itemMotion` and `staggeredEntrance` in com.vayunmathur.library.ui, the \+ `FadeVisibility` / `PopVisibility` / `ExpandVisibility` / `SwappedContent` \+ wrappers beside them, and `sharedText` / `sharedContainer` / `MorphPage` in \+ com.vayunmathur.library.util for navigation and shared-element motion.++ Imperative motion on androidx.compose.animation.core.Animatable is NOT flagged. A \+ game board wave or a camera capture flash is genuinely one-off, and wrapping it \+ would be a passthrough that names nothing.++ com.vayunmathur.library.* is exempt because the helpers are built there, and \+ com.vayunmathur.launcher.* is exempt because its home screen is built on \+ animateBounds and Animatable. For anything else that genuinely needs the raw API, \+ add the marker comment `// RAW ANIMATION EXCEPTION: <reason>` to the file.+ """.trimIndent(),+ category = Category.CORRECTNESS,+ priority = 6,+ severity = Severity.ERROR,+ implementation = Implementation(+ DirectComposeAnimationDetector::class.java,+ Scope.JAVA_FILE_SCOPE,+ ),+ )+ }+}
lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt b/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.ktindex 45a6c25ba5..91b0af0a71 100644--- a/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt+++ b/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt@@ -11,6 +11,7 @@ class ModernAppsIssueRegistry : IssueRegistry() { override val issues: List<Issue> = listOf( ToastDetector.ISSUE, DirectBuildDatabaseDetector.ISSUE,+ DirectComposeAnimationDetector.ISSUE, OneComposablePerFileDetector.ISSUE, PackageStructureDetector.ISSUE, RawScaffoldInAppDetector.ISSUE,