Flutter项目GitLab CI/CD构建APK失败求助:配置与依赖排查
GitLab CI构建Flutter APK失败:依赖下载超时及Android配置问题
问题概述
本地Android Studio终端可正常构建Flutter项目APK,但GitLab CI执行构建时失败,核心错误为Gradle无法下载Kotlin协程依赖(超时),同时子模块:archem_idcard找不到android属性。
现有配置
.gitlab-ci.yml
image: cirrusci/flutter:3.7.1 stages: - build cache: paths: - .pub-cache/ before_script: - cd my_app - export PUB_HOSTED_URL=https://pub.flutter-io.cn - export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn build: stage: build script: - flutter build apk --target-platform android-arm,android-arm64,android-x64 --no-sound-null-safety
报错信息
Dart 3 will only support sound null safety, see https://dart.dev/null-safety Running Gradle task 'assembleRelease'... FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':archem_idcard'. > Could not resolve all files for configuration ':archem_idcard:classpath'. > Could not download kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) > Could not get resource 'https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.4.1/kotlinx-coroutines-core-jvm-1.4.1.jar'. > Read timed out > Failed to notify project evaluation listener. > Could not get unknown property 'android' for project ':archem_idcard' of type org.gradle.api.Project. > Could not get unknown property 'android' for project ':archem_idcard' of type org.gradle.api.Project. * 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 BUILD FAILED in 1h 45m 19s
已尝试操作
配置了国内Flutter镜像(https://pub.flutter-io.cn和https://storage.flutter-io.cn),但问题未解决。
解决方案
1. 给Gradle配置国内镜像
修改项目根目录下的android/build.gradle,在buildscript和allprojects的repositories中添加国内Maven镜像,优先从国内源下载依赖:
buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'http://maven.aliyun.com/nexus/content/groups/public' } google() mavenCentral() } // 保留原有的dependencies配置 } allprojects { repositories { maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'http://maven.aliyun.com/nexus/content/groups/public' } google() mavenCentral() } }
2. 修复:archem_idcard模块的Android配置
检查子模块archem_idcard的build.gradle文件,确保已正确应用Android插件并配置android闭包:
- 顶部添加插件声明:
plugins { id 'com.android.library' id 'kotlin-android' }
- 确保存在
android配置块:
android { compileSdkVersion 33 // 替换为项目实际使用的SDK版本 defaultConfig { minSdkVersion 21 // 替换为项目实际的最低SDK版本 targetSdkVersion 33 versionCode 1 versionName "1.0" } // 其他Android配置(如buildTypes等) }
3. 扩展GitLab CI缓存范围
将Gradle缓存目录加入缓存,减少重复下载耗时:
修改.gitlab-ci.yml的cache部分:
cache: paths: - .pub-cache/ - ~/.gradle/caches/ - ~/.gradle/wrapper/
4. 延长CI构建超时时间
当前构建耗时接近2小时,在build阶段添加超时配置:
build: stage: build timeout: 2h 30m script: - flutter build apk --target-platform android-arm,android-arm64,android-x64 --no-sound-null-safety
内容的提问来源于stack exchange,提问作者lz_researcher
相关产品推荐
相关产品推荐

