Flutter打包Android App Bundle报错:找不到compileSdkVersion()方法
解决Flutter构建App Bundle时"Could not find method compileSdkVersion()"错误
问题重现
构建Android App Bundle发布版本时触发错误,提示:
Could not find method compileSdkVersion() for arguments [21] on project ':app' of type org.gradle.api.Project.
错误定位在android/app/build.gradle第36行。
错误原因
你的android代码块缺少包裹代码的大括号,导致Gradle无法识别compileSdkVersion属于Android构建配置的一部分,反而将其当作项目级别的方法调用,从而抛出找不到方法的错误。
解决方案
在第36行的android关键字后添加{,并在整个Android配置代码的末尾(buildTypes代码块之后)添加},将所有Android相关配置包裹起来。
修改后的关键代码片段:
android { // 补充左大括号 compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "com.example.eduself" minSdkVersion 21 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release } } } // 补充右大括号,闭合android代码块
验证步骤
- 保存修改后的
build.gradle文件 - 执行
flutter clean清理缓存 - 重新运行
flutter build appbundle
内容的提问来源于stack exchange,提问作者Zenji
相关产品推荐
相关产品推荐

