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

Flutter打包APK后启动闪屏变白,伴Kotlin版本不兼容警告

Flutter Release APK闪屏后白屏+Kotlin版本不兼容警告解决方案

一、先解决Kotlin版本不兼容问题

构建时出现的Kotlin元数据版本不兼容警告(1.8.0 vs 1.6.0),是项目依赖的Kotlin版本过低,与第三方库编译用的Kotlin版本冲突导致。即使构建成功,运行时可能触发类加载异常,导致初始化失败进而出现白屏。

修复步骤:

  1. 打开项目根目录下的android/build.gradle文件,将Kotlin版本修改为警告中出现的1.8.22:
buildscript {
    ext.kotlin_version = '1.8.22' // 替换原有的1.6.x版本
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // 确保Gradle版本与Kotlin版本兼容,Gradle 7.x+适配Kotlin 1.8.x
        classpath "com.android.tools.build:gradle:7.4.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
  1. 清理项目缓存并重新构建:
flutter clean
flutter pub get
flutter build apk --release

二、排查白屏的其他潜在原因

如果修复Kotlin版本后仍出现白屏,需检查Release模式下的初始化和渲染逻辑:

1. 捕获初始化阶段的异常

setUpLocator()是异步初始化逻辑,Release模式下未捕获的异常会导致应用崩溃或白屏。修改入口代码添加异常捕获:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  try {
    await setUpLocator();
  } catch (e, stackTrace) {
    FlutterError.reportError(FlutterErrorDetails(
      exception: e,
      stack: stackTrace,
      library: 'dependency_injection',
    ));
    // 可选:Release模式下将日志写入本地文件,方便后续排查
  }
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(const MobileScaffoldApp(isDebug: false));
}

2. 检查首屏布局

确认MobileScaffoldApp的首屏是否设置了正确的根组件(如Scaffold)和背景色,避免因无有效内容渲染显示白屏。

3. 验证ProGuard/R8混淆规则

如果启用了代码混淆,确保依赖注入框架、第三方库的关键类未被错误移除。可在android/app/proguard-rules.pro中添加必要的保留规则,例如:

-keep class com.yourpackage.locator.** { *; }

内容的提问来源于stack exchange,提问作者Priscilla Abhulimen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 05:40:12