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

现有Android Studio项目集成Jetpack Compose编译报错求助

解决Jetpack Compose集成编译错误的方案

核心问题

你开启了compose = true的构建特性,但未添加任何Compose相关依赖,编译器找不到必要的Compose编译组件,因此抛出该错误。

具体修复步骤

  1. 添加必备的Compose依赖
    在模块级build.gradle(或build.gradle.kts)的dependencies块中添加基础Compose依赖:

    dependencies {
        // Compose核心UI依赖
        implementation "androidx.compose.ui:ui:$compose_version"
        // Material Design组件依赖
        implementation "androidx.compose.material:material:$compose_version"
        // Preview预览功能依赖
        implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
        debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    }
    
  2. 验证版本一致性
    确认项目根目录build.gradle中的Kotlin插件版本与你声明的kotlin_version一致:

    buildscript {
        ext {
            kotlin_version = '1.7.10'
            compose_version = '1.3.0'
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  3. 同步并重建项目

    • 点击Android Studio右上角的「Sync Project with Gradle Files」按钮同步项目
    • 依次执行Build > Clean Project和Build > Rebuild Project
  4. 额外检查项

    • 确保AGP(Android Gradle Plugin)版本不低于7.3.0,根目录build.gradle中确认:
      dependencies {
          classpath "com.android.tools.build:gradle:7.3.0"
      }
      
    • 确认compileSdk和targetSdk均设置为33(Compose 1.3.0支持compileSdk 31及以上版本)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 03:29:54