Android jetpack compose NoSuchMethodError: No static method setContent

Posted

技术标签:

【中文标题】Android jetpack compose NoSuchMethodError: No static method setContent【英文标题】: 【发布时间】:2021-02-16 09:36:24 【问题描述】:

Jetpack Compose 发布版本:1.0.0-alpha06 Android Studio 构建: 4.2 1Canery 15

我的活动

class MainActivityA : AppCompatActivity() 
    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContent 
              Text(text = "hello", modifier = Modifier.padding(12.dp))
        

    

上面运行时编译这个错误:

java.lang.NoSuchMethodError: No static method setContent$default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/Recomposer;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Landroidx/compose/runtime/Composition; in class Landroidx/compose/ui/platform/WrapperKt; or its super classes (declaration of 'androidx.compose.ui.platform.WrapperKt' appears in /data/app/com.jonathan-_j03c44RbBo00Hn7DeOjeA==/base.apk)
        

我的一些 gradle 设置

def jetpack_compose = "1.0.0-alpha06"

 kotlinOptions 
        jvmTarget = '1.8'
        useIR = true
    

    composeOptions 
        kotlinCompilerVersion '1.4.0'
        kotlinCompilerExtensionVersion "$jetpack_compose"
    

  //JetPack Compose stuff
    implementation "androidx.compose.ui:ui:$jetpack_compose"
    // Tooling support (Previews, etc.)
    implementation "androidx.ui:ui-tooling:$jetpack_compose"
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation "androidx.compose.foundation:foundation:$jetpack_compose"
    // Material Design
    implementation "androidx.compose.material:material:$jetpack_compose"
    // Material design icons
    implementation "androidx.compose.material:material-icons-core:$jetpack_compose"
    implementation "androidx.compose.material:material-icons-extended:$jetpack_compose"
    // Integration with observables
    implementation "androidx.compose.runtime:runtime-livedata:$jetpack_compose"
    implementation "androidx.compose.runtime:runtime-rxjava2:$jetpack_compose"

***构建脚本

/ Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript 
    ext.kotlin_version = "1.4.10"
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath "com.android.tools.build:gradle:4.2.0-alpha15"
        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
    


allprojects 
    repositories 
        google()
        jcenter()
    


task clean(type: Delete) 
    delete rootProject.buildDir

【问题讨论】:

【参考方案1】:

我遇到了同样的问题。在应用程序 Gradle 文件中添加以下行后,它对我有用

buildFeatures 
    compose true

【讨论】:

【参考方案2】:

我还必须删除 androidx.navigation:navigation-compose 依赖项,这对我来说是个问题。

【讨论】:

以上是关于Android jetpack compose NoSuchMethodError: No static method setContent的主要内容,如果未能解决你的问题,请参考以下文章

Android Jetpack Compose学习—— Jetpack compose基础布局

Android Jetpack Compose学习—— Jetpack compose入门

Android Jetpack Compose学习—— Jetpack compose入门

Android Kotlin Jetpack Compose 使用

Jetpack Compose 和 Compose Navigation 如何处理 Android 活动?

Android高级Jetpack架构组件+Jetpack compose强化实战