Android Jetpack Compose - java.lang.NoSuchMethodError:没有虚拟方法 setContent(Lkotlin/jvm/functions/Function0;)

Kes*_*ker 32 android gradle android-jetpack-compose

java.lang.NoSuchMethodError我在尝试运行时遇到异常setContent{ Composable() }

完整代码:

class ComposeFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
        ComposeView(requireContext()).apply { setContent { Text("hello") } }

}
Run Code Online (Sandbox Code Playgroud)

完整异常:

java.lang.NoSuchMethodError: No virtual method setContent(Lkotlin/jvm/functions/Function0;)V in class Landroidx/compose/ui/platform/ComposeView; or its super classes (declaration of 'androidx.compose.ui.platform.ComposeView' appears in /data/app/~~3OmVKUoYitZ_S4H81xmyuw==/my.app.package-PAQxAKmtRuhnzp4M2DME8w==/base.apk)
Run Code Online (Sandbox Code Playgroud)

类似问题的解决方案/答案建议添加buildFeatures { compose = true }kotlinCompilerExtensionVersion,我已经这样做了,但问题仍然存在。

我的完整 Gradle 配置如下:

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
    useIR = true
}

buildFeatures {
    compose = true
}

composeOptions{
    kotlinCompilerExtensionVersion = "1.0.5"
}

implementation( "androidx.activity:activity-compose:1.3.1" )
implementation( "androidx.activity:activity-compose:1.3.1" )
implementation( "androidx.compose.material:material:1.0.5" )
implementation( "androidx.compose.animation:animation:1.0.5" )
implementation( "androidx.compose.ui:ui-tooling:1.0.5" )
implementation( "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07" )
androidTestImplementation( "androidx.compose.ui:ui-test-junit4:1.0.5" )
Run Code Online (Sandbox Code Playgroud)

Pha*_*hai 28

今天遇到了同样的异常,所以我把我的解决方案放在这里,以防其他人需要它。

build.gradle就我而言,这是因为该android部分的文件中缺少以下设置

    buildFeatures.compose = true
    composeOptions {
        kotlinCompilerExtensionVersion = Versions.COMPOSE
    }
Run Code Online (Sandbox Code Playgroud)

Version.COMPOSE是撰写版本,就我而言是1.1.0-alpha05


gui*_*tgl 6

我遇到了同样的问题,通过将以下行添加到我的应用程序的 build.gradle 文件中解决了这个问题:

android {
   buildFeatures {
       compose true
   }
}
Run Code Online (Sandbox Code Playgroud)


Col*_*bri 5

将此行添加到模块的 gradle 文件中:

buildFeatures {
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion = rootProject.extra["compose_version"] as String
}
Run Code Online (Sandbox Code Playgroud)

如果您从事多模块项目,则必须为每个组合包含的模块添加此内容。


And*_*Dev 2

您的 Compose 版本非常旧,几乎可以肯定是您出现问题的原因。使用以下内容更新您的项目 gradle。确保同时升级插件版本:

buildscript {
    ext {
        compose_version = '1.1.0-rc01'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'
    }
}
Run Code Online (Sandbox Code Playgroud)

确保所有依赖项都使用最新的稳定版本。

您还应该更新您的 Kotlin 插件版本:

211-1.6.10-release-923-AS7442.40

如果您不知道在哪里设置,请单击:

Android Studio > Preferences > Languages & Frameworks > Kotlin

最后,我强烈建议您使用 Java 11 而不是 8,并将应用程序的 build.gradle 设置为:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

apply from: 'codeinc.gradle'

android {
    compileSdkVersion 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "mydomain.myapp.blablahblah"
        minSdkVersion 21
        targetSdkVersion 31
        versionName "1.0.0"
        
        versionCode 1
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.6.0'
    }
}
Run Code Online (Sandbox Code Playgroud)

  • “很古老”?!在您撰写本文时,Compose 1.0.5(提问者使用的)才发布了不到 3 个月。 (2认同)