ktlint-gradle icon indicating copy to clipboard operation
ktlint-gradle copied to clipboard

Add documentation on how to add to an Android project?

Open ColtonIdle opened this issue 4 years ago • 3 comments

It might be good to add documentation on how to add this in an Android repository. As someone that's not super familiar with gradle, I'm used to most Android plugins directly calling out what goes into your root build.gradle and what goes into your project level build.gradle

Even the Android sample in the repo only shows a single build.gradle which is not representative of the majority of Android projects in my opinion. Although it's just a small piece of documentation to add, this would have saved me like 4+ hours (that's really long... I know 😞 ) because of all of the trial and error I did of figuring out where to actually put those blocks of code.

At least something like this would be nice?

Simple Android Setup Add maven { url "https://plugins.gradle.org/m2/" } to your root build.gradle in buildscript > repositories and classpath "org.jlleitschuh.gradle:ktlint-gradle:<current_version>" to your root build.gradle in buildscript > dependencies

buildscript {
  repositories {
    google()
    maven { url "https://plugins.gradle.org/m2/" }
  }
  dependencies {
    classpath "com.android.tools.build:gradle:AGP_VERSION"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:KOTLIN_VERSION"
    classpath "org.jlleitschuh.gradle:ktlint-gradle:<current_version>"
  }
}

Then apply the plugin in your root build.gradle via apply plugin: 'org.jlleitschuh.gradle.ktlint' and this will generate a few ktlint tasks, but it will not yet apply ktlint to your project. For that you must open up your app module and apply plugin: "org.jlleitschuh.gradle.ktlint" or you can apply it to all of your modules by setting up your root build.gradle as such:

subprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent
    
    // Optionally configure plugin
    ktlint {
       debug = true
    }
}

Again, this is just my opinion because I'm not familiar with gradle and the sample android app was very different than what comes from file > new project in Android Studio. Thank you

ColtonIdle avatar May 07 '20 16:05 ColtonIdle

@ColtonIdle would you be willing to contribute a sample or some changes to the README?

JLLeitschuh avatar Jun 15 '20 20:06 JLLeitschuh

@JLLeitschuh does my snippet above look good to you? If so, I can create a PR for it.

ColtonIdle avatar Jun 17 '20 15:06 ColtonIdle

I'd actually recommend the following, if possible.

plugins {
  id("org.jlleitschuh.gradle.ktlint").version("<current_version>").apply(false)
}

subprojects {
    apply(plugin="org.jlleitschuh.gradle.ktlint") // Version is inherited from parent
}

Or, for Groovy:

plugins {
  id "org.jlleitschuh.gradle.ktlint" version "<current_version>" apply false
}

subprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint" // Version is inherited from parent
}

JLLeitschuh avatar Jun 17 '20 16:06 JLLeitschuh