Skip to content

Commit ee44992

Browse files
committed
Move Kotlin integration to a task-based approach
Even though adding an additional test root directory for Kotlin sources might be the cleaner way, the IDE won't detect the tests by itself. Therefore, this introduces the manual "copy task" workaround, which moves the Kotlin test classes to a location where both the command line and IDEs will pick them up.
1 parent 81cd4f5 commit ee44992

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

android-junit5/src/main/groovy/de/mannodermaus/gradle/plugins/android_junit5/AndroidJUnit5Test.groovy

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ class AndroidJUnit5Test extends JavaExec {
117117
// e.g. "build/intermediates/classes/test/debug/..."
118118
variant.unitTestVariant.variantData.scope.javaOutputDir]
119119

120-
if (config.kotlinPluginApplied) {
121-
testRootDirs += "$project.buildDir/tmp/kotlin-classes/${variant.name}UnitTest"
122-
}
123-
124120
project.logger.info(
125121
"$AndroidJUnitPlatformPlugin.LOG_TAG: Assembled JUnit 5 Task '$task.name':")
126122
testRootDirs.each {

android-junit5/src/main/groovy/de/mannodermaus/gradle/plugins/android_junit5/AndroidJUnitPlatformPlugin.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package de.mannodermaus.gradle.plugins.android_junit5
33
import com.android.build.gradle.api.BaseVariant
44
import de.mannodermaus.gradle.plugins.android_junit5.jacoco.AndroidJUnit5JacocoExtension
55
import de.mannodermaus.gradle.plugins.android_junit5.jacoco.AndroidJUnit5JacocoReport
6+
import de.mannodermaus.gradle.plugins.android_junit5.kotlin.AndroidJUnit5CopyKotlin
67
import org.gradle.api.GradleException
78
import org.gradle.api.Plugin
89
import org.gradle.api.Project
@@ -198,13 +199,18 @@ class AndroidJUnitPlatformPlugin implements Plugin<Project> {
198199
def testVariants = project.android[allVariants].findAll { it.hasProperty("unitTestVariant") }
199200

200201
def isJacocoApplied = projectConfig.jacocoPluginApplied
202+
def isKotlinApplied = projectConfig.kotlinPluginApplied
201203

202204
testVariants.each { variant ->
203205
def testTask = AndroidJUnit5Test.create(projectConfig, variant as BaseVariant)
204206

205207
if (isJacocoApplied) {
206208
AndroidJUnit5JacocoReport.create(project, testTask)
207209
}
210+
211+
if (isKotlinApplied) {
212+
AndroidJUnit5CopyKotlin.create(project, testTask)
213+
}
208214
}
209215
}
210216
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package de.mannodermaus.gradle.plugins.android_junit5.kotlin
2+
3+
import com.android.build.gradle.api.BaseVariant
4+
import com.android.build.gradle.internal.scope.TaskConfigAction
5+
import com.android.build.gradle.internal.scope.VariantScope
6+
import de.mannodermaus.gradle.plugins.android_junit5.AndroidJUnit5Test
7+
import org.gradle.api.Project
8+
import org.gradle.api.plugins.JavaBasePlugin
9+
import org.gradle.api.tasks.Copy
10+
11+
class AndroidJUnit5CopyKotlin extends Copy {
12+
13+
static AndroidJUnit5CopyKotlin create(Project project, AndroidJUnit5Test testTask) {
14+
def configAction = new ConfigAction(project, testTask)
15+
return project.tasks.create(configAction.getName(), configAction.getType(), configAction)
16+
}
17+
18+
static class ConfigAction implements TaskConfigAction<AndroidJUnit5CopyKotlin> {
19+
20+
private static final String TASK_NAME_DEFAULT = "copyKotlinUnitTestClasses"
21+
private static final String TASK_GROUP = JavaBasePlugin.VERIFICATION_GROUP
22+
23+
private final Project project
24+
private final AndroidJUnit5Test testTask
25+
private final BaseVariant variant
26+
private final VariantScope scope
27+
28+
ConfigAction(Project project, AndroidJUnit5Test testTask) {
29+
this.project = project
30+
this.testTask = testTask
31+
this.variant = testTask.variant
32+
this.scope = variant.variantData.scope
33+
}
34+
35+
@Override
36+
String getName() {
37+
return scope.getTaskName(TASK_NAME_DEFAULT)
38+
}
39+
40+
@Override
41+
Class<AndroidJUnit5CopyKotlin> getType() {
42+
return AndroidJUnit5CopyKotlin.class
43+
}
44+
45+
@Override
46+
void execute(AndroidJUnit5CopyKotlin copyTask) {
47+
copyTask.from "$project.buildDir/tmp/kotlin-classes/${variant.name}UnitTest"
48+
copyTask.into "$project.buildDir/intermediates/classes/test/$variant.name"
49+
copyTask.group = TASK_GROUP
50+
51+
testTask.dependsOn copyTask
52+
}
53+
}
54+
}

android-junit5/src/test/groovy/de/mannodermaus/gradle/plugins/android_junit5/BasePluginSpec.groovy

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ abstract class BasePluginSpec extends Specification {
277277
project.tasks.findByName("jacocoTestReportRelease") == null
278278
}
279279

280-
def "Presence of Kotlin plugin will add a dedicated Test Root Directory"() {
280+
def "Application: Kotlin Integration"() {
281281
when:
282282
def project = factory.newProject(rootProject())
283283
.asAndroidApplication()
@@ -286,12 +286,46 @@ abstract class BasePluginSpec extends Specification {
286286
.buildAndEvaluate()
287287

288288
then:
289-
def runDebug = project.tasks.getByName("junitPlatformTestDebug") as AndroidJUnit5Test
290-
def debugClasspath = runDebug.args[runDebug.args.indexOf("--scan-class-path") + 1]
291-
assert debugClasspath.contains("/build/tmp/kotlin-classes/debugUnitTest")
289+
project.tasks.getByName("copyKotlinUnitTestClassesDebug")
290+
project.tasks.getByName("copyKotlinUnitTestClassesRelease")
291+
}
292+
293+
def "Library: Kotlin Integration"() {
294+
when:
295+
def project = factory.newProject(rootProject())
296+
.asAndroidLibrary()
297+
.applyJunit5Plugin()
298+
.applyKotlinPlugin()
299+
.buildAndEvaluate()
300+
301+
then:
302+
project.tasks.getByName("copyKotlinUnitTestClassesDebug")
303+
project.tasks.getByName("copyKotlinUnitTestClassesRelease")
304+
}
292305

293-
def runRelease = project.tasks.getByName("junitPlatformTestRelease") as AndroidJUnit5Test
294-
def releaseClasspath = runRelease.args[runRelease.args.indexOf("--scan-class-path") + 1]
295-
assert releaseClasspath.contains("/build/tmp/kotlin-classes/releaseUnitTest")
306+
def "Application: Kotlin Tasks not added if plugin absent"() {
307+
when:
308+
def project = factory.newProject(rootProject())
309+
.asAndroidApplication()
310+
.applyJunit5Plugin()
311+
.applyKotlinPlugin(false)
312+
.buildAndEvaluate()
313+
314+
then:
315+
project.tasks.findByName("copyKotlinUnitTestClassesDebug") == null
316+
project.tasks.findByName("copyKotlinUnitTestClassesRelease") == null
317+
}
318+
319+
def "Library: Kotlin Tasks not added if plugin absent"() {
320+
when:
321+
def project = factory.newProject(rootProject())
322+
.asAndroidLibrary()
323+
.applyJunit5Plugin()
324+
.applyKotlinPlugin(false)
325+
.buildAndEvaluate()
326+
327+
then:
328+
project.tasks.findByName("copyKotlinUnitTestClassesDebug") == null
329+
project.tasks.findByName("copyKotlinUnitTestClassesRelease") == null
296330
}
297331
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android.injected.build.model.only.versioned = 3
55

66
# Artifact configuration (common)
77
GROUP_ID = de.mannodermaus.gradle.plugins
8-
VERSION_NAME = 1.0.12-SNAPSHOT
8+
VERSION_NAME = 1.0.11-SNAPSHOT
99
VCS_URL = https://github.com/mannodermaus/android-junit5
1010

1111
# Artifact configuration (plugin)

0 commit comments

Comments
 (0)