更新Flutter、Dart、Java及Android Studio后App构建失败求助
Flutter更新后构建失败的解决方法
问题描述
此前我的Flutter项目运行完全正常,但在将Flutter、Dart、Java及Android Studio全部更新至最新版本后,构建操作现在失败了。构建过程中的输出信息如下:
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode... Running Gradle task 'assembleDebug'... Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: unexpected element (uri:"", local:"extension-level"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugMainManifest'. > Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module @7353d2ea * 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 24s Exception: Gradle task assembleDebug failed with exit code 1
核心原因
更新到高版本Java(如Java 17)后,旧版Gradle无法适配Java模块系统的访问权限限制,导致Manifest处理任务失败。那些XML命名空间警告是SDK版本更新后的兼容提示,不影响构建结果。
解决步骤
1. 升级项目Gradle版本
- 打开项目根目录下的
android/build.gradle文件,找到dependencies块中的Gradle插件依赖,将其更新为与当前Android Studio匹配的稳定版本,例如:dependencies { classpath 'com.android.tools.build:gradle:7.4.2' // 其他依赖... } - 打开
android/gradle/wrapper/gradle-wrapper.properties文件,修改distributionUrl为对应版本的Gradle发行包,例如插件7.4.x对应Gradle 7.5:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
2. 配置Java兼容性
打开android/app/build.gradle文件,在android代码块中添加Java版本兼容配置:
android { // 其他配置... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // 如果项目使用Kotlin,添加以下配置 kotlinOptions { jvmTarget = '1.8' } }
3. 清理缓存并重新构建
执行以下命令清理项目缓存,然后重新构建:
flutter clean flutter pub get flutter run
内容的提问来源于stack exchange,提问作者Paul Coshott
相关产品推荐
相关产品推荐

