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

Please add a sample for how to apply kotlinter on a multi module Android project #256

Open
nuhkoca opened this issue Jun 21, 2022 · 10 comments

Comments

@nuhkoca
Copy link

nuhkoca commented Jun 21, 2022

Hello,

Thanks for the up-to-date ktlint plugin. This is what I am looking for nowadays. I set up the plugin well but still didn't understand how to exclude some packages in a multi module project. It was enough with ktlint-gradle to exclude project-wide packages within the plugin block itself like below

ktlint {
    exclude = ...
}

but for this one, from what I understand from the doc, I need to configure the task(lintKotlinMain, formatKotlinMain) created by the plugin in each module or is there a better and centralized way of doing so? Thank you!

@jeremymailen
Copy link
Owner

That is correct, a task exists for each SourceSet, so you need to customize them as shown here https://github.com/jeremymailen/kotlinter-gradle#customizing-tasks

As with all things gradle you can programmatically iterate through tasks of that type and apply the customization.

@nuhkoca
Copy link
Author

nuhkoca commented Jun 23, 2022

Thanks @jeremymailen. You mean like I can iterate through the respective LintTask inside allprojects block to configure all modules in one go, right?

allprojects {
    apply(plugin = "org.jmailen.kotlinter")

    repositories {
        google()
        mavenCentral()
    }

    kotlinter {
        ignoreFailures = false
        reporters = arrayOf("html")
        experimentalRules = true
        disabledRules = emptyArray()
    }

    tasks.withType<LintTask>().configureEach {
        exclude("**/generated/**", "**/build/**")
    }
}

@jeremymailen
Copy link
Owner

That's what I was thinking.

Do note when you are excluding file paths above the package root, you need to use a slight different incantation to exclude the path. Two styles:

#242 (comment)

#242 (comment)

@svenjacobs
Copy link

@jeremymailen I was struggling with finding the right syntax for excluding generated files.

Maybe one of the solutions in the comments that you mentioned could be added to the documentation?
I personally went with:

tasks.withType<LintTask>().configureEach {
    exclude { it.file.path.contains("/build/generated/") }
}

Thank you very much for this great Gradle plugin 👍🏼

@jeremymailen
Copy link
Owner

I agree, we should document a simple example like that to help people so they don't need to go digging.
This would be a great little PR for someone to contribute :).

@adityabhaskar
Copy link

I spent a few hours today figuring out how to set up the pre push hook task to a multi module project. A sample project would've been mightily helpful.

@amaranthius
Copy link

I spent a few hours today figuring out how to set up the pre push hook task to a multi module project. A sample project would've been mightily helpful.

+1 on this. It doesn't make any sense to apply the plugin to the root project of a multi-module/Android build configuration but even when I tried, it still didn't work. In order to add this to the root project (as described in the README):

tasks.check {
    dependsOn("installKotlinterPrePushHook")
}

you'd need to apply the com.android.application plugin as well (because that's where the check task comes from) and that triggers a whole other bunch of nightmares

@junerver
Copy link

junerver commented May 17, 2023

I spent a few hours today figuring out how to set up the pre push hook task to a multi module project. A sample project would've been mightily helpful.

+1 on this. It doesn't make any sense to apply the plugin to the root project of a multi-module/Android build configuration but even when I tried, it still didn't work. In order to add this to the root project (as described in the README):

tasks.check {
    dependsOn("installKotlinterPrePushHook")
}

you'd need to apply the com.android.application plugin as well (because that's where the check task comes from) and that triggers a whole other bunch of nightmares

Can you guide me how to use git hook in android project, I added it under the app/build.gradle.kts, but when I run the project or build, no file are generated under .git/hooks, thank you very much if you can help
Now I install this hook by manually running ./gradlew installKotlinterPrePushHook, which is a bit troublesome for multi-person cooperation

@jeremymailen
Copy link
Owner

I recommend using the git hooks that come with ktlint actually since a command line invocation makes a lot more sense in a git workflow. Likely hooks will be phased out of the next major release of this plugin.

Beyond that, if you want some kind of setup task in your root gradle file to install a hook and you don't otherwise have kotlin code there, I would recommend just making a custom task rather than relying on the suggestion to tie it to check.

@dessalines
Copy link

Did anyone figure this out? The above tasks.check doesn't work for android projects.

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

No branches or pull requests

7 participants