Flutter Android项目Amplify Datastore构建报错:包名含下划线
Flutter Android项目Amplify Datastore构建失败(KtLint包名校验错误)
问题描述
执行./gradlew build构建项目时失败,报错如下:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':amplify_datastore:ktlintTestSourceSetFormat'. > A failure occurred while executing org.jlleitschuh.gradle.ktlint.worker.ConsoleReportWorkAction > KtLint found code style violations. Please see the following reports: > - /Users/seanbrown/project/watmobiledev/build/amplify_datastore/reports/ktlint/ktlintTestSourceSetFormat/ktlintTestSourceSetFormat.txt
执行./gradlew ktlintFormat排查后确认,核心问题是amplify_datastore依赖包的包名包含下划线,且该问题无法自动修复:
/Users/seanbrown/.pub-cache/hosted/pub.dev/amplify_datastore-1.4.2/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/hub/FlutterSubscriptionsEstablishedEvent.kt:4:1 Package name must not contain underscore (cannot be auto-corrected)
已尝试的操作:
- 将pubspec.yaml中的amplify_datastore依赖更新为
amplify_datastore: ^1.4.0 - 删除android目录后执行
flutter create .重新初始化项目 - 按照Amplify官方文档配置Android项目
解决方案
方案1:排除第三方依赖的KtLint校验(推荐)
修改项目根目录下的android/build.gradle,添加KtLint过滤规则,跳过pub缓存中依赖包的校验:
allprojects { // ... 原有仓库配置 // 添加以下KtLint过滤配置 apply plugin: "org.jlleitschuh.gradle.ktlint" ktlint { filter { exclude { element -> element.file.path.contains(".pub-cache") } } } }
或者更精准地只排除amplify_datastore:
tasks.withType(org.jlleitschuh.gradle.ktlint.KtlintTask).configureEach { exclude("**/.pub-cache/**/amplify_datastore/**") }
方案2:临时跳过KtLint校验构建
如果只是临时需要构建项目,可以执行命令时跳过KtLint的校验任务:
./gradlew build -x ktlintTestSourceSetFormat
⚠️ 注意:这只是临时方案,不建议长期使用,会跳过代码风格校验。
方案3:升级Amplify Datastore到最新版本
检查Amplify Datastore的最新版本,升级到符合Kotlin命名规范的版本(如2.x系列),官方可能已经修复了包名的问题。修改pubspec.yaml:
dependencies: amplify_datastore: ^2.0.0 # 替换为最新兼容版本
问题原因
该问题源于旧版本Amplify Datastore的Android端代码包名使用了下划线,违反了Kotlin的命名规范(包名应使用小写字母,不含下划线),而KtLint默认会校验所有代码(包括第三方依赖库),导致构建失败。
内容的提问来源于stack exchange,提问作者Sean Brown
相关产品推荐
相关产品推荐

