Skip to content

Commit b72e78b

Browse files
henriquenfariavanniktech
authored andcommitted
Add support for kotlin multiplatform plugin (#141)
1 parent 37fba63 commit b72e78b

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
compileOnly 'com.android.tools.build:gradle:3.1.2'
4444

4545
testCompile 'com.android.tools.build:gradle:3.1.2'
46+
testCompile 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11'
4647
testCompile 'junit:junit:4.12'
4748
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4', { exclude module: "groovy-all" } // Use localGroovy()
4849
}

src/main/groovy/com/vanniktech/android/junit/jacoco/GenerationPlugin.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class GenerationPlugin implements Plugin<Project> {
195195
"**/intermediates/javac/${sourceName}/*/classes/**" // Android Gradle Plugin 3.2.x support.
196196
]
197197

198-
if (isKotlinAndroid(subProject)) {
198+
if (isKotlinAndroid(subProject) || isKotlinMultiplatform(subProject)) {
199199
classPaths << "**/tmp/kotlin-classes/${sourcePath}/**"
200200
if (productFlavorName) {
201201
classPaths << "**/tmp/kotlin-classes/${productFlavorName}${buildTypeName.capitalize()}/**"
@@ -356,6 +356,10 @@ class GenerationPlugin implements Plugin<Project> {
356356
return project.plugins.hasPlugin('org.jetbrains.kotlin.android')
357357
}
358358

359+
protected static boolean isKotlinMultiplatform(final Project project) {
360+
return project.plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')
361+
}
362+
359363
protected static boolean isAndroidApplication(final Project project) {
360364
return project.plugins.hasPlugin('com.android.application')
361365
}

src/test/groovy/com/vanniktech/android/junit/jacoco/GenerationTest.groovy

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import org.gradle.testing.jacoco.plugins.JacocoPlugin
66
import org.gradle.testing.jacoco.tasks.JacocoReport
77
import org.junit.Test
88

9+
import java.nio.file.Paths
10+
911
import static com.vanniktech.android.junit.jacoco.ProjectHelper.ProjectType.*
1012

1113
class GenerationTest {
@@ -269,15 +271,22 @@ class GenerationTest {
269271
}
270272

271273
assert reports.xml.enabled
272-
assert reports.xml.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.xml"
274+
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.xml")
273275
assert reports.csv.enabled
274-
assert reports.csv.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.csv"
276+
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.csv")
275277
assert reports.html.enabled
276-
assert reports.html.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}"
278+
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}")
277279

278280
assert classDirectories.dir == project.file("build/")
279281
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString()])
280282

283+
if (hasKotlin(project)) {
284+
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString(),
285+
"**/tmp/kotlin-classes/${buildType}/**".toString(), "**/tmp/kotlin-classes/${flavor}${buildType.capitalize()}/**".toString()])
286+
} else {
287+
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString()])
288+
}
289+
281290
assert taskDependsOn(task, "test${flavor.capitalize()}${buildType.capitalize()}UnitTest")
282291
assert taskDependsOn(project.tasks.findByName('check'), "jacocoTestReport${flavor.capitalize()}${buildType.capitalize()}")
283292
}
@@ -311,14 +320,18 @@ class GenerationTest {
311320
}
312321

313322
assert reports.xml.enabled
314-
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug/jacoco.xml'
323+
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug/jacoco.xml")
315324
assert reports.csv.enabled
316-
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug/jacoco.csv'
325+
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug/jacoco.csv")
317326
assert reports.html.enabled
318-
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug'
327+
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug")
319328

320329
assert classDirectories.dir == project.file("build/")
321-
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
330+
if (hasKotlin(project)) {
331+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**', '**/tmp/kotlin-classes/debug/**'])
332+
} else {
333+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
334+
}
322335

323336
assert taskDependsOn(debugTask, 'testDebugUnitTest')
324337
assert taskDependsOn(project.tasks.findByName('check'), 'jacocoTestReportDebug')
@@ -347,14 +360,18 @@ class GenerationTest {
347360
}
348361

349362
assert reports.xml.enabled
350-
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug/jacoco.xml'
363+
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug/jacoco.xml')
351364
assert reports.csv.enabled
352-
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug/jacoco.csv'
365+
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug/jacoco.csv')
353366
assert reports.html.enabled
354-
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug'
367+
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug')
355368

356369
assert classDirectories.dir == project.file("build/")
357-
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
370+
if (hasKotlin(project)) {
371+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**', '**/tmp/kotlin-classes/debug/**'])
372+
} else {
373+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
374+
}
358375

359376
assert taskDependsOn(debugTaskCombined, 'testDebugUnitTest')
360377
assert taskDependsOn(debugTaskCombined, 'createDebugCoverageReport')
@@ -387,14 +404,18 @@ class GenerationTest {
387404
}
388405

389406
assert reports.xml.enabled
390-
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release/jacoco.xml'
407+
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release/jacoco.xml')
391408
assert reports.csv.enabled
392-
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release/jacoco.csv'
409+
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release/jacoco.csv')
393410
assert reports.html.enabled
394-
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release'
411+
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release')
395412

396413
assert classDirectories.dir == project.file("build/")
397-
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
414+
if (hasKotlin(project)) {
415+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**', '**/tmp/kotlin-classes/release/**'])
416+
} else {
417+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
418+
}
398419

399420
assert taskDependsOn(releaseTask, 'testReleaseUnitTest')
400421
assert taskDependsOn(project.tasks.findByName('check'), 'jacocoTestReportRelease')
@@ -424,14 +445,18 @@ class GenerationTest {
424445
}
425446

426447
assert reports.xml.enabled
427-
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release/jacoco.xml'
448+
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release/jacoco.xml')
428449
assert reports.csv.enabled
429-
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release/jacoco.csv'
450+
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release/jacoco.csv')
430451
assert reports.html.enabled
431-
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release'
452+
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release')
432453

433454
assert classDirectories.dir == project.file("build/")
434-
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
455+
if (hasKotlin(project)) {
456+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**', '**/tmp/kotlin-classes/release/**'])
457+
} else {
458+
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
459+
}
435460

436461
assert taskDependsOn(releaseTaskCombined, 'testReleaseUnitTest')
437462
assert taskDependsOn(releaseTaskCombined, 'createReleaseCoverageReport')
@@ -496,6 +521,10 @@ class GenerationTest {
496521
return false
497522
}
498523

524+
static boolean hasKotlin(Project project) {
525+
return project.plugins.hasPlugin('org.jetbrains.kotlin.android') || project.plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')
526+
}
527+
499528
@Test void getExcludesDefault() {
500529
final def excludes = GenerationPlugin.getExcludes(new JunitJacocoExtension())
501530

src/test/groovy/com/vanniktech/android/junit/jacoco/ProjectHelper.groovy

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final class ProjectHelper {
3838
project = builder.withName('java').build()
3939
break
4040
case ProjectType.ANDROID_APPLICATION:
41+
case ProjectType.ANDROID_KOTLIN_APPLICATION:
4142
project = builder.withName('android app').build()
4243
def androidMock = new MockFor(AppExtension)
4344
def buildTypesMock = ["debug", "release"].collect { bt ->
@@ -62,6 +63,7 @@ final class ProjectHelper {
6263
break
6364
case ProjectType.ANDROID_LIBRARY:
6465
case ProjectType.ANDROID_FEATURE:
66+
case ProjectType.ANDROID_KOTLIN_MULTIPLATFORM:
6567
project = builder.withName('android library').build()
6668
def androidMock = new MockFor(LibraryExtension)
6769
def buildTypesMock = ["debug", "release"].collect { bt ->
@@ -93,8 +95,12 @@ final class ProjectHelper {
9395
break
9496
}
9597

96-
if (projectType.pluginName != null) {
97-
project.plugins.apply(projectType.pluginName)
98+
if (projectType.pluginNames != null) {
99+
for (String pluginName : projectType.pluginNames) {
100+
if (pluginName) {
101+
project.plugins.apply(pluginName)
102+
}
103+
}
98104
}
99105
}
100106

@@ -153,16 +159,18 @@ final class ProjectHelper {
153159

154160
enum ProjectType {
155161
ANDROID_APPLICATION('com.android.application'),
162+
ANDROID_KOTLIN_APPLICATION('com.android.application', 'org.jetbrains.kotlin.android'),
163+
ANDROID_KOTLIN_MULTIPLATFORM('com.android.library', 'org.jetbrains.kotlin.multiplatform'),
156164
ANDROID_LIBRARY('com.android.library'),
157165
ANDROID_FEATURE('com.android.feature'),
158166
ANDROID_TEST('com.android.test'),
159167
JAVA('java'),
160168
ROOT(null)
161169

162-
private final String pluginName
170+
private final String[] pluginNames
163171

164-
ProjectType(String pluginName) {
165-
this.pluginName = pluginName
172+
ProjectType(String... pluginNames) {
173+
this.pluginNames = pluginNames
166174
}
167175
}
168176
}

0 commit comments

Comments
 (0)