kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Cannot generate external serializer: class is defined in another module

Open nvkleban opened this issue 1 year ago • 0 comments

Describe the bug Can't generate external serializer for a 3rd party Kotlin class in Android project. 3rd party class is like by definition class from "another module" why I can't serialize it that way?

Simplified example for problem:

I have Example class in :mylibrary module

data class Example(val name: String)

In :app module I try to encode it

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val example = Example("test")
        val text = Json.encodeToString(ExampleSerializer, example)
    }
}

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = Example::class)
object ExampleSerializer

Output: Error: Cannot generate external serializer 'ExampleSerializer': class 'Example' is defined in another module

To Reproduce serialization.zip

Expected behavior External serializer generated succesfully

Environment

  • Kotlin version: 1.9.20
  • Library version: plugin:1.9.20, json:1.6.1
  • Kotlin platforms: JVM
  • Gradle version: 8.4
  • IDE version: Android Studio Giraffe | 2022.3.1 Patch 4
  • Other relevant context: Mac OS, M1

nvkleban avatar Nov 21 '23 20:11 nvkleban