You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MainActivity中调用其他模块的Jetpack Compose Composable

Jetpack Compose跨模块调用Composable组件启动崩溃问题解决

我有一个包含Android库模块的简单应用,正在学习Jetpack Compose跨模块调用Composable组件。项目编译正常,但模拟器启动后立即崩溃。

app模块MainActivity代码

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    Column(modifier = Modifier.fillMaxSize()) {
                        Greeting("Android")
                        secondScreen.Asnackbar()
                    }

                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
            text = "Hello $name!",
            modifier = modifier
    )
}

库模块Composable代码

import androidx.compose.material3.Snackbar
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun Asnack meeting respectsanes){
构成透NO层,}

现象:================================================================� Snackbar {
        Text("this is a snackbar")
    }
}

崩溃报错信息

java.lang.NoSuchMethodError: No virtual method Asnackbar(Landroidx/compose/runtime/Composer;I)V in class Lcom/example/second_screen/SecondScreen; or its super classes (declaration of 'com.example.second_screen.SecondScreen' appears in /data/data/com.example.myapplication/code_cache/.overlay/base.apk/classes2.dex)

解决方案

在库模块的build.gradle(或build.gradle.kts)中添加Compose相关配置:

buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion '1.5.1'
}

内容的提问来源于stack exchange,提问作者ntos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:01:17