Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoSuchMethodError when classes are in different modules #576

Open
lallam opened this issue Oct 11, 2019 · 35 comments
Open

java.lang.NoSuchMethodError when classes are in different modules #576

lallam opened this issue Oct 11, 2019 · 35 comments

Comments

@lallam
Copy link

lallam commented Oct 11, 2019

Runtime Crash

java.lang.NoSuchMethodError: No direct method (ILkotlinx/serialization/SerializationConstructorMarker;)V in class Lcom/xxxx/common/core/Model; or its super classes (declaration of 'com.xxxx.common.core.Model' appears in /data/app/com.xxxx.demo-PV-n86-hzEl-eyc8UqbACQ==/base.apk!classes7.dex)
10-11 15:30:10.048 E/AndroidRuntime(25760): at com.xxxx.reg.data.User.(Unknown Source:9)
10-11 15:30:10.048 E/AndroidRuntime(25760): at com.xxxx.reg.data.User$$serializer.deserialize(Unknown Source:624)
10-11 15:30:10.048 E/AndroidRuntime(25760): at com.xxxx.reg.data.User$$serializer.deserialize(User.kt:17)
10-11 15:30:10.048 E/AndroidRuntime(25760): at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:33)

To Reproduce

@serializable
abstract class Model(
var uniqueId: String = "",
}

@serializable
data class User(
var userName: String = "",
var dateOfBirth: String = "",
) : Model()

Library A - Model
Library B - User

I am getting the above crash.

BUT if I move the Model from Library A to Library B NO issues.
[Just an FYI]
if the variable uniqueId from Model is removed no issue
if @serializable is removed from Model no issues

Expected behavior
Not crash or throw fatal error.

Environment

  • Kotlin version: [e.g. 1.3.50]
  • Library version: [e.g. 0.13.0]

Updated Post with More comments and Link to Code - #576 (comment)

@lallam
Copy link
Author

lallam commented Jan 6, 2020

any update on this or verfied this as a known issue or has been fixed?

a class extended from either same library or a different library should yield same results.

@sandwwraith
Copy link
Member

Sorry, seems like this issue slipped out of my sight. Can confirm this bug on 1.3.60 with mutli-module (library/app) setup

@lallam
Copy link
Author

lallam commented Jan 27, 2020

@sandwwraith

Yes, this is still an issue.

We are using

Kotlin - 1.3.61
Kotlin Serialization - 0.14.0

@JavierSegoviaCordoba
Copy link

Kotlin 1.3.61 and Kotlin Serialization 0.14.0 is working for me, but with Kotlin 1.3.70:

Caused by: java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/String;Lkotlinx/serialization/internal/GeneratedSerializer;I)V in class Lkotlinx/serialization/internal/SerialClassDescImpl; or its super classes (declaration of 'kotlinx.serialization.internal.SerialClassDescImpl' appears in /data/app/com.javiersc.gitGut-9pedF-cZ8kBfR8wDIz6hJg==/base.apk)
        at com.javiersc.gitGut.models.UserDTO$$serializer.<clinit>(UserDTO.kt:7)
        at com.javiersc.gitGut.models.UserDTO$Companion.serializer(Unknown Source:0)

@b95505017
Copy link

I can confirm kotlin 1.3.70 is not compatible with serialization 0.14.0 as well.

@JavierSegoviaCordoba
Copy link

I tried the last eap 0.20.0 and it is working

@sandwwraith
Copy link
Member

Yes, unfortunately, there's a breaking change in 1.3.70 plugin. We'll release compatible version of serialization in the following next few hours. Meanwhile, you can try eap builds from bintray: https://bintray.com/kotlin/kotlinx/kotlinx.serialization.runtime (e.g. 0.20.0-1.3.70-eap-274-2)

@brianguertin
Copy link

brianguertin commented Mar 4, 2020

Hmm, even the latest 0.20.0 is still failing for me with "no direct method " error using Kotlin 1.3.70

@sandwwraith
Copy link
Member

Original issue, which is a bug, not an incompatibility with Kotlin, is still present. @JavierSegoviaCordoba's issue was #736

@lallam
Copy link
Author

lallam commented Mar 10, 2020

Updated the versions to

Kotlin - 1.3.70
Kotlin Serialization - 0.20.0

Confirming that Issue is still there.

@enigmaCoder
Copy link

