Jetpack Compose类反编译异常及源码下载失败问题求助
解决Jetpack Compose类显示为反编译.class文件的问题
问题详情
学习Jetpack Compose时,打开material3库中的Scaffold相关类,显示的是反编译后的.class文件而非源码,反编译代码如下:
// IntelliJ API Decompiler stub source generated from a class file // Implementation of methods is not available package androidx.compose.material3 private val FabSpacing: androidx.compose.ui.unit.Dp /* compiled code */ internal val LocalFabPlacement: androidx.compose.runtime.ProvidableCompositionLocal<androidx.compose.material3.FabPlacement?> /* compiled code */ @androidx.compose.material3.ExperimentalMaterial3Api public var ScaffoldSubcomposeInMeasureFix: kotlin.Boolean /* compiled code */ public final @androidx.compose.material3.ExperimentalMaterial3Api get public final @androidx.compose.material3.ExperimentalMaterial3Api set(value: kotlin.Boolean) {/* compiled code */ } @androidx.compose.runtime.Composable @androidx.compose.runtime.ComposableInferredTarget private fun LegacyScaffoldLayout(fabPosition: androidx.compose.material3.FabPosition, topBar: @androidx.compose.runtime.Composable () -> kotlin.Unit, content: @androidx.compose.runtime.Composable (androidx.compose.foundation.layout.PaddingValues) -> kotlin.Unit, snackbar: @androidx.compose.runtime.Composable () -> kotlin.Unit, fab: @androidx.compose.runtime.Composable () -> kotlin.Unit, contentWindowInsets: androidx.compose.foundation.layout.WindowInsets, bottomBar: @androidx.compose.runtime.Composable () -> kotlin.Unit): kotlin.Unit { /* compiled code */ } @androidx.compose.runtime.Composable @androidx.compose.runtime.ComposableInferredTarget public fun Scaffold(modifier: androidx.compose.ui.Modifier = COMPILED_CODE, topBar: @androidx.compose.runtime.Composable () -> kotlin.Unit = COMPILED_CODE, bottomBar: @androidx.compose.runtime.Composable () -> kotlin.Unit = COMPILED_CODE, snackbarHost: @androidx.compose.runtime.Composable () -> kotlin.Unit = COMPILED_CODE, floatingActionButton: @androidx.compose.runtime.Composable () -> kotlin.Unit = COMPILED_CODE, floatingActionButtonPosition: androidx.compose.material3.FabPosition = COMPILED_CODE, containerColor: androidx.compose.ui.graphics.Color = COMPILED_CODE, contentColor: androidx.compose.ui.graphics.Color = COMPILED_CODE, contentWindowInsets: androidx.compose.foundation.layout.WindowInsets = COMPILED_CODE, content: @androidx.compose.runtime.Composable (androidx.compose.foundation.layout.PaddingValues) -> kotlin.Unit): kotlin.Unit { /* compiled code */ } @androidx.compose.runtime.Composable @androidx.compose.runtime.ComposableInferredTarget private fun ScaffoldLayout(fabPosition: androidx.compose.material3.FabPosition, topBar: @androidx.compose.runtime.Composable () -> kotlin.Unit, content: @androidx.compose.runtime.Composable (androidx.compose.foundation.layout.PaddingValues) -> kotlin.Unit, snackbar: @androidx.compose.runtime.Composable () -> kotlin.Unit, fab: @androidx.compose.runtime.Composable () -> kotlin.Unit, contentWindowInsets: androidx.compose.foundation.layout.WindowInsets, bottomBar: @androidx.compose.runtime.Composable () -> kotlin.Unit): kotlin.Unit { /* compiled code */ } @androidx.compose.runtime.Composable @androidx.compose.runtime.ComposableInferredTarget private fun ScaffoldLayoutWithMeasureFix(fabPosition: androidx.compose.material3.FabPosition, topBar: @androidx.compose.runtime.Composable () -> kotlin.Unit, content: @androidx.compose.runtime.Composable (androidx.compose.foundation.layout.PaddingValues) -> kotlin.Unit, snackbar: @androidx.compose.runtime.Composable () -> kotlin.Unit, fab: @androidx.compose.runtime.Composable () -> kotlin.Unit, contentWindowInsets: androidx.compose.foundation.layout.WindowInsets, bottomBar: @androidx.compose.runtime.Composable () -> kotlin.Unit): kotlin.Unit { /* compiled code */ }
点击「Download resources」尝试下载源码时,出现Gradle构建错误:
FAILURE: Build failed with an exception. * Where: Initialization script 'C:\Users\ad011\AppData\Local\Temp\ijmiscinit1.gradle' line: 25 * What went wrong: Execution failed for task ':app:DownloadSources'. > Could not resolve all files for configuration ':app:downloadSources_01e59d2f-b2e1-4cb4-af0e-21e0cab1c0b2'. > Could not find androidx.compose.material3:material3-android:1.2.1@aar. Searched in the following locations: - https://dl.google.com/dl/android/maven2/androidx/compose/material3/material3-android/1.2.1@aar/material3-android-1.2.1@aar.pom - https://repo.maven.apache.org/maven2/androidx/compose/material3/material3-android/1.2.1@aar/material3-android-1.2.1@aar.pom Required by: project :app * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 11s 1 actionable task: 1 executed
项目当前app/build.gradle配置如下:
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { namespace = "com.example.jetpack_compose" compileSdk = 34 defaultConfig { applicationId = "com.example.jetpack_compose" minSdk = 24 targetSdk = 34 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary = true } } 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" } buildFeatures { compose = true } composeOptions { kotlinCompilerExtensionVersion = "1.5.1" } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } } } dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0") implementation("androidx.activity:activity-compose:1.8.2") implementation(platform("androidx.compose:compose-bom:2024.02.02")) implementation("androidx.compose.material3:material3") //view model implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0") //hilt implementation("androidx.hilt:hilt-navigation-compose:1.1.0-alpha01") //coil implementation("io.coil-kt:coil-compose:2.6.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation(platform("androidx.compose:compose-bom:2024.02.02")) // compose androidTestImplementation("androidx.compose.ui:ui-test-junit4") debugImplementation("androidx.compose.ui:ui-tooling") debugImplementation("androidx.compose.ui:ui-test-manifest") // constrains layout implementation ("androidx.constraintlayout:constraintlayout-compose:1.0.1") implementation("androidx.compose.ui:ui:") implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.runtime:runtime:1.6.3") implementation("androidx.compose.runtime:runtime-livedata:1.6.3") implementation("androidx.compose.runtime:runtime-rxjava2:1.6.3") }
问题原因
- 依赖版本冲突:使用Compose BOM管理版本的同时,手动指定部分Compose依赖版本(如
androidx.compose.runtime:runtime:1.6.3),导致BOM版本管理失效,出现依赖解析异常。 - 无效依赖配置:存在无版本号的依赖(如
androidx.compose.ui:ui:),Gradle无法正确解析。 - 版本不兼容:Compose Compiler版本与Kotlin插件版本未匹配,导致依赖解析错误。
解决方案
1. 修正依赖配置,统一使用Compose BOM管理版本
删除手动指定版本的Compose依赖及无版本号依赖,让BOM自动管理所有Compose相关依赖版本。修改后的dependencies块如下:
dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0") implementation("androidx.activity:activity-compose:1.8.2") implementation(platform("androidx.compose:compose-bom:2024.02.02")) implementation("androidx.compose.material3:material3") // 由BOM管理版本的基础Compose依赖 implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.runtime:runtime") implementation("androidx.compose.runtime:runtime-livedata") implementation("androidx.compose.runtime:runtime-rxjava2") //view model implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0") //hilt implementation("androidx.hilt:hilt-navigation-compose:1.1.0-alpha01") //coil implementation("io.coil-kt:coil-compose:2.6.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation(platform("androidx.compose:compose-bom:2024.02.02")) // compose test androidTestImplementation("androidx.compose.ui:ui-test-junit4") debugImplementation("androidx.compose.ui:ui-tooling") debugImplementation("androidx.compose.ui:ui-test-manifest") // constrains layout implementation ("androidx.constraintlayout:constraintlayout-compose:1.0.1") }
2. 指定兼容的Kotlin版本
在项目根目录的build.gradle中,明确指定与Compose Compiler 1.5.1兼容的Kotlin版本(1.9.20):
plugins { id("com.android.application") version "8.2.2" apply false id("org.jetbrains.kotlin.android") version "1.9.20" apply false }
3. 清理并重建项目
- 打开Android Studio,点击
File->Invalidate Caches...,选择Invalidate and Restart。 - 重启后,依次执行
Build->Clean Project、Build->Rebuild Project。
4. 重新下载源码
项目重建完成后,再次打开目标Compose类,点击「Download resources」,Gradle即可正确解析依赖并下载源码。
内容的提问来源于stack exchange,提问作者Ahmed Abdelnaser
相关产品推荐
相关产品推荐

