I have a class in Kotlin that looks like this:
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class AnnotationData (
public val description: String,
public val mid: String,
public val score: Float,
public val topicality: Float) {
companion object {
@JvmField
val DEFAULT = AnnotationData("","", 0.0f, 0.0f)
}
}
My Gradle file has this:
apply plugin: 'kotlin-kapt'
...
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.6.0'
I have this in another file...
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()
val annotationAdapter = moshi.adapter(AnnotationData::class.java)
val issue = annotationAdapter.fromJson(labelAnnotation.toString())
When I run the application (in Intellij FWIW) I see this:
Exception in thread "main" java.lang.RuntimeException: Failed to find the generated JsonAdapter class for class com.myapp.gcv.kotlin.AnnotationData
at com.squareup.moshi.StandardJsonAdapters.generatedAdapter(StandardJsonAdapters.java:249)
at com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:62)
at com.squareup.moshi.Moshi.adapter(Moshi.java:130)
at com.squareup.moshi.Moshi.adapter(Moshi.java:68)
at com.myapp.gcv.kotlin.GVProcessor.examineResponse(GVProcessor.kt:117)
at com.myapp.gcv.kotlin.GVProcessor.access$examineResponse(GVProcessor.kt:12)
at com.myapp.gcv.kotlin.GVProcessor$scanImage$1.doResume(GVProcessor.kt:93)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchTask.run(CoroutineDispatcher.kt:129)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:236)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:174)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:167)
at com.myapp.gcv.kotlin.GVProcessor.scanImage(GVProcessor.kt:58)
at com.myapp.gcv.kotlin.GVProcessor.processImage(GVProcessor.kt:36)
at com.myapp.gcv.kotlin.MainKt.main(Main.kt:43)
Caused by: java.lang.ClassNotFoundException: com.myapp.gcv.kotlin.AnnotationDataJsonAdapter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:374)
at com.squareup.moshi.StandardJsonAdapters.generatedAdapter(StandardJsonAdapters.java:236)
... 15 more
I cannot find any "AnnotationDataJsonAdapter" in the entire directory so I am assuming it really isn't being generated.
Note that I have tried removing the companion object in the AnnotationData class. Didn't help.
Even tried just this and it fails with the same thing on this class, so I don't think it's the data class itself causing the issue.
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class MoshiTestData (
public val test: String)
I got around it partially. Changed to this:
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(List::class.java, AnnotationData::class.java)
val jsonAdapter:JsonAdapter<List<AnnotationData>> = moshi.adapter(type)
val annotationData = jsonAdapter.fromJson(labelAnnotation.toString())
And then commented out this line so something still isn't quite as expected although it works without the auto-generation
@JsonClass(generateAdapter = true)
Doesn’t look like there’s further action required here.
@darrinps I had the same issue building for Android until I disabled minify. Now if only the provided Proguard rules options actually worked for me :/
...
buildTypes {
debug {
minifyEnabled false
// Uses new built-in shrinker http://tools.android.com/tech-docs/new-build-system/built-in-shrinker
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardTest-rules.pro'
}
...
Oh yeah, adding the kaptKotlin dirs to the src sets didn't work for me either (referenced in the Misc section of this article).
I'll note I've also had this problem, despite various attempts with Kotlin 1.2.70, I couldn't get the codegen working - it just didn't want to generate the adapter class. Had to revert to the reflection version like @darrinps.
@vyadh have you looked at the directories under build/generated/source/kaptKotlin? In my case an adapter was generated but not packaged into the APK unless I set minifyEnabled = false.
not packaged into the APK unless I set minifyEnabled = false
This means you’re missing the proguard rules needed for this, which can be found in the README
@hzsweers When I use the ProGuard rules I get the following error:
ProGuard configuration parser error:
Referring to this line:
-if @com.squareup.moshi.JsonClass class *
I don't urgently need ProGuard enabled atm, so disabling minify is good enough for me for now. Haven't had time to look further into it.
I've also added the kaptKotlin source sets as referenced in your Moshi's Kotlin Code Gen article.
Ah that’s the new proguard 6 rules. You’ll want the previous rules if you’re using an older one. See the change here: https://github.com/square/moshi/commit/4bbc5b2ff80646e1ec5e037e3de430f017b1173f
Thanks @pauledwardtang, but unfortunately those files are not being generated. I thought I'd chip in here in case it was a general problem, maybe with the version of Kotlin I'm using, but maybe I've not got something configured correctly...
Thanks @hzsweers, using the older ProGuard rules fixed the issue for me. I didn't even need to add the kaptKotlin source directories in Gradle!
@vyadh I was using 1.2.61 and recently switched to 1.2.70 and both generated sources for me just fine. Maybe there's something missing or misconfigured in your Gradle configuration?
Still not working in my case. If I remove the comment from this line:
//@JsonClass(generateAdapter=true)
...then try to run the code I see this:
Caused by: java.lang.RuntimeException: Failed to find the generated
JsonAdapter class for class com.wgvs.gcv.kotlin.AnnotationData
at
com.squareup.moshi.StandardJsonAdapters.generatedAdapter(StandardJsonAdapters.java:249)
at
com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:62)
at com.squareup.moshi.Moshi.adapter(Moshi.java:130)
at com.squareup.moshi.Moshi.adapter(Moshi.java:64)
I have this in my gradle:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.70'
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.apis:google-api-services-vision:v1-rev357-1.22.0'
compile 'com.google.code.findbugs:jsr305:2.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.0"
compile 'commons-io:commons-io:2.5'
compile 'io.github.microutils:kotlin-logging:1.6.10'
implementation 'com.squareup.moshi:moshi:1.6.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.6.0'
implementation 'com.squareup.moshi:moshi-adapters:1.6.0'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.6.0'
implementation "org.slf4j:slf4j-api:1.8.0-beta2"
implementation "org.slf4j:slf4j-simple:1.8.0-beta2"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
On Wed, Sep 19, 2018 at 7:39 AM Paul notifications@github.com wrote:
Thanks @hzsweers https://github.com/hzsweers, using the older ProGuard
rules fixed the issue for me. I didn't even need to add the kaptKotlin
source directories in Gradle!@vyadh https://github.com/vyadh I was using 1.2.61 and recently
switched to 1.2.70 and both generated sources for me just fine. Maybe
there's something missing or misconfigured in your Gradle configuration?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/646#issuecomment-422786731, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABWQTBacHV8EB6Mx9pCAF69gJovSccOgks5ucjsLgaJpZM4WeBei
.
@darrinps According to the readme:
val moshi =
Moshi.Builder()
// ... add your own JsonAdapters and factories ...
.add(KotlinJsonAdapterFactory())
.build()
Moshi’s adapters are ordered by precedence, so you always want to add the Kotlin adapter after your own custom adapters. Otherwise the KotlinJsonAdapterFactory will take precedence and your custom adapters will not be called.
I'm not sure how both reflection and codegen are supposed to work together (is this what you want?), but in your original post you're not adding the KotlinJsonAdapterFactory last before building.
I finally figured out my problem. So, in case it helps anyone else I'll say that my JsonClass annotated type was in my test folder (was messing about with stuff there before moving it later). Once I moved it to the main folder, everything worked as expected. Yay!
@vyadh Is it possible to put that class in test folder? I need that for testing purpose and don't want to put it in the main folder
@DangOnGitHub not sure if this is still a problem for you but if you have code in your test folder that needs to be generated make sure you tell kapt that it needs to check the test folder as well:
kaptTest "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
See more info here.
My problem: the adapters were not generated (in build/generated/source/kaptKotlin).
My solution: run 'gradlew build' in the terminal instead of using IntelliJ IDEA 2018.3.5 (Community Edition) 'Build Project'.
The strange thing is, that they were generated when I start using the moshi, but after some time it stopped working (even in different project).
Hope this will help somebody. BEST
My problem: the adapters were not generated (in build/generated/source/kaptKotlin).
My solution: run 'gradlew build' in the terminal instead of using IntelliJ IDEA 2018.3.5 (Community Edition) 'Build Project'.
The strange thing is, that they were generated when I start using the moshi, but after some time it stopped working (even in different project).
Hope this will help somebody. BEST
This solved it for me, running it from the command line. I deleted the generated files and tried running build from the gradle view in intellij and that also worked. Running the kaptKotlin task worked, as well.
Using any of the build options under the Build menu did not work, so either it's something in my intellij settings or it's a bug in intellij, or both; I'm on build IU#-192.5728.92 intellij ultimate btw.
I was adding _kapt 'com.squareup.moshi:moshi-kotlin-codegen:version'_
into core module but was missing it in feature module. That's why code-gen was not generating the adapters in feature module. Added the _kapt 'com.squareup.moshi:moshi-kotlin-codegen:version'_ into the feature module's gradle and it started generating Adapters without issue.
-dontwarn org.jetbrains.annotations.**
-keep class kotlin.Metadata { *; }
-keepclassmembers class com.myapp.packagnename.** {
<init>(...);
<fields>;}
-keep @android.support.annotation.Keep class * {*;}
Change com.myapp.packagnename to your package-name
Same issue here. Forgot to add this:
.add(KotlinJsonAdapterFactory())
Removed the @JsonAdapter(generateAdapter = true) and the error went away.
I got around it partially. Changed to this:
val moshi = Moshi.Builder().build() val type = Types.newParameterizedType(List::class.java, AnnotationData::class.java) val jsonAdapter:JsonAdapter<List<AnnotationData>> = moshi.adapter(type) val annotationData = jsonAdapter.fromJson(labelAnnotation.toString())And then commented out this line so something still isn't quite as expected although it works without the auto-generation
@JsonClass(generateAdapter = true)
Your comment helped me fix an error caused by the @JsonClass annotation and gave me an example of using ParametrizedType. Thanks🙏.
Most helpful comment
@DangOnGitHub not sure if this is still a problem for you but if you have code in your test folder that needs to be generated make sure you tell
kaptthat it needs to check the test folder as well:See more info here.