android gradle spotless自动格式化代码

gradle依赖

项目根目录下的build.gradle添加spotless依赖


    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "com.diffplug.spotless:spotless-plugin-gradle:4.3.0"
    }

APP下的build.gradle添加如下代码

apply plugin: 'com.diffplug.gradle.spotless'

spotless {
    java {
        target project.fileTree(project.rootDir) {
            include '**/*.java'
            exclude 'app/build/generated/*'
        }
        googleJavaFormat()
    }
}

编译执行检查

./gradlew build会自动执行spotless,如果代码有需要格式化的,会有如下提示

Task :app:spotlessJavaCheck FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:app:spotlessJavaCheck’.

    Run ‘./gradlew :app:spotlessApply’ to fix these violations.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

按照提示执行./gradlew :app:spotlessApply

ok,格式化完成。

你可能感兴趣的:(Android)