Flutter 2.10 升级填坑指南

相信大家已经都在对 Flutter 2.10 版本跃跃欲试,本篇就目前升级用 Flutter 2.10 版本遇到的问题做一些总结提炼。

事实上按照 Flutter 每个版本的投入使用规律,应该是第三个小版本最稳,以 Flutter 目前庞大的用户量,每次正式版的发布必然带来各种奇奇怪怪的问题,一般情况下我推荐 2.10 版本等到 2.10.3 发布再投入生产会更稳妥,但是如果你等不及官方 hotfix ,那么后面的内容可能可以帮助到你。

本次如果你是从 2.8 升级的到 2.10 ,那么 dart 层需要调整几乎等于零。

Kotlin 版本

首先就项目升级的第一个,也就是最重要的一个,就是升级你的 kotlin 插件版本,这个是强制的,因为之前的旧版本使用的基本都是 1.3.x 的版本,而这些 Flutter 2.10 强制要求 1.5.31 以上的版本。

buildscript {
-    ext.kotlin_version = '1.3.50'
+    ext.kotlin_version = '1.5.31'

这里需要注意,这次升级 Kotlin 版本,会带来一些 Kotlin 包的 API 出现一些 break 的变化 ,所以如果你本身 App 使用了较多 Kotlin 开发,或者插件里使用了一些 Kotlin 的包,就需要注意升级带来的适配成本,例如:

ProducerScope 需要 override 新的 trySend 方法,但是这个方法需要 return 一个 ChannelResultChannelResult@InternalCoroutinesApi

Gradle 版本

因为 Kotlin 版本升级了,所以 AGP 插件必须使用最低 4.0.0 配合 Gradle 6.1.1 的版本,也就是:

classpath 'com.android.tools.build:gradle:4.0.0'
 /
distributionUrl=https://services.gradle.org/distributions/gradle-6.1.1-all.zip

因为以前的老版本使用的 AGP 可能是 AGP 3.x 配合 Gradle 5.x 的版本,所以如果升级了 Kotlin 版本,这一步升级就必不可少。

这里顺便放一张 AGP 和 Gradle 之间的版本对应截图

image

Android SDK 问题

cmdline-tools & license

这个问题可能大家不一定会遇到,首先如果你在执行 flutter doctor 的时候出现以下情况

[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for
      more details.

也就是 cmdline-toolsAndroid license 都是 的显示时,那可能你还需要额外做一些步骤来完善配置。

首先你需要安装 cmdline-tools ,如下图所示直接安装就可以了

image

然后执行 flutter doctor --android-licenses ,就可以很简单地完善你的环境的配置。

Build Tools

其次,如果你在编译 Android Apk 的过程中出现 : Installed Build Tools revision 31.0.0 is corrupted 之类的问题:

Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.

那么可以通过执行如下命令行来完成配置 :

# change below to your Android SDK path
cd ~/Library/Android/sdk/build-tools/31.0.0 \
  && mv d8 dx \
  && cd lib  \
  && mv d8.jar dx.jar

Window 用户可以看 https://stackoverflow.com/questions/68387270/android-studio-error-installed-build-tools-revision-31-0-0-is-corrupted

NDK

如果你在编译过程中出现 No version of NDK matched 的问题:

Execution failed for task ':app:stripDebugDebugSymbols'.
> No version of NDK matched the requested version 21.0.6113669. Versions available locally: 19.1.5304403

这个问题其实很简单,如图打开你的 SDK Manager 下载对应的版本就可以了。

image

本地 AAR 文件问题

因为前面升级了 AGP 版本,这时候就带来一个问题,这个问题仅存在于你使用的 Flutter Plugin 里的本地的 aar 文件

正常情况下编译时就会遇到如果的提示:

> Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The following direct local .aar file dependencies of the :********* project caused this error: /Users/guoshuyu/.pub-cache/git/*********-01d03bf549e512f6e15dd539411a8c236d77cd47/android/libs/libc*********.aar, /Users/guoshuyu/.pub-cache/git/*********-01d03bf549e512f6e15dd539411a8c236d77cd47/android/libs/*********.aar, /Users/guoshuyu/.pub-cache/git/*********-01d03bf549e512f6e15dd539411a8c236d77cd47/android/libs/*********.aar

这时候听我一声劝,什么办法都不好使,直接搭一个私服 Maven ,很简单的,把 aar 上传上去,然后远程依赖进来就可以了

强制 V2

Android 上在这个版本上就强制要求 V2 的,例如如果之前使用了 android:name="io.flutter.app.FlutterApplication" ,那么在编译时你会看到:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Warning
──────────────────────────────────────────────────────────────────────────────
Your Flutter application is created using an older version of the Android
embedding. It is being deprecated in favor of Android embedding v2. Follow the
steps at

https://flutter.dev/go/android-project-migration

to migrate your project. You may also pass the --ignore-deprecation flag to
ignore this check and continue with the deprecated v1 embedding. However,
the v1 Android embedding will be removed in future versions of Flutter.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The detected reason was:

  /Users/guoshuyu/workspace/***/*********/android/app/src/main/AndroidManifest.xml uses `android:name="io.flutter.app.FutterApplication"`
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

这里如果你只需要简单删除 android:name="io.flutter.app.FutterApplication" 就可以了。

更多关于 V2 的可以参考:https://flutter.dev/go/android-project-migration

Material 图标出现异常

Flutter 2.10 针对 Material Icon 做了一次升级,结果很明显这次发布不小心又挖了个坑,目前问题看起来是因为某个 issue 的回滚导致部分 icon 的提交也被回退,所以这部分只能静待 hotfix ,目前官方已经知道这个问题,具体可见:

https://github.com/flutter/flutter/issues/97767

iOS CocoaPods not installed

如果你运行 iOS 出现 CocoaPods not installed 的错误提示,那么不要着急,这个是 Android Studio 团队的锅

Warning: CocoaPods not installed. Skipping pod install.
  CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
  Without CocoaPods, plugins will not work on iOS or macOS.
  For more info, see https://flutter.dev/platform-plugins
To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

Exception: CocoaPods not installed or not in valid state.

其实你在执行 flutter doctor 时可能就是看到提示,说你本地缺少 CocoaPods , 但是实际上你本地是有 CocoaPods 的,这时候解决的方案有几个可以选择:

  • 直接通过命令行 flutter run 运行就不会有这个问题;
  • 通过命令行 open /Applications/Android\ Studio.app 启动 Android Studio ;
  • 执行 chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv (如果你使用了 JetBrains Toolbox ,那 printenv 文件路径可能会有所变化)
  • 静待 Android Studio 的小版本更新

更多可以参考 : https://github.com/flutter/flutter/issues/97251

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容