Two "issues" I added detect to my android project ...
# detekt
c
Two "issues" I added detect to my android project via the plugins block in my root build.gradle. 1. Now I try to run
./gradlew detektMain
, but I get an error:
Task 'detektMain' not found in root project 'rollertoaster'.
2. If I run ./gradlew detekt, I get a success message in 1s, but I don't think detekt actually analyzed my code.
Copy code
The following Kotlin source sets were configured but not added to any Kotlin compilation:
 * androidAndroidTestRelease
 * androidTestFixtures
 * androidTestFixturesDebug
 * androidTestFixturesRelease
You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
See <https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets>

BUILD SUCCESSFUL in 1s
3. I can't add detekt into spotless right? I use spotless in order to run ktfmt, so being able to add detekt into spotless would be cool. Can I get help on those three?
c
Interesting. 1+2: Running detektAll doesn't work for me 3. Looks like there's no reason to configure detekt inside of spotless
e
there is no detektAll task, "It can be simply implemented like `...`". either do that or run the specific detekt tasks you want
c
Oh. I saw "I think we could add a 
detektAll
 task that can depend on all the 
detekt*
 tasks." and thought they implemented
Added detekt to a plain android app (not KMM) and running ./gradlew detekt succeeds, but I can't find any output and it finishes in less than 1 second. not sure if it's actually running.
e
ditto,
detektDebug
etc.
although I'm surprised that
detekt
wouldn't work in an Android app, the sources should be in the right place by default...
c
Yeah. Anyone got ideas for what I should do?
g
So for 1:
via the plugins block in my root build.gradle.
That’s actually the problem. If you add Detekt to your root project
plugins{}
, you’re applying to a non-Android project. Therefore all the Android specific tasks will not be added.
for 2: You can still run with
--info
and see if actually something is happening.
But yeah it’s ok that is fast. The plain
detekt
task is just running syntactic analysis so it will be quite fast.
for 3: No idea honestly. But happy to support if someone is looking into it.
c
@gammax 1. oh. well apparently I misinterpreted the docs. LET ME TRY NOW! 2. Cool. 3. It's fine for now. Was just curious if someone else had the same experieience.
@gammax hm. Docs seem pretty clear about it going into my root/project level build.gradle
g
Docs says “make sure to have detekt configured in the project level
build.gradle
file” no? They never mention root 🤔
c
All other docs for libraries I've used say "project level" as root, and app level as the android app module. Even plugins from the android team use that same nomenclature.
Maybe I'm just an idiot though. Didn't think that project level meant app directory instead of project directory.
e
for what it's worth, I do like to add an extra Detekt task in the root project which includes all the kotlin buildscripts
c
I only need to have it run on like 2 of 8 modules. So I'm currently trying to figure that out. Lol
g
We tend to refer to
project
as Gradle do. So what in Android you would call a
module
for Gradle is a `project`: https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:creating_multi_project_builds
it run on like 2 of 8 modules.
Either you use
subprojects{}
and you put an
if
inside. Or you just apply the plugin only to the modules that should use it (recommended).
c
And then I'd run detekt via
./gradle :module2:detekt
?
g
correct 👍
Or you can just
./gradle detekt
which will call
detekt
on all your submodules (if you don’t have a top level
detekt
task).
c
Oooh. That's nice. But all of the results will still be placed into the modulename/build/whatever right?
Cool. Okay. I'm excited. I will get this integrated into our projects CI today/tomorrow!
And just for reference... since I setup some plugins on this project recently... this is why I thought "project" == "root build.gradle"
g
Yeah I agree that the whole “project” thing in Gradle is really confusing 😞
☝️ 1
But all of the results will still be placed into the modulename/build/whatever right?
Correct
c
Awesome. Thank you for the help Nicola. I also love the podcast. Keep it up!
❤️ 1
e
@Colton Idle I had a similar issue, and my projects include the following in a plugin to fix it (I think I found this somewhere in a GitHub issue):
Copy code
val artifactType = Attribute.of("artifactType", String::class.java)

class DisambiguationRule : AttributeDisambiguationRule<String> {
  override fun execute(t: MultipleCandidatesDetails<String>) {
    /**
     * The default disambiguation rules exclude the jar variants because they define the `org.gradle.libraryelements`
     *
     * See <https://github.com/gradle/gradle/blob/master/subprojects/dependency-management/src/main/java/org/gradle/internal/component/model/MultipleCandidateMatcher.java#L221>
     *
     * Not 100% sure why this happens but forcing to use the jar in this specific case fixes the issue
     */
    val jar = t.candidateValues.firstOrNull { it == "jar" }
    if(jar != null) {
      t.closestMatch(jar)
    }
  }
}

dependencies.attributesSchema.attribute(artifactType).disambiguationRules.add(DisambiguationRule::class.java)e