Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose into existing project, No virtual method Int

Cant make compose run in existing kotlin/native project for month now, trying to set default Greeting example to splash instead of its ui, cant make it:

java.lang.NoSuchMethodError: No virtual method Int$class-SplashActivity()I in class Lcom/a/b/c/activity/LiveLiterals$SplashActivityKt; or its super classes (declaration of 'a.b.c.ui.activity.LiveLiterals$SplashActivityKt' appears in /data/app/a.b.c.develop-7Zjdfjfy762lPctajg==/base.apk!classes29.dex)
    at a.b.c.ui.activity.SplashActivity.<clinit>
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Yes, crash before activity start.

kotlin 1.5.21

compose_version = 1.0.2

mTargetSdkVersion=30(I'm tired of merging manifests every sync, so no 31 until I'l run it all)

mCompileSdkVersion=31

mMinSdkVersion=21

project level build.gradle:

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "com.android.tools.build:gradle:7.1.0-alpha12"
    classpath "gradle.plugin.com.wiredforcode:gradle-spawn-plugin:0.8.2"
    classpath "com.google.gms:google-services:4.3.10"
    classpath "com.google.firebase:firebase-crashlytics-gradle:2.7.1"

android module build.gradle(not whole file, its existing project):

implementation "com.android.support:recyclerview-v7:28.0.0"
implementation "com.android.support.constraint:constraint-layout:2.0.4"
implementation "com.android.support:cardview-v7:28.0.0"

implementation "androidx.activity:activity-ktx:1.4.0-alpha02"
implementation "androidx.appcompat:appcompat:1.4.0-alpha03"
implementation "com.google.accompanist:accompanist-appcompat-theme:0.18.0"
implementation "com.google.android.material:material:1.5.0-alpha03"

implementation "androidx.navigation:navigation-fragment-ktx:2.4.0-alpha09"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha09"

implementation 'androidx.core:core-ktx:1.7.0-beta01'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0-beta01'

implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.activity:activity-compose:1.4.0-alpha02'

implementation "androidx.tracing:tracing-ktx:1.0.0"

implementation "androidx.multidex:multidex:2.0.1"


implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"

also contains pushwoosh, appsflyer, crashlytics, junit tests(does impact compose libs?)

EDIT: pushwoosh, appsflyer, crashlytics was removed for sake of test, fixed nothing.

Downgrade to compose 1.0.1 and stable androidx did nothing

like image 571
Ivan Mitsura Avatar asked Nov 15 '22 19:11

Ivan Mitsura


1 Answers

If you analyze the exception message closely, you might notice the mention of LiveLiterals:

java.lang.NoSuchMethodError: No virtual method Int$class-SplashActivity()I in class Lcom/a/b/c/activity/LiveLiterals$SplashActivityKt;

The LiveLiterals part is not a part of your own API, but it's generated code by Compose, that enables the live editing of literals in the compose preview: https://developer.android.com/jetpack/compose/tooling#live-edit-literals

It seems that for one of your classes (in this case SplashActivity) the LiveLiterals support cannot be generated properly. I don't know if this is a bug in Compose or a known limitation, but as a workaround there is a way to opt out of live literals: https://developer.android.com/reference/kotlin/androidx/compose/runtime/NoLiveLiterals

So if you have a class for which you get the NoSuchMethodError exception, add the @NoLiveLiterals annotation:

@NoLiveLiterals
class SplashActivity {
   // ...

The annotation may be added to a property, a function, a class or an entire file.

like image 174
Danilo Bargen Avatar answered Dec 04 '22 14:12

Danilo Bargen