Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't setup ktfmt via gradle according to docs #858

Closed
ColtonIdle opened this issue May 1, 2021 · 11 comments
Closed

Can't setup ktfmt via gradle according to docs #858

ColtonIdle opened this issue May 1, 2021 · 11 comments
Labels

Comments

@ColtonIdle
Copy link

I have an android project.

In my root build.gradle.kts I added the spotless plugin to my plugin block

then

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
    kotlin {
        ktfmt("0.24").dropboxStyle()
    }
}

If I run ./gradlew build I get an error of

* What went wrong:
Cannot add task 'clean' as a task with that name already exists.

Android projects by default come with this

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

so I removed that, and then I get a different error

* What went wrong:
A problem occurred configuring root project 'my-android-app'.
> Could not create task ':spotlessKotlinCheck'.
   > Could not create task ':spotlessKotlin'.
      > You must either specify 'target' manually or apply a kotlin plugin.
@ColtonIdle ColtonIdle changed the title Can't setup ktfmt according to docs Can't setup ktfmt via gradle according to docs May 1, 2021
@jbduncan
Copy link
Member

jbduncan commented May 1, 2021

These are both behaviours of Spotless that I've seen before.

My understanding is Spotless wasn't fully coded with Android in mind, so as a workaround, try adjusting your clean like so:

tasks.named("clean", Delete::class) {
    delete(rootProject.buildDir)
}

...so that it refers to the clean task from the Java plugin (sorry, I meant to say the Base plugin!), which Spotless imports indirectly but which the Android plugin does not.

And then, if it still reports that last error message, you'll need to tell Spotless where your Kotlin source files actually live with a "target". For example, if you have an app subproject, you can tell Spotless about it like so:

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
    kotlin {
        target("app/src/**/*.kt")
        ktfmt("0.24").dropboxStyle()
    }
}

I'm assuming that your configure block is in your root build.gradle.kts, if you have more than one. If the configure block is actually in e.g. your app/build.gradle.kts, then the "app/" of the target would go away, if I understand correctly.

I hope this helps!

Having said all this, this does sound like a bug, so @nedtwigg do you have any other thoughts?

@jbduncan
Copy link
Member

jbduncan commented May 1, 2021

@ColtonIdle I should add that this is all going from memory, so if it doesn't work as expected, please let us know!

@ColtonIdle
Copy link
Author

ColtonIdle commented May 1, 2021

@jbduncan AHHH It worked!

Thank you so much!

Quick questions if you don't mind.

  1. The tip about how to change the clean task was great. Do you think it's worth filing a bug for Android Studio to generate tasks.named instead of tasks.register? Or is this more-so a bug on spotless' part?
  2. Naming the targets was indeed my issue... but is there any way to just say "ALL .kt and .kts files?" I'll play around with this, but in general I'm curious if that's recommended or if that's shooting myself in the foot somehow. For example
        target("**/*.kt") <DID NOT WORK
        target("**/*.kts") <SEEMS TO WORK

Edit regarding question #2. I think I got it working with just target("**/*.kt", "**/*.kts")!

@jbduncan
Copy link
Member

jbduncan commented May 2, 2021

Good questions!

  1. It's a Spotless issue I reckon, because you only need to use tasks.named if clean exists already. The reason why it's there already because Spotless always imports it from Gradle's Base plugin.

Regardless, in most Android projects, clean comes out of the box as part of the root build.gradle, rather than with the Android plugin (for some reason). So raising a bug report about it might lead to the Android Devs saying "no", because if they fixed it then it would break most projects.

Feel free to raise a bug report about having clean be part of the Android plugin anyway, as it might encourage the devs to build clean into a future, breaking version of the plugin anyway, but I wouldn't count on it. :)

  1. Yep, that target works! Just be warned that it may be slow. So try app/src/**/*.kt (assuming all your Kotlin files live under "app/src") to speed things up a bit.

@jbduncan
Copy link
Member

jbduncan commented May 2, 2021

I'm closing this as it looks like everything is resolved now. If not, please let us know and we'll reopen.

@jbduncan jbduncan closed this as completed May 2, 2021
@jbduncan
Copy link
Member

jbduncan commented May 2, 2021

And of course I promptly forgot that I said this is likely a Spotless bug. Oops! Reopening.

@jbduncan jbduncan reopened this May 2, 2021
@ColtonIdle
Copy link
Author

@jbduncan thanks for the advice. Filed a bug here: https://issuetracker.google.com/issues/186924459

In regards to your advice of app/src/**/*.kt... I use multiple modules in this project. Maybe about 30 modules and so it seemed like this was the simple way of defining that I want to format all of my modules kt files.

@nedtwigg
Copy link
Member

nedtwigg commented Dec 5, 2021

#1014 fixes the conflict with the clean task.

@nedtwigg nedtwigg closed this as completed Dec 5, 2021
@nedtwigg
Copy link
Member

nedtwigg commented Dec 6, 2021

Fixed in plugin-gradle/6.0.3.

@ColtonIdle
Copy link
Author

@nedtwigg after all these years... the android studio team (because of a bug I filed) is going to get rid of the clean task in the template.

https://issuetracker.google.com/issues/186924459

Just posting this as a heads up. I'm not sure if it backfires with that latest PR you made to address this.

@nedtwigg
Copy link
Member

Thanks for the heads up! No backfire, Spotless is strictly more resilient now than it was before, so all good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants