求助:document_scanner_flutter插件Kotlin版本兼容问题及升级方法
解决document_scanner_flutter与Kotlin版本不兼容的方案
方案一:强制项目统一Kotlin版本(最简单)
- 在Flutter项目的
android/build.gradle文件中添加配置,强制所有模块使用兼容的Kotlin版本:
找到buildscript块,指定Kotlin版本(需为1.5.20及以上,建议匹配你的Android Gradle插件版本),同时通过subprojects块覆盖所有依赖的Kotlin版本:buildscript { ext.kotlin_version = "1.8.20" // 可替换为1.5.20+的兼容版本 repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.4.2" // 确保AGP版本与Kotlin版本兼容 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } subprojects { project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'org.jetbrains.kotlin' && details.requested.name.startsWith("kotlin-")) { details.useVersion "$kotlin_version" } } } } - 同步Gradle后重新运行项目,该配置会强制插件使用你指定的Kotlin版本,无需修改插件源码。
方案二:本地修改插件源码(适合需要定制插件的场景)
- 找到插件的本地目录:可以从GitHub克隆插件源码,或者直接访问Flutter pub缓存路径
~/.pub-cache/hosted/pub.dartlang.org/document_scanner_flutter-0.2.7/android - 进入插件的
android目录,打开build.gradle文件,修改kotlin-gradle-plugin的版本号:dependencies { classpath 'com.android.tools.build:gradle:4.1.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20" // 将1.3.50替换为1.5.20+版本 } - 在项目的
pubspec.yaml中,将插件依赖改为本地路径:dependencies: document_scanner_flutter: path: ./本地插件目录路径 // 替换为你修改后的插件本地路径 - 执行
flutter pub get后重新构建项目。
方案三:替换为维护活跃的替代插件
如果不想折腾旧插件,可换用维护状态更好的同类插件,比如:
flutter_document_scanner:兼容最新Android/iOS依赖document_scanner:适配新版本生态
替换pubspec.yaml中的依赖后,按照新插件的文档完成集成即可。
内容的提问来源于stack exchange,提问作者joshua
相关产品推荐
相关产品推荐