@sandwwraith This issue is not the same as @JavierSegoviaCordoba is mentioning fixed.
This issue is rather an issue in the Serialization annotation processor which doesn't allow inheritance of the Serialization property when working in an multi module project and serialization property are failing to be serialized at runtime. As you can see it throws an java.lang.NoSuchMethod error as it cannot process
However while working in an non multi module project this issue of inheritance is not there. If it was an Kotlin issue it would have given in an non multi module project as well if I am not wrong

@enigmaCoder
Copy link

@sandwwraith Also I am not sure but I believe the fix would need @inherit added in the @Serialzable annotation so that this can be inherited across modules

@enigmaCoder
Copy link

@Krondo The issue mentioned in #763 is not the same.

@sandwwraith sandwwraith changed the title java.lang.NoSuchMethodError java.lang.NoSuchMethodError when classes are in different modules Mar 26, 2020
@DevSrSouza
Copy link

DevSrSouza commented Mar 30, 2020

I had the same issue, any workaround on this ?

@bilalekremharmansa
Copy link

bilalekremharmansa commented Apr 2, 2020

I might have a workaround..

kotlin: 1.3.70, kotlinx-serialization: 0.20.0

I was running my newly created project on IntelliJ ever since i got "java.lang.NoSuchMethodError". I figure out, kotlinx plugin is not get activated when I run my test classes in IDE. I need to run mvn clean compile and here we go;

[INFO] --- kotlin-maven-plugin:1.3.70:compile (compile) @ healthcheck ---
[INFO] Applied plugin: 'kotlinx-serialization'

so the serializer() function will be generated and exception will be gone right after you see the lines above. Hope it's solves your problem as well.

===

Edit: i'm still suffering when i move my data class to test module

@MrPowerGamerBR
Copy link

MrPowerGamerBR commented May 19, 2020

This bug seems to happen only in JVM targets, I'm able to serialize/deserialize with no issues on my JS target but on my JVM target that bug happens.

In my case, I'm having this exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at kotlinx.serialization.SerializationKt.constructSerializerForGivenTypeArgs(Serialization.kt:40)
	at kotlinx.serialization.SerializationKt.compiledSerializerImpl(Serialization.kt:13)
	at kotlinx.serialization.PlatformUtilsKt.serializerOrNull(PlatformUtils.kt:34)
	at kotlinx.serialization.PlatformUtilsKt.serializer(PlatformUtils.kt:20)
	at kotlinx.serialization.modules.SerialModuleExtensionsKt.getContextualOrDefault(SerialModuleExtensions.kt:26)
	at net.perfectdreams.loritta.website.routes.api.v1.loritta.TestKt.main(Test.kt:23)
	at net.perfectdreams.loritta.website.routes.api.v1.loritta.TestKt.main(Test.kt)
Caused by: java.lang.NoSuchMethodError: kotlinx.serialization.internal.SerialClassDescImpl.<init>(Ljava/lang/String;Lkotlinx/serialization/internal/GeneratedSerializer;)V
	at net.perfectdreams.loritta.datawrapper.Background$$serializer.<clinit>(Background.kt:7)
	at net.perfectdreams.loritta.datawrapper.Background$Companion.serializer(Background.kt)
	... 11 more

Edit: Changing kotlinx.serialization and Kotlin libs to 1.3.70 fixed my issue: https://stackoverflow.com/questions/61259946/receiving-noclassdeffounderror-when-invoking-generated-serializer-method/61259947#61259947

@SanjayKushwah0601
Copy link

kotlin: 1.3.70, kotlinx-serialization: 0.20.0

Fixed my issue

@enigmaCoder
Copy link

I see how ppl are commenting that the issues are fixed in latest kotlin and kotlin serilzation plugin. Let me remind you guys to please understand the issue before commenting it's fixed and stop misleading everyone.

And NO THIS ISSUE is not fixed in kotlin: 1.3.70, kotlinx-serialization: 0.20.0

@sandwwraith
Copy link
Member

