如何使用Gradle将KtLint样式添加到Android Studio? [英] How do I add KtLint style to Android Studio using Gradle?

查看:409
本文介绍了如何使用Gradle将KtLint样式添加到Android Studio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ktlint所使用的样式添加到Android Studio中,以便在我自己设置格式时使用ktlint样式.

I want to add the style ktlint uses to Android Studio so that when I apply formatting myself it uses the ktlint style.

根据文档,我安装了ktlint CLI

Based on the documentation, I installed the ktlint CLI

brew install ktlint 

然后我导航到项目的根目录并执行

I then navigated to the root of my project and executed

ktlint --android applyToIDEAProject

样式现在出现在我的使用偏好中.

The style now appears in my preferences for use.

问题在于通过CLI应用的样式使用的是我认为是ktlint的最新版本,因为我的手动格式与格式化Gradle任务不同.如果使用Gradle中的样式,我会更喜欢>插件,因此应用于项目的样式与Gradle任务在格式化时使用的样式相同.

The issue is that the style applied through CLI uses what I think is the latest version of ktlint since my manual formatting is different from the formatting Gradle task.I would prefer if it used the style from the Gradle plugin so the style applied to the project is the same one the Gradle task uses when formatting.

最后,我希望它成为Gradle任务,以便其他开发人员可以导入和应用与创建钩子相同的样式.

Finally, I would like it to be a Gradle task so that other developers can import and apply the same style as I could create a hook.

下面是我的"ktlint" Gradle文件

Below is my 'ktlint' Gradle file

dependencies {
    ktlint "com.pinterest:ktlint:0.34.2"
}


task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "src/**/*.kt"
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "-F", "src/**/*.kt"
}

更新1

我查看了可用的Gradle插件之一,并看到了该文件.对我来说,它看起来像是主库的包装器,因此在不使用3rd party插件的情况下必须能够实现. https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintApplyToIdeaTask.kt

I looked into one of the Gradle plugins available and saw this file. To me it looks like a wrapper around the main library so it must be possible without using the 3rd party plugin. https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintApplyToIdeaTask.kt

我使用的版本是0.34.2,并将其与最新版本进行比较,似乎稍后添加了对这些命令的支持,如此处所示

The version I was using is 0.34.2 and comparing that to the latest, it looks like support for those commands were added later as seen here https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt#L49

根据这些信息,我添加了此Gradle任务,该任务使我进步了,但仍然失败了.

From this information, I have added this Gradle task which has progressed me but still fails.

task addKtLintStyle(type: JavaExec, group: "formatting") {
    description = "yep"
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "--android", "applyToIDEA"
}

它因此错误而失败

.idea directory not found. Are you sure you are inside project root directory? 

哪个扔在这里

我的gradle文件位于我的项目的根目录中,因此我不确定下一步该怎么做.

My gradle file is in the root of my project so I am not sure what to do next at this point.

推荐答案

我设法使它起作用.这是最终的任务定义

I managed to get this to work. This was the final task definition

task addKtLintStyle(type: JavaExec, group: "formatting") {
    description = "Adds The KtLint Style To Your IDE"
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "--android", "applyToIDEA", "-y"
    //Point to the root directory because this task needs access to the .idea directory
    workingDir(getRootDir())
}

注意该行

    //Point to the root directory because this task needs access to the .idea directory
    workingDir(getRootDir())

还有

    args "--android", "applyToIDEA", "-y"

-y仅接受应用的样式.

重新启动Android Studio后,我可以看到ktlint样式

After restarting Android Studio, I am able to see the ktlint style

这篇关于如何使用Gradle将KtLint样式添加到Android Studio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