Flutter集成TFLite时Android构建报错:找不到compileSdkVersion()方法
Flutter集成TFLite Android构建失败解决办法
问题本质
报错Could not find method compileSdkVersion() for arguments [28]的核心原因是**compileSdkVersion的语法位置错误**——新版Android Gradle Plugin(AGP)要求该属性必须放在android代码块内部,不能直接在项目根级别调用;也有可能是Gradle与AGP版本不兼容,导致语法不被识别。
解决步骤
- 打开
C:\Users\fox\untitled\android\app\build.gradle文件,定位到第30行 - 将
compileSdkVersion 28移动到android代码块内部,与defaultConfig同级位置,正确代码结构示例:
android { compileSdkVersion 28 // 确保该代码在android块内 defaultConfig { applicationId "com.example.untitled" minSdkVersion 21 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } }
- (可选)检查项目根目录
android/build.gradle中的版本兼容性:
若使用compileSdkVersion 28,建议将AGP版本设为4.2.2,对应Gradle版本6.7.1,避免版本不匹配问题:
// 根目录build.gradle的buildscript块 buildscript { ext.kotlin_version = '1.5.31' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:4.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
内容的提问来源于stack exchange,提问作者Abdelrahman Ahmed
相关产品推荐
相关产品推荐

