Flutter执行flutter build apk时build.gradle报unexpected token错误
错误根因
你遇到的构建报错核心是Gradle语法解析失败,具体问题出在build.gradle的defaultConfig代码块:versionName字段要求传入字符串类型值,你当前写的1.0.0未加引号,被识别为浮点数格式,触发语法错误,间接导致后续android {代码块被判定为非法输入。
修复步骤
- 找到
defaultConfig代码块,将versionName 1.0.0修改为versionName "1.0.0" - 可选优化:若你使用的Android Gradle插件版本≥7.0,可将
buildTypes.release下的useProguard true替换为isMinifyEnabled true,避免废弃参数警告。
修复后完整代码
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 GradleException("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" def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { // TODO: Specify your own unique Application ID applicationId "com.company.project" minSdkVersion 23 targetSdkVersion 30 versionCode 1 versionName "1.0.0" } signingConfigs { release { storeFile file('../keystore/key.jks') storePassword file('../keystore/keystore.password').text.trim() keyPassword file('../keystore/keystore.password').text.trim() keyAlias 'key' } } buildTypes { release { signingConfig signingConfigs.release minifyEnabled true useProguard true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
修改完成后重新执行flutter build apk --split-per-abi即可正常构建。
内容的提问来源于stack exchange,提问作者97_HYUN
相关产品推荐
相关产品推荐

