How to install Ktlint in windows

3.7k Views Asked by At

What I am trying to do: I am trying to install ktlint on windows locally

What I tried:

Used below command in command prompt

curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.38.1/ktlint && chmod a+x ktlint

Also tried above command by using in below steps in link

https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/

What is happening: Not able to install in windows.

How to install in windows ?

2

There are 2 best solutions below

2
On

I was having trouble installing ktlint for Windows 10. What worked for me was going to the Releases page, downloading the ktlint file, renaming the ktlint file to ktlint.jar and adding it inside my Android project root folder (in reference to this GitHub issue) (Note: Java must be installed so that the file extension can be recognized), then running java -jar ktlint.jar --apply-to-idea-project (in the command line, from the Android project root directory). Then finally I was able to get it to work, resulting in the following text:

The following files are going to be updated:

        .\.idea\codeStyles\codeStyleConfig.xml
        .\.idea\codeStyles\Project.xml
        .\.idea\inspectionProfiles\profiles_settings.xml
        .\.idea\inspectionProfiles\ktlint.xml
        .\.idea\workspace.xml

Do you wish to proceed? [y/n]
(in future, use -y flag if you wish to skip confirmation)
y
(updated)

Please restart your IDE
(if you experience any issues please report them at https://github.com/shyiko/ktlint)

Without updating the file to have the .jar extension and adding .jar to the filename command line parameter, I kept getting the following error:

Error: Unable to access jarfile ktlint

To run ktlint immediately, run java -jar ktlint.jar in the command line (in the directory where the ktlint.jar file resides).

You can install a git hook to automatically check files for style violations on commit by entering the following command in the Command Line (run "ktlint installGitPrePushHook" if you wish to run ktlint on push instead): java -jar ktlint.jar installGitPreCommitHook

But this will only allow you to set hook for your current command line session. If you close out of Command Prompt, the settings will be reset. If you want to have the ktlinter run on every session for your project, you have to add the java -jar ktlint.jar command to your project's .git/hook/pre-commit file and paste the ktlint.jar file into the .git/hook folder as well.

0
On

Download the ktlint jar ffile from the lib and add it in your project

Run java -jar ktlint on the command line

From that point and then gradlew ktlintCheck should work