Changing the Kotlin version forces clean building, which fixes the issue sometimes (when it's caused by incremental compilation in one module).

@lallam
Copy link
Author

lallam commented Jun 2, 2020

@sandwwraith

Issue is NOT fixed.
Tested:
Kotlin 1.3.72
kotlinx-serialization: 0.20.0

I have pushed test code outlined in original post #576 (comment) in this repo below.

https://github.com/lallam/broke.git

(A) if you compile above code and run AS-IS, app crashes at line 32 during deserialization in MainActivity [it is a very simple code]

(B) to illustrate the multi project/module inheritance and deserialization issue.

in libraryb, i have created a duplicate class ModelX (Same as Model from LibraryA) , and inherited the User class

User - if you uncomment line 8 and comment line 9. problem goes away.

serialize/deserialize should work regardless of, if Parent class is in
(A) Different library or (B) Same library.

At the moment.
(A) - Doesn't work
(B) - Works.

@mmr118
Copy link

mmr118 commented Jun 8, 2020

I'm new to android. @lallam, can you (or anyone) ELI5 how to set kotlinx-serialization?

My build script is:

buildscript {
    ext.kotlin_version = '1.3.70'
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

build.gradle:app is dependencies:



dependencies {
    ...
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
}

and my variable values are
serialization_version set to 0.20.0
kotlin_version set to 1.3.70


UPDATE: I guess it's no mystery on how to set kotlinx-serialization, but I am still getting the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: gtink.deere.com.hammlink, PID: 5067
            java.lang.NoSuchFieldError: No field Companion of type Lkotlinx/serialization/json/Json$Companion; in class Lkotlinx/serialization/json/Json; or its superclasses (declaration of 'kotlinx.serialization.json.Json' appears in /data/app/~~RUbPhu7SrnGhEv-E3WOq3A==/PROJ.FOLDERS.com.***-NvjPkuiwQNXcO5VogochCg==/base.apk!classes3.dex)

@dgomolka
Copy link

Kotlin 1.3.72
kotlinx-serialization: 0.20.0
java.lang.NoSuchMethodError: No direct method (ILkotlinx/serialization/SerializationConstructorMarker;)V

still occurs

@mschirmacher
Copy link

Is there any progress on this one? This issue drives the library completly useless...

Dabami pushed a commit to helios-h2020/h.extension-RewardingModel that referenced this issue Jul 15, 2020
- Replaced kotlinx serializer by gson serializer due to open issue in github Kotlin/kotlinx.serialization#576
- Replaced okhttp android client by ktor android client due to issues related to kotlin. It seems that it is related to latest version of okjhttp but even compiling with jvm target 1.8 is still failing:
square/okhttp#4597
@lallam
Copy link
Author

lallam commented Jul 15, 2020

Reference: #576 (comment)
#576 (comment)

this has become a critical issue for us. we are currently using a VERY crude method to over come this temporarily.

Library A - Model

Library B - Model extends LibraryA.Model
- User extends LibraryB.Model

Library C - Model extends LibraryA.Model
- Device extends LibraryC.Model

Model classes in LibraryB and LibraryC are identical to LibraryA.Model and override the members.
We are hoping that once the issue is fixed, we could DELETE these duplicate classes and extend User and Device classes directly from LibraryA.Model.

or other alternative is to abandon kotlin serialization altogether and refactor it to a different Library. fingers crossed.

@sandwwraith
Copy link
Member

#378 (comment)

@lallam
Copy link
Author

lallam commented Jul 23, 2020

thanks @sandwwraith we will test and confirm.

@IgnacioGarcia198
Copy link

IgnacioGarcia198 commented Sep 6, 2020

Hello, this is happening to me with Kotlin 1.4 and org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.0.0-RC which is included in Retrofit kotlinx serialization converter dependency (I could not use org.jetbrains.kotlinx:kotlinx-serialization-runtime-jvm:1.0-M1-1.4.0-rc since it creates dependency conflict).

@sandwwraith
Copy link
Member

@IgnacioGarcia198 Do you have a sample project with reproducer?

IgnacioGarcia198 pushed a commit to IgnacioGarcia198/BikeSharing-Android that referenced this issue Sep 7, 2020
@IgnacioGarcia198
Copy link

@sandwwraith I posted a project in my GitHub repository in order to reproduce it. Here is the link.
https://github.com/IgnacioGarcia198/BikeSharing-Android
If you try the test SerializationTest.kt, you will see it is working, but when you run the app the problem is with Retrofit. Please if you notice something I am doing wrong tell me ;)

@ronjunevaldoz
Copy link

ronjunevaldoz commented Sep 29, 2020

Any update on this issue or example workaround? I'm experiencing this also in 1.4.10

@pnemonic78
Copy link

maybe it has something to do with proguard obfuscation?

@enigmaCoder
Copy link

@IgnacioGarcia198 don't use the serializer factory of retrofit instead accept the Retrofit response as Response Body objects and the write your own serializing extension api and convert the response body received to the required module using the modules serializer using kotlin serialization.

@sandwwraith sandwwraith reopened this Dec 17, 2020
@sandwwraith sandwwraith self-assigned this Dec 17, 2020
@Arushi129
Copy link

Hi, I have the same error on my KMM project.

java.lang.NoSuchMethodError: No direct method (Ljava/lang/String;Lkotlinx/serialization/internal/GeneratedSerializer;I)V in class Lkotlinx/serialization/internal/SerialClassDescImpl; or its super classes (declaration of 'kotlinx.serialization.internal.SerialClassDescImpl' appears in /data/app/com.example.mykmmclient12april-eQ9TG4Mg_9sDGC-WYU26Eg==/base.apk)
at org.openapitools.client.models.Order$$serializer.(Order.kt:28)
at org.openapitools.client.models.Order$Companion.serializer(Unknown Source:0)
at org.openapitools.client.infrastructure.ApiClient$Companion.setMappers(ApiClient.kt:64)
at org.openapitools.client.infrastructure.ApiClient$Companion.access$setMappers(ApiClient.kt:57)
at org.openapitools.client.infrastructure.ApiClient$serializer$2.invoke(ApiClient.kt:41)
at org.openapitools.client.infrastructure.ApiClient$serializer$2.invoke(ApiClient.kt:28)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at org.openapitools.client.infrastructure.ApiClient.getSerializer(Unknown Source:2)
at org.openapitools.client.infrastructure.ApiClient.jsonRequest(ApiClient.kt:148)
at org.openapitools.client.apis.StoreApi.placeOrder(StoreApi.kt:171)
at com.example.mykmmclient12april.MainActivity.callApi(MainActivity.kt:44)
at com.example.mykmmclient12april.MainActivity$onCreate$1.invokeSuspend(MainActivity.kt:29)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)

