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

升级AGP 8.0后Jetpack Compose小费计算器项目编译失败求助

解决AGP 8.0升级后Jetpack Compose小费计算器项目编译失败问题

问题根源

编译失败主要有两个核心原因:

  • Gradle处于离线模式,无法下载缺失的kotlin-script-runtime:1.7.0依赖缓存
  • AGP 8.0与当前使用的Kotlin 1.7.0版本不兼容,同时Compose版本也未匹配AGP 8.0的要求

分步解决方案

1. 关闭Gradle离线模式

错误提示明确指出离线模式导致无法获取依赖,先关闭该模式:

  • 点击Android Studio右上角的Gradle大象图标
  • 在弹出的Gradle窗口中,点击顶部的「Toggle Offline Mode」飞机图标,确保按钮处于未激活状态

2. 升级Kotlin版本适配AGP 8.0

AGP 8.0要求Kotlin版本最低为1.8.0,修改顶级build.gradle中的Kotlin插件版本:

plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.22' apply false // 升级到兼容AGP8.0的稳定版本
}

3. 同步升级Compose版本与Kotlin版本匹配

Kotlin 1.8.x对应推荐的Compose版本为1.4.3,需同步修改compose_ui_version和编译扩展版本:
修改顶级build.gradle:

buildscript {
    ext {
        compose_ui_version = '1.4.3'
    }
}

修改模块内build.gradle的composeOptions:

composeOptions {
    kotlinCompilerExtensionVersion '1.4.3' // 必须与Compose UI版本一致
}

4. 升级核心依赖至兼容版本

为避免版本冲突,同步升级几个核心AndroidX依赖:

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation "androidx.compose.material:material:1.4.3" // 同步升级material库版本
    // 其他测试、Compose工具类依赖保持原引用方式即可
}

5. 清理并重新构建项目

  • 点击顶部工具栏的「Sync Project with Gradle Files」按钮同步配置
  • 执行Build > Clean Project,再执行Build > Rebuild Project

最终配置示例

顶级build.gradle

buildscript {
    ext {
        compose_ui_version = '1.4.3'
    }
}
plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.22' apply false
}

模块build.gradle(关键修改部分)

android {
    namespace 'edu.qc.seclass.tipcalculatoractivity'
    compileSdk 33

    // ...其他默认配置、buildTypes等保持不变

    composeOptions {
        kotlinCompilerExtensionVersion '1.4.3'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation "androidx.compose.material:material:1.4.3"
    
    // 以下测试依赖保持原版本即可
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 12:57:53