Flutter编译报错::app:compileFlutterBuildDebug执行失败求助
问题描述
每次运行Flutter项目到Android模拟器时编译失败,但Chrome浏览器可正常运行。核心错误为无法写入ink_sparkle.frag.spirv文件,同时VS Code提示需将Gradle JDK从1.8升级到11。
错误日志
Launching lib\main.dart on sdk gphone x86 arm in debug mode... Running Gradle task 'assembleDebug'... Could not write file to C:\Users\elkha\OneDrive\Документы\crypto_list2\build\app\intermediates\flutter\debug\flutter_assets\shaders/ink_sparkle.frag.spirv Target debug_android_application failed: ShaderCompilerException: Shader compilation of "C:\Users\elkha\flutter\packages\flutter\lib\src\material\shaders\ink_sparkle.frag" to "C:\Users\elkha\OneDrive\Документы\crypto_list2\build\app\intermediates\flutter\debug\flutter_assets\shaders/ink_sparkle.frag" failed with exit code 1. impellerc stdout: impellerc stderr: Could not write file to C:\Users\elkha\OneDrive\Документы\crypto_list2\build\app\intermediates\flutter\debug\flutter_assets\shaders/ink_sparkle.frag.spirv FAILURE: Build failed with an exception. * Where: Script 'C:\Users\elkha\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1151 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\Users\elkha\flutter\bin\flutter.bat'' finished with non-zero exit value 1 * 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 49s Exception: Gradle task assembleDebug failed with exit code 1
项目配置信息
app模块build.gradle
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = '11' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "com.elcode.crypto_list2" minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
根目录build.gradle
buildscript { ext.kotlin_version = '1.5.30' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
settings.gradle
include ':app' def localPropertiesFile = new File(rootProject.projectDir, "local.properties") def properties = new Properties() assert localPropertiesFile.exists() localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
gradle.properties
org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true android.overridePathCheck=true
已尝试无效方案
- Android Studio中寻找Gradle项目设置修改JDK,未找到入口
- 修改build.gradle配置无效果
- 降级Gradle版本失败
解决方案
1. 解决Shader文件写入失败问题
项目存储在OneDrive目录下,写入失败大概率是OneDrive同步锁或权限限制导致:
- 直接将项目移动到本地普通文件夹(如
C:\Projects\crypto_list2),执行flutter clean后重新编译 - 若不想移动项目,暂时暂停OneDrive同步后再尝试编译
2. 解决Gradle JDK版本升级问题
Android Studio图形化设置
- 点击顶部菜单栏
File->Project Structure(快捷键Ctrl+Alt+Shift+S) - 左侧面板选择
Project,右侧找到Gradle JDK选项,选择已安装的JDK 11;若无则点击下拉框Download JDK选择版本11自动安装 - 点击
Apply和OK保存设置
手动指定JDK路径
编辑项目根目录的gradle.properties,添加以下内容(路径根据自身Android Studio自带JDK位置调整):
org.gradle.java.home=C:\\Program Files\\Android\\Android Studio\\jbr
3. 额外排查步骤
- 执行
flutter doctor检查Android环境组件是否配置完整 - 执行
flutter clean清除旧编译缓存,再运行flutter run
内容的提问来源于stack exchange,提问作者Elkhan Jafarov
相关产品推荐
相关产品推荐