Order Class -
@serializable
data class Order (
@SerialName(value = "id") val id: kotlin.Long? = null,
@SerialName(value = "petId") val petId: kotlin.Long? = null,
@SerialName(value = "quantity") val quantity: kotlin.Int? = null,
@SerialName(value = "shipDate") val shipDate: kotlin.String? = null,
/* Order Status */
@SerialName(value = "status") val status: Order.Status? = null,
@SerialName(value = "complete") val complete: kotlin.Boolean? = null
) {

Environment:

Kotlin version: [1.3.72]
serialization plugin version:[0.12.0]
IDE version [Android Studio 4.1.1]
gradle version:[6.5]

I doesn't want to update the kotlin and plugin version as it may affect my other projects. Please help.

@enigmaCoder
Copy link

Okay
So first of, if you are using the @serializable annotations you don't need to again declare @SerialName as kotlin serialization will automatically use the assigned one

Next is your "Order.Status" class serializable? Just check if it's not and make it serializable.

Next this problem would only happen if you are extending a serializable class which is in a seperate module to a Serializable class which is in a different module, this problem would arise.
This problem however is not there if both the classes exist in the same module.

So assuming you are facing the above problem, following are the solutions
1.) You must make sure both the serializable classes available in the same module make a copy of the class you are extending and keep a copy of it
2. ) Instead of defining a common class provide a common interface and extend and implement it in all the classes needed
3.) Develop a reflection based serialization mechanism or use gson to serialize those modules which are having intermodule dependency

Hope this helps. Please share a sample piece of the above problem, so that we can take a closer look at the problem you are facing

@Arushi129
Copy link

Hi,

Thanks for the reply.
I am following the mentioned link to create KMM project using open api generator tool(provided yaml file only) - 1-)To create open api client - https://www.reddit.com/r/Kotlin/comments/d9xndy/openapi_generators_kotlin_client_generator_now/
2-) To download jar file - https://github.com/OpenAPITools/openapi-generator#13---download-jar

But the "kotlin version" in the generated open api client project from the above link is - "1.3.50" which is very old and not compatible with KMM latest version which is "1.4.32".

Note: I am trying to access the kotlin files or methods from swift ios standalone project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests