iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💥

How to Fix "Dart Error: Can't load Kernel binary"

に公開

When you build and run a Flutter project, the following error may be output to the log, and the screen may not be displayed, causing the process to hang. This occurs on all platforms.

[ERROR:flutter/shell/common/shell.cc(93)] Dart Error: Can't load Kernel binary: Invalid kernel binary format version.
[ERROR:flutter/runtime/dart_isolate.cc(143)] Could not prepare isolate.
[ERROR:flutter/runtime/runtime_controller.cc(385)] Could not create root isolate.
[ERROR:flutter/shell/common/shell.cc(604)] Could not launch engine with configuration.

This error occurs when there are binaries built with different versions of Dart. The main cause is a mismatch between the Dart version used to build commands like flutter and the Dart version used to build the project. Therefore, it is likely to occur after updating Flutter.

There are several similar articles, but I am documenting this because there were methods other than deleting the cache.

Solutions

Clear the cache

Delete cached pre-built binaries. This usually resolves the issue.

  • Run flutter clean. This deletes the build directory directly under the project.
    flutter clean
    
  • Run flutter pub cache repair. This command deletes the library cache. The cache for libraries downloaded from pub.dev etc. is located at $HOME/.pub-cache.
    flutter pub cache repair
    
  • Delete the cache directory located in the same directory as the flutter command.
    $ which flutter             
    /opt/flutter/bin/flutter
    $ ls /opt/flutter/bin 
    cache           dart.bat        flutter.bat
    dart            flutter         internal
    $ rm -rf /opt/flutter/bin/cache
    

Review LD_LIBRARY_PATH

If clearing the cache does not resolve the issue, the environment variable LD_LIBRARY_PATH may be set. If there are binaries of a different version in the directory specified by LD_LIBRARY_PATH, or if the binaries required to start the app cannot be found, an error will occur. Try reviewing the contents of LD_LIBRARY_PATH or try without setting it.

Preventive Measures

Introducing FVM (Flutter Version Management) allows you to verify with past versions when trouble occurs. It is also convenient because it can be used for verification with the latest version as well.

Discussion