title: How do I add KtLint style to Android Studio using Gradle?
body: <p>I want to add the style <code>ktlint</code> uses to Android Studio so that when I apply formatting myself it uses the <code>ktlint</code> style.</p>
  
  <p>Based on the documentation, I installed the <code>ktlint</code> CLI</p>
  
  <pre><code>brew install ktlint 
  </code></pre>
  
  <p>I then navigated to the root of my project and executed</p>
  
  <pre><code>ktlint --android applyToIDEAProject
  </code></pre>
  
  <p>The style now appears in my preferences for use.</p>
  
  <p>The issue is that the style applied through CLI uses what I think is the latest version of <code>ktlint</code> since my manual formatting is different from the formatting <code>Gradle</code> task.I would prefer if it used the style from the <code>Gradle</code> plugin so the style applied to the project is the same one the <code>Gradle</code> task uses when formatting. </p>
  
  <p>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. </p>
  
  <p>Below is my 'ktlint' Gradle file</p>
  
  <pre><code>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"
  }
  </code></pre>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>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. <a href="https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintApplyToIdeaTask.kt" rel="nofollow noreferrer">https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintApplyToIdeaTask.kt</a> </p>
  
  <p>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 <a href="https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt#L49" rel="nofollow noreferrer">https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt#L49</a></p>
  
  <p>From this information, I have added this Gradle task which has progressed me but still fails.</p>
  
  <pre><code>task addKtLintStyle(type: JavaExec, group: "formatting") {
      description = "yep"
      classpath = configurations.ktlint
      main = "com.pinterest.ktlint.Main"
      args "--android", "applyToIDEA"
  }
  </code></pre>
  
  <p>It fails with this error</p>
  
  <pre><code>.idea directory not found. Are you sure you are inside project root directory? 
  </code></pre>
  
  <p>Which is thrown here </p>
  
  <p><a href="https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEACommandHelper.kt#L27" rel="nofollow noreferrer">https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEACommandHelper.kt#L27</a></p>
  
  <p>My gradle file is in the root of my project so I am not sure what to do next at this point.</p>