Closed vinnorman closed 4 years ago
The mockwebserver and OkHttp module need to be on the same version. If you add a dependency on okhttp 4.4.0 it should fix. Or use the okhttp-bom feature and get your versions managed centrally.
I get the same error adding this dependency:
implementation("com.squareup.okhttp3:okhttp:4.4.0")
Also doesn't explain why it works on kotlin("android") and not kotlin("jvm").
Any ideas?
Ok the plot thickens a little. The specific dependency that breaks it is actually another module. So I have a 'models' module, that I add as follows:
implementation(project(":models"))
And that module's dependencies look like this:
plugins {
kotlin("jvm")
kotlin("kapt")
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61")
implementation("com.squareup.moshi:moshi-kotlin:1.9.2")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.2")
}
And just having the implementation(project(":models"))
definitely breaks it, and causes the NoSuchFieldError: Companion
So unless I'm doing something wrong here, this is definitely a bug, and I think the issue should be reopened.
When I remove this dependency from my ":models" module, the error stops:
implementation("com.squareup.moshi:moshi-kotlin:1.9.2")
So, to be specific, MockWebServer returns a NoSuchFieldError: Companion
when there is a dependency to another module that has this dependency: implementation("com.squareup.moshi:moshi-kotlin:1.9.2")
I have managed to reproduce exactly this behaviour in a brand new project. Any guidance would be appreciated, thanks!
@vinnorman I ran into java.lang.NoSuchFieldError: Companion
as well. My situation was that I was using a certain version of okhttp in my project, but I have another dependency that internally it was using a different version of okhttp (so 2 different versions). I ended up using gradle's dependencyInsight
to track down the different versions.
Usage:
cd
into your subproject and run: gradle -q dependencyInsight --dependency okhttp3
Sample Output:
com.squareup.okhttp3:okhttp:3.10.0 -> 4.4.1
From there you can try excluding a dependency or forcing a particular version in your gradle file
Apparently this can be triggered even in a blank project that just uses HttpsURLConnection
. When I try to install the org.jetbrains.kotlin.plugin.serialization
plugin:
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.android']
at ...
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin 'org.jetbrains.kotlin.android'.
...
Caused by: org.gradle.api.reflect.ObjectInstantiationException: Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension.
...
Caused by: java.lang.NoSuchFieldError: Companion
at org.jetbrains.kotlin.gradle.dsl.ToolchainSupport$Companion.createToolchain$kotlin_gradle_plugin(ToolchainDsl.kt:33)
at org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension.<init>(KotlinProjectExtension.kt:69)
at org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension.<init>(KotlinProjectExtension.kt:109)
...
I have a modularised project, where the api layer is separated into its own module. As this has no dependency on the Android SDK, I have made it a kotlin only module. This has the kotlin jvm plugin as shown below in the build file. This results in a java.lang.NoSuchFieldError: Companion error when I try to use MockWebServer (only this, everything else works fine).
Using kotlin dsl in the build file, the build.gradle.kts file looks like this:
And here is the snippet of my unit test. The line where I instantiate MockWebServer() fails with the NoSuchFieldError: Companion:
And the stack trace:
After various experiments, I have discovered the following:
Working build.gradle.kts:
My instinct tells me it's some difference between the way kotlin android compiles the Kotlin, with Companion objects or static methods etc., compared with when just using the kotlin jvm compiler, but I confess to not knowing enough about what happens 'under the hood' with all this.