You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目集成sqflite包后Android端无法运行,求解决方案

解决sqflite在Android编译时的Build.VERSION_CODES.TIRAMISU与ApplicationInfoFlags找不到问题

问题描述

我创建新Flutter项目后,通过flutter pub add sqflite安装了sqflite 2.3.0包,iOS模拟器运行正常,但Android模拟器编译时报错:

/Users/receparda/.pub-cache/hosted/pub.dev/sqflite-2.3.0/android/src/main/java/com/tekartik/sqflite/Database.java:107: error: cannot find symbol
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                                                            ^
  symbol:   variable TIRAMISU
  location: class VERSION_CODES
/Users/receparda/.pub-cache/hosted/pub.dev/sqflite-2.3.0/android/src/main/java/com/tekartik/sqflite/Database.java:108: error: cannot find symbol
                applicationInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA));
                                                                                          ^
  symbol:   variable ApplicationInfoFlags
  location: class PackageManager
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':sqflite:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* 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

BUİLD FAILED in 1s
Exception: Gradle task assembleDebug failed with exit code 1

已尝试将compileSdkVersion和targetSdkVersion改为33、34,升级Gradle到8.5,问题仍未解决。

解决方案

方案1:升级sqflite到兼容版本

sqflite 2.3.0的Android代码依赖Android 13(API 33)的SDK常量,直接升级到最新稳定版通常能解决这类API兼容问题:

  • 执行命令:flutter pub upgrade sqflite
  • 或手动修改pubspec.yaml中sqflite的版本号为最新版,再执行flutter pub get

方案2:确保Android SDK组件安装完整

  1. 打开Android Studio的SDK Manager,检查是否安装了**Android 13 (API 33)**的SDK Platform和对应版本的Build Tools
  2. 确认项目配置中SDK版本统一:
    • 项目级android/build.gradle:
      buildscript {
          ext {
              compileSdkVersion = 33
              // 其他配置...
          }
      }
      
    • 模块级android/app/build.gradle:
      android {
          compileSdkVersion rootProject.ext.compileSdkVersion
          defaultConfig {
              targetSdkVersion rootProject.ext.compileSdkVersion
              // 其他配置...
          }
      }
      

方案3:强制指定sqflite模块的编译SDK版本

如果升级包和调整全局SDK版本无效,可在android/app/build.gradle中添加配置,强制sqflite模块使用指定版本:

android {
    // 其他配置...
    subprojects {
        if (name.contains('sqflite')) {
            android {
                compileSdkVersion 33
            }
        }
    }
}

方案4:清理缓存后重新构建

  1. 执行Flutter缓存清理:flutter clean
  2. 删除.pub-cache中sqflite的对应版本目录(路径:/Users/receparda/.pub-cache/hosted/pub.dev/sqflite-2.3.0)
  3. 重新获取依赖:flutter pub get
  4. 重新构建项目:flutter run

内容的提问来源于stack exchange,提问作者Recep Arda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 16:20:11