【发布时间】:2022-01-14 15:16:25
【问题描述】:
我在尝试运行 setContent{ Composable() } 时遇到 java.lang.NoSuchMethodError 异常。
完整代码:
class ComposeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
ComposeView(requireContext()).apply { setContent { Text("hello") } }
}
完全例外:
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)
类似问题的解决方案/答案建议添加buildFeatures { compose = true } 或kotlinCompilerExtensionVersion,我已经这样做了,但问题仍然存在。
我完整的 compose 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" )
【问题讨论】:
-
请提供一个示例项目来重现该错误。我尝试创建一个新项目(Fragment + ViewModel),添加你的 gradle 配置,用你的替换生成的片段代码,它运行良好。
-
嗨@PhilipDukhov,我会在有机会时这样做,谢谢。
标签: android gradle android-jetpack-compose