0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Android] spotlessのビルド時自動実行が意外と便利かもしれない

Last updated at Posted at 2025-12-31

概要

この記事の設定をすると次のことが可能になる

  • spotlessによるフォーマット
    • ktlint
    • misc
    • kotlinGradle
  • ビルド時自動実行

導入

build.gradle.kts

plugins {
    alias(libs.plugins.spotless)
}

subprojects {
    apply(plugin = "com.diffplug.spotless")
    configure<com.diffplug.gradle.spotless.SpotlessExtension> {

        ratchetFrom("origin/main")
        kotlin {
            target("src/**/*.kt")
            ktlint(libs.versions.ktlint.get())
                .setEditorConfigPath("${rootProject.projectDir}/.editorconfig")
                .customRuleSets(
                    listOf(
                        libs.compose.rules.ktlint.get().toString(),
                    ),
                )
        }
        format("misc") {
            target("*.md", ".gitignore", "*.xml")
            trimTrailingWhitespace()
            endWithNewline()
        }

        kotlinGradle {
            target("*.gradle.kts")
            ktlint()
            trimTrailingWhitespace()
            endWithNewline()
        }
    }
    afterEvaluate {
        tasks.named("preBuild") {
            dependsOn("spotlessApply")
        }
    }
}


libs.versions.toml
[versions]
spotless = "8.1.0"
ktlint = "1.3.1"
composeRulesKtlint = "0.5.3"

[libraries]
# Ktlint Compose Rules - Spotlessのカスタムルールセットとして使用
compose-rules-ktlint = { module = "io.nlopez.compose.rules:ktlint", version.ref = "composeRulesKtlint" }

[plugins]
# Spotlessプラグイン - コードフォーマットツール
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }

雑談

  • subprojectsの中のapplyの記述ではlibs.versions.tomlが使えないらしい
  • pre-commitは作業の途中で一旦コミットしておくことができなくて使い勝手が悪かった
  • ビルド時自動実行は、作業の邪魔にならないし、忘れづらいからよき
  • 一緒にGitHubActionsのワークフローも作っておくと実行漏れなくて良さそう
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?