组合到现有项目中,无虚方法 Int

Iva*_*ura 6 android android-jetpack-compose

现在无法让 compose 在现有的 kotlin/native 项目中运行一个月,尝试将默认的 Greeting 示例设置为splash而不是其 ui,但无法实现:

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)
Run Code Online (Sandbox Code Playgroud)

是的,在活动开始之前崩溃。

科特林 1.5.21

撰写版本 = 1.0.2

mTargetSdkVersion=30(我厌倦了每次同步时合并清单,所以在我全部运行之前不会使用 31)

mCompileSdkVersion=31

mMinSdkVersion=21

项目级别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"
Run Code Online (Sandbox Code Playgroud)

android module build.gradle(不是整个文件,其现有项目):

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?)
Run Code Online (Sandbox Code Playgroud)

编辑:为了测试目的,pushwoosh、appsflyer、crashlytics 被删除,没有修复任何内容。

降级到 compose 1.0.1 和稳定的 androidx 什么也没做

Dan*_*gen 5

如果您仔细分析异常消息,您可能会注意到其中提到LiveLiterals

java.lang.NoSuchMethodError:类 Lcom/a/b/c/activity/ LiveLiterals $SplashActivityKt中没有虚拟方法 Int$class-SplashActivity()I ;

LiveLiterals部分不是您自己的 API 的一部分,但它是由 Compose 生成的代码,可以在撰写预览中实时编辑文字:https ://developer.android.com/jetpack/compose/tooling#live-edit-文字

看来您的一个类(在本例中SplashActivityLiveLiterals无法正确生成支持。我不知道这是否是 Compose 中的错误或已知限制,但作为一种解决方法,有一种方法可以选择退出实时文字: https: //developer.android.com/reference/kotlin/androidx/compose/运行时/NoLiveLiterals

因此,如果您有一个类出现 NoSuchMethodError 异常,请添加注释@NoLiveLiterals

@NoLiveLiterals
class SplashActivity {
   // ...
Run Code Online (Sandbox Code Playgroud)

注释可以添加到属性、函数、类或整个文件中。


Iva*_*ura 0

//contains 99% common code, now contains Compose view 
open class AppLegasyFlavorActivity

//absolutely empty for default flavor
class SplashActivity : AppLegasyFlavorActivity()
Run Code Online (Sandbox Code Playgroud)

由于某种原因,您无法在 1.0.2 compose 中实现这一点。您的孩子必须重写 onCreate(即使它只是超级调用)。我删除了遗留的父项,进行了口味重构,一切正常。这个奇怪的错误是由于 kotlin 无法理解 Int 类而导致的。我想,在技术方式的边缘。