Jetpack Compose中setContent函数致Android应用崩溃求助
Jetpack Compose应用启动崩溃:NoSuchMethodError 与 setContent相关问题
我正在跟着Jetpack Compose官方教程学习,但运行代码时应用无法启动。使用的是最新版Android Studio和API 34。
代码示例
package com.app1.app1 import android.os.Bundle import androidx.compose.ui.tooling.preview.Preview import androidx.activity.ComponentActivity import androidx.compose.runtime.Composable import androidx.activity.compose.setContent import androidx.compose.material3.Text class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MessageCard("Matt") } } } @Composable fun MessageCard(name: String) { Text(text = "Hello $name!") }
崩溃日志
17:02:03.719 W Unable to open '/data/app/~~GY4CdUSsI14breeI4KXVIg==/com.app1.app1-trtyu0tTL5-i-SDx3OR2cg==/base.dm': No such file or directory 17:02:03.719 W Unable to open '/data/app/~~GY4CdUSsI14breeI4KXVIg==/com.app1.app1-trtyu0tTL5-i-SDx3OR2cg==/base.dm': No such file or directory 17:02:03.793 D Configuring clns-4 for other apk /data/app/~~GY4CdUSsI14breeI4KXVIg==/com.app1.app1-trtyu0tTL5-i-SDx3OR2cg==/base.apk. target_sdk_version=33, uses_libraries=, library_path=/data/app/~~GY4CdUSsI14breeI4KXVIg==/com.app1.app1-trtyu0tTL5-i-SDx3OR2cg==/lib/arm64, permitted_path=/data:/mnt/expand:/data/user/0/com.app1.app1 17:02:03.802 V ANGLE Developer option for 'com.app1.app1' set to: 'default' 17:02:03.802 V ANGLE GameManagerService for com.app1.app1: false 17:02:03.802 V Neither updatable production driver nor prerelease driver is supported. 17:02:03.805 D No Network Security Config specified, using platform default 17:02:03.805 D No Network Security Config specified, using platform default 17:02:03.832 D Shutting down VM 17:02:03.835 E FATAL EXCEPTION: main Process: com.app1.app1, PID: 16669 java.lang.NoSuchMethodError: No static method setContent$default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/ CompositionContext;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V in class Landroidx/activity/compose/ComponentActivityKt; or its super classes (declaration of 'androidx.activity.compose.ComponentActivityKt' appears in /data/app/~~GY4CdUSsI14breeI4KXVIg==/com.app1.app1-trtyu0tTL5-i-SDx3OR2cg==/base.apk) at com.app1.app1.MainActivity.onCreate(MainActivity.kt:13) at android.app.Activity.performCreate(Activity.java:8342) at android.app.Activity.performCreate(Activity.java:8321) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7918) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) ----------------- PROCESS ENDED (16669) for package com.app1.app1 ---------------------
补充配置文件
Module级build.gradle
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { namespace = "com.app1.app1" compileSdk = 34 buildFeatures { compose = true } defaultConfig { applicationId = "com.app1.app1" minSdk = 29 targetSdk = 33 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } dependencies { implementation("androidx.activity:activity-compose:1.8.0") implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") implementation("androidx.hilt:hilt-navigation-compose:1.1.0-beta01") implementation("androidx.compose.material3:material3:1.2.0-alpha09") implementation("androidx.compose.ui:ui-tooling-preview-android:1.5.3") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.APP1" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
解决方案
核心原因
崩溃的直接原因是Compose相关依赖版本不兼容:activity-compose:1.8.0与你使用的material3:1.2.0-alpha09、ui-tooling-preview-android:1.5.3版本不匹配,导致运行时找不到setContent的对应方法签名。
修复步骤
使用Compose BOM统一管理依赖版本
官方推荐使用Compose BOM(物料清单)自动同步所有Compose库的版本,避免版本冲突。修改你的module级build.gradle:- 在dependencies块顶部添加BOM依赖:
implementation platform("androidx.compose:compose-bom:2024.04.00") - 移除各个Compose库的版本号,由BOM自动管理:
// 替换原有material3和ui-tooling依赖 implementation "androidx.compose.material3:material3" implementation "androidx.compose.ui:ui-tooling-preview" debugImplementation "androidx.compose.ui:ui-tooling" - 同步
activity-compose版本到与BOM兼容的最新稳定版:implementation "androidx.activity:activity-compose:1.8.2"
- 在dependencies块顶部添加BOM依赖:
清理冗余依赖
移除当前代码未用到的hilt-navigation-compose依赖,减少不必要的版本冲突风险。同步并重建项目
- 点击Android Studio顶部的「Sync Project with Gradle Files」按钮同步Gradle配置
- 执行「Build > Clean Project」清理旧构建缓存
- 执行「Build > Rebuild Project」重新构建项目
修改后的完整dependencies示例
dependencies { // Compose BOM管理所有Compose库版本 implementation platform("androidx.compose:compose-bom:2024.04.00") implementation "androidx.activity:activity-compose:1.8.2" implementation "androidx.core:core-ktx:1.12.0" implementation "androidx.appcompat:appcompat:1.6.1" implementation "com.google.android.material:material:1.9.0" implementation "androidx.constraintlayout:constraintlayout:2.1.4" // Compose核心依赖(版本由BOM控制) implementation "androidx.compose.material3:material3" implementation "androidx.compose.ui:ui-tooling-preview" debugImplementation "androidx.compose.ui:ui-tooling" testImplementation "junit:junit:4.13.2" androidTestImplementation "androidx.test.ext:junit:1.1.5" androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1" }
额外说明
- 你的业务代码本身没有错误,问题完全源于依赖版本不匹配
- Compose BOM会自动保证所有相关库的版本兼容性,是官方推荐的依赖管理方式
- 清理重建项目能解决Gradle缓存导致的版本不一致问题
内容的提问来源于stack exchange,提问作者R1B07008
相关产品推荐
相关产品推荐

