Repository: RBusarow/ModuleCheck (kotlin)

Issue:
Kotlin source dirs for Android modules aren't parsed
This refers to using `src/main/kotlin` instead of `src/main/java`.

This works just fine in `java-library` modules, whether the module in question is the dependent or the dependency.

For Android modules, the source dir needs to be added like this:

```groovy
android {
  sourceSets {
    androidTest.java.srcDirs += 'src/androidTest/kotlin'
    main.java.srcDirs += 'src/main/kotlin'
    debug.java.srcDirs += 'src/debug/kotlin'
    release.java.srcDirs += 'src/release/kotlin'
    test.java.srcDirs += 'src/test/kotlin'
  }
}
```

This isn't hooking into the `KotlinCompile` task or anything like that, so it is included in the configuration when ModuleCheck tasks run.

The `src/main/kotlin` files are invisible to the parser, so anything which is only used inside that directory will be reported as unused.  And if the Android module is a library, then anything which is _declared_ inside that directory won't be counted.

Pull request:
fixes #172

Patch:
diff --git a/modulecheck-plugin/src/main/kotlin/modulecheck/gradle/GradleProjectProvider.kt b/modulecheck-plugin/src/main/kotlin/modulecheck/gradle/GradleProjectProvider.kt
index 36656a4362..f73890361e 100644
--- a/modulecheck-plugin/src/main/kotlin/modulecheck/gradle/GradleProjectProvider.kt
+++ b/modulecheck-plugin/src/main/kotlin/modulecheck/gradle/GradleProjectProvider.kt
@@ -301,8 +301,9 @@ class GradleProjectProvider(
           .distinctBy { it.name }
           .map { sourceProvider ->
 
-            val jvmFiles = sourceProvider
-              .javaDirectories
+            val jvmFiles = with(sourceProvider) {
+              javaDirectories + kotlinDirectories
+            }
               .flatMap { it.listFiles().orEmpty().toList() }
               .toSet()
 


Test patch:
diff --git a/modulecheck-plugin/src/test/kotlin/modulecheck/gradle/UnusedDependenciesTest.kt b/modulecheck-plugin/src/test/kotlin/modulecheck/gradle/UnusedDependenciesTest.kt
index 9aec849b47..db114fd20b 100644
--- a/modulecheck-plugin/src/test/kotlin/modulecheck/gradle/UnusedDependenciesTest.kt
+++ b/modulecheck-plugin/src/test/kotlin/modulecheck/gradle/UnusedDependenciesTest.kt
@@ -456,19 +456,93 @@ class UnusedDependenciesTest : BaseTest() {
         ProjectSpec("lib-1") {
           addSrcSpec(
             ProjectSrcSpec(Path.of("src/main/java")) {
-              addFileSpec(
-                FileSpec.builder("com.example.lib1", "Lib1View")
-                  .addType(
-                    TypeSpec.classBuilder("Lib1View")
-                      .addAnnotation(
-                        AnnotationSpec
-                          .builder(ClassName.bestGuess("com.squareup.anvil.annotations.ContributesMultibinding"))
-                          .addMember("%T", ClassName.bestGuess("com.example.lib1.AppScope"))
-                          .build()
-                      )
-                      .build()
-                  )
-                  .build()
+              addRawFile(
+                "Lib1View.kt",
+                """
+                package com.example.lib1
+
+                import com.squareup.anvil.annotations.ContributesMultibinding
+
+                @ContributesMultibinding(AppScope)
+                public class Lib1View
+                """.trimIndent()
+              )
+            }
+          )
+          addBuildSpec(
+            ProjectBuildSpec {
+              addPlugin("kotlin(\"jvm\")")
+              addProjectDependency("api", jvmSub1)
+            }
+          )
+        }
+      )
+      addSettingsSpec(projectSettings.build())
+      addBuildSpec(
+        projectBuild
+          .addBlock(
+            """moduleCheck {
+            |  autoCorrect = false
+            |}
+          """.trimMargin()
+          ).build()
+      )
+    }
+      .writeIn(testProjectDir.toPath())
+
+    build("moduleCheckUnusedDependency").shouldSucceed()
+  }
+
+  @Test
+  fun `module with a declaration used in an android module with kotlin source directory should not be unused`() {
+    val appProject = ProjectSpec("app") {
+      addBuildSpec(
+        ProjectBuildSpec {
+          addPlugin("""id("com.android.library")""")
+          addPlugin("kotlin(\"android\")")
+          android = true
+          addProjectDependency("api", jvmSub1)
+        }
+      )
+      addSrcSpec(
+        ProjectSrcSpec(Path.of("src/main/kotlin")) {
+          addRawFile(
+            "MyModule.kt",
+            """
+            package com.example.app
+
+            import com.example.lib1.AppScope
+            import com.squareup.anvil.annotations.ContributesTo
+            import dagger.Module
+
+            @Module
+            @ContributesTo(AppScope::class)
+            object MyModule
+            """.trimIndent()
+          )
+        }
+      )
+    }
+
+    ProjectSpec("project") {
+      applyEach(projects) { project ->
+        addSubproject(project)
+      }
+      addSubproject(appProject)
+      addSubproject(
+        ProjectSpec("lib-1") {
+          addSrcSpec(
+            ProjectSrcSpec(Path.of("src/main/java")) {
+              addRawFile(
+                "AppScope.kt",
+                """
+                package com.example.lib1
+
+                import javax.inject.Scope
+                import kotlin.reflect.KClass
+
+                abstract class AppScope private constructor()
+                """.trimIndent()
               )
             }
           )