From 80ea397e81e0d37ef23699b78da354e8b38fd7d0 Mon Sep 17 00:00:00 2001 From: Qizhao Chen Date: Fri, 23 Aug 2024 22:01:52 +0100 Subject: [PATCH] Fix 'Type mismatch' error --- .../gradle/AndroidMultiplatformTasks.kt | 20 +++++++++++------- .../gradle/AndroidSourceGenerator.kt | 21 +++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/plugin/main/src/kotlinx/benchmark/gradle/AndroidMultiplatformTasks.kt b/plugin/main/src/kotlinx/benchmark/gradle/AndroidMultiplatformTasks.kt index 13c8eef1..8c2d9d41 100644 --- a/plugin/main/src/kotlinx/benchmark/gradle/AndroidMultiplatformTasks.kt +++ b/plugin/main/src/kotlinx/benchmark/gradle/AndroidMultiplatformTasks.kt @@ -155,13 +155,8 @@ private fun Project.createAndroidBenchmarkExecTask(target: AndroidBenchmarkTarge .redirectError(ProcessBuilder.Redirect.PIPE) .start() - val outputGobbler = StreamGobbler(process.inputStream) { line -> - if (line.contains("Iteration") || line.contains("run finished")) { - println(line) - } - } - - val errorGobbler = StreamGobbler(process.errorStream) { System.err.println(it) } + val outputGobbler = StreamGobbler(process.inputStream) { } + val errorGobbler = StreamGobbler(process.errorStream) { } outputGobbler.start() errorGobbler.start() @@ -184,14 +179,23 @@ private fun Project.createAndroidBenchmarkExecTask(target: AndroidBenchmarkTarge private fun captureLogcatOutput() { try { - val logcatProcess = ProcessBuilder("adb", "logcat", "-v", "time") + val logcatProcess = ProcessBuilder("adb", "logcat", "TestRunner:D", "KotlinBenchmark:D", "*:S") .redirectErrorStream(true) .start() val logcatGobbler = StreamGobbler(logcatProcess.inputStream) { line -> when { + line.contains("started") -> + println( + "Android: " + + line.substringAfter("started: ") + .substringBefore("(") + .replace(Regex("\\[\\d+: "), "[") + ) + line.contains("Iteration") -> println(line.substring(line.indexOf("Iteration"))) line.contains("run finished") -> println(line.substring(line.indexOf("run finished"))) + line.contains("finished") -> println() } } diff --git a/plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt b/plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt index 3f8dce44..38bd7930 100644 --- a/plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt +++ b/plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt @@ -39,6 +39,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro .addImport("org.junit.runner", "RunWith") .addImport("androidx.benchmark", "BenchmarkState") .addImport("androidx.benchmark", "ExperimentalBenchmarkStateApi") + .addImport("android.util", "Log") if (descriptor.hasSetupOrTeardownMethods()) { fileSpecBuilder @@ -68,6 +69,7 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc .addImport("androidx.benchmark", "BenchmarkState") .addImport("androidx.benchmark", "ExperimentalBenchmarkStateApi") .addImport("org.junit", "Test") + .addImport("android.util", "Log") if (descriptor.hasSetupOrTeardownMethods()) { fileSpecBuilder @@ -75,12 +77,6 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc .addImport("org.junit", "After") } - fileSpecBuilder.addAnnotation( - AnnotationSpec.builder(ClassName("org.junit.runner", "RunWith")) - .addMember("%T::class", ClassName("org.junit.runners", "Parameterized")) - .build() - ) - // Generate constructor val constructorSpec = FunSpec.constructorBuilder() val paramFields = descriptor.getSpecificField(paramAnnotationFQN) @@ -89,6 +85,11 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc } val typeSpecBuilder = TypeSpec.classBuilder(descriptorName) + .addAnnotation( + AnnotationSpec.builder(ClassName("org.junit.runner", "RunWith")) + .addMember("%T::class", ClassName("org.junit.runners", "Parameterized")) + .build() + ) .primaryConstructor(constructorSpec.build()) .addProperties(paramFields.map { param -> PropertySpec.builder(param.name, getTypeName(param.type)) @@ -114,7 +115,7 @@ private fun generateParametersFunction(paramFields: List") - .addStatement("println(\"Iteration \${index + 1}: \$time ns\")") + .addStatement("Log.d(\"KotlinBenchmark\", \"Iteration \${index + 1}: \$time ns\")") .endControlFlow() typeSpecBuilder.addFunction(methodSpecBuilder.build()) @@ -307,6 +305,7 @@ private fun getTypeName(type: String): TypeName { "char" -> Char::class.asTypeName() "byte" -> Byte::class.asTypeName() "short" -> Short::class.asTypeName() + "java.lang.String" -> String::class.asTypeName() else -> ClassName.bestGuess(type) } } \ No newline at end of file