Skip to content

Commit

Permalink
Fix sample-viewmodel running (#430)
Browse files Browse the repository at this point in the history
```
Caused by: java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.example.molecule.viewmodel.PupperPicsApi$ListResponse.
Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior.
Please use KotlinJsonAdapterFactory from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.
  at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:98)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
```

Co-authored-by: Jake Wharton <[email protected]>
  • Loading branch information
Goooler and JakeWharton authored May 29, 2024
1 parent 2800bee commit 1d2642d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ buildscript {
classpath libs.android.plugin
classpath libs.kotlin.plugin.core
classpath libs.kotlin.plugin.compose
classpath libs.kotlin.ksp.plugin
classpath libs.maven.publish.plugin
classpath libs.dokka.plugin
classpath libs.spotless.plugin
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ junit = { module = "junit:junit", version = "4.13.2" }
kotlin-plugin-core = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-plugin-compose = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-ksp-plugin = "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:2.0.0-1.0.21"

kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutine" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutine" }
Expand All @@ -47,6 +48,7 @@ squareup-okhttp-logging-interceptor = { module = "com.squareup.okhttp3:logging-i
squareup-retrofit-client = { module = "com.squareup.retrofit2:retrofit", version.ref = "squareup-retrofit" }
squareup-retrofit-converter-scalars = { module = "com.squareup.retrofit2:converter-scalars", version.ref = "squareup-retrofit" }
squareup-retrofit-converter-moshi = { module = "com.squareup.retrofit2:converter-moshi", version.ref = "squareup-retrofit" }
squareup-moshi-codegen = "com.squareup.moshi:moshi-kotlin-codegen:1.15.1"

assertk = "com.willowtreeapps.assertk:assertk:0.28.1"
turbine = { module = "app.cash.turbine:turbine", version = "1.1.0" }
10 changes: 10 additions & 0 deletions renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
"extends": [
"config:base"
],
"packageRules": [
{
// KSP is tightly coupled to Kotlin version.
"groupName": "Kotlin/KSP",
"matchPackagePrefixes": [
"org.jetbrains.kotlin:kotlin",
"com.google.devtools.ksp",
],
},
],
}
3 changes: 3 additions & 0 deletions sample-viewmodel/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'org.jetbrains.kotlin.plugin.compose'
apply plugin: 'com.google.devtools.ksp'

dependencies {
implementation projects.moleculeRuntime
Expand All @@ -11,6 +12,8 @@ dependencies {
implementation libs.squareup.retrofit.client
implementation libs.squareup.retrofit.converter.moshi

ksp libs.squareup.moshi.codegen

testImplementation libs.junit
testImplementation libs.kotlinx.coroutines.test
testImplementation libs.turbine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.example.molecule.viewmodel

import com.squareup.moshi.JsonClass
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.create
Expand Down Expand Up @@ -50,13 +51,16 @@ fun PupperPicsService(): PupperPicsService {
}
}

private interface PupperPicsApi {
interface PupperPicsApi {
@GET("breeds/list/all")
suspend fun listBreeds(): ListResponse

@GET("breed/{breed}/images/random")
suspend fun randomImageFor(@Path("breed", encoded = true) breed: String): ImageResponse

@JsonClass(generateAdapter = true)
data class ListResponse(val message: Map<String, List<String>>)

@JsonClass(generateAdapter = true)
data class ImageResponse(val message: String)
}

0 comments on commit 1d2642d

Please sign in to comment.