flutter 的安装应该比较简单,已经记不清楚具体过程了,反正由于国情的特殊性,貌似是搭过梯子的。当时准备就绪后并未立即深度着手,以至于昨天重拾,就遇到了一堆问题。
前一阵有合作伙伴对产品提了个要求,说要支持内网的 C/S 结构访问服务,而不要只支持 B/S 结构的。三太爷在最短的时间给团队指了一条明路,那就是用 Electron,在本地把浏览器包装起来,最后在与其时间范围内基本达到了想要的效果。
昨天又来了一拨合作伙伴,希望能在移动设备上访问服务。老夫第一时间又想到一个完美的技术栈:flutter。正好记起电脑上之前安装过,就顺手编译个例子吧。在基友哈勃上下载了个两年前别人写的例子,仿知乎客户端。编译过程举步维艰,困难重重。
flutter 的项目编译,最简洁的方式,很有点 Web 项目的风格,就是把一台手机连接到电脑,命令行执行 flutter run
。这会触发其一系列的连锁反应,编译、链接、打包、安装、启动、调试。随之而来的,当然就是这各个环节都可能发生的问题,需要仔细辨别,分而治之。
三太爷有个不是很稳当的习惯,就是喜欢顺手把这种各样的东西 upgrade。不管是 apt,还是 yum/dnf,或者 brew、npm 之流。flutter 也有这个命令,但是一直失败,因为你翻或不翻,墙就在那里。后来才知道,flutter 项目为了解决大家遇到的这个问题,甚至都给出了变通的解决方案,指定以下的环境变量:
1 2 |
FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn PUB_HOSTED_URL=https://pub.flutter-io.cn |
然后再正常 flutter upgrade
。另外最好记住,flutter 提供的 flutter doctor
命令是诊断帮手,它也会触发更新。在我的电脑上,它的执行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
C:\code\zhihu-flutter>flutter doctor Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source! Downloading package sky_engine... 39.2s Downloading flutter_patched_sdk tools... 19.2s Downloading flutter_patched_sdk_product tools... 19.4s Downloading windows-x64 tools... 47.0s Downloading windows-x64/font-subset tools... 4.7s Downloading android-arm-profile/windows-x64 tools... 3.4s Downloading android-arm-release/windows-x64 tools... 2.5s Downloading android-arm64-profile/windows-x64 tools... 3.4s Downloading android-arm64-release/windows-x64 tools... 3.2s Downloading android-x64-profile/windows-x64 tools... 3.3s Downloading android-x64-release/windows-x64 tools... 2.9s Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 1.20.2, on Microsoft Windows [Version 10.0.19041.450], locale zh-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3) [!] Android Studio (version 4.0) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. [✓] VS Code (version 1.48.0) [✓] Connected device (1 available) ! Doctor found issues in 1 category. |
解决了更新问题,接下来的 flutter run
又出问题,从错误信息来看,都与网络相关,不是 connection shutdown 就是 download failed,要不就是 unknown host 异常,最后切到香港的线路才算勉强应付过去。
第三关是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... [!] Your app isn't using AndroidX. To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY . Running Gradle task 'assembleDebug'... Checking the license for package Android SDK Build-Tools 26.0.2 in C:\Users\xxxxx\AppData\Local\Android\Sdk\licenses License for package Android SDK Build-Tools 26.0.2 accepted. Preparing "Install Android SDK Build-Tools 26.0.2 (revision: 26.0.2)". "Install Android SDK Build-Tools 26.0.2 (revision: 26.0.2)" ready. Installing Android SDK Build-Tools 26.0.2 in C:\Users\xxxxx\AppData\Local\Android\Sdk\build-tools\26.0.2 "Install Android SDK Build-Tools 26.0.2 (revision: 26.0.2)" complete. "Install Android SDK Build-Tools 26.0.2 (revision: 26.0.2)" finished. Checking the license for package Android SDK Platform 27 in C:\Users\xxxxx\AppData\Local\Android\Sdk\licenses License for package Android SDK Platform 27 accepted. Preparing "Install Android SDK Platform 27 (revision: 3)". "Install Android SDK Platform 27 (revision: 3)" ready. Installing Android SDK Platform 27 in C:\Users\xxxxx\AppData\Local\Android\Sdk\platforms\android-27 "Install Android SDK Platform 27 (revision: 3)" complete. "Install Android SDK Platform 27 (revision: 3)" finished. FAILURE: Build failed with an exception. * Where: Build file 'C:\code\zhihu-flutter\android\build.gradle' line: 24 * What went wrong: A problem occurred evaluating root project 'android'. > A problem occurred configuring project ':app'. > No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. * Get more help at https://help.gradle.org BUILD FAILED in 36m 11s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 2173.0s (!) Exception: Gradle task assembleDebug failed with exit code 1 |
这个错误原来写 Android 项目也遇到过,通常是 gradle 版本导致的问题,找了个别的可以正常编译的工程看了一下,把项目的 build.gradle 中以下代码段
1 2 3 4 5 6 7 8 9 10 |
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' } } |
里面的 3.0.1
照着改成了 4.0.1
。于是跟着第四关就来了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... [!] Your app isn't using AndroidX. To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY . Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * Where: Build file 'C:\code\zhihu-flutter\android\app\build.gradle' line: 14 * What went wrong: A problem occurred evaluating project ':app'. > Failed to apply plugin [id 'com.android.internal.version-check'] > Minimum supported Gradle version is 6.1.1. Current version is 4.1. If using the gradle wrapper, try editing the distributionUrl in C:\Dandy\code\personal\zhihu-flutter\android\gradle\wrapper\gradle-wrapper.properties to gradle-6.1.1-all.zip * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. * Get more help at https://help.gradle.org BUILD FAILED in 1m 59s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 120.9s (!) Exception: Gradle task assembleDebug failed with exit code 1 |
这一关很简单,因为答案就在信息里,4.1
-> 6.1.1
之后,第五关:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... [!] Your app isn't using AndroidX. To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY . Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugAssets'. > This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry. The following AndroidX dependencies are detected: androidx.core:core:1.1.0, androidx.fragment:fragment:1.1.0, androidx.versionedparcelable:versionedparcelable:1.1.0, androidx.lifecycle:lifecycle-common:2.2.0, androidx.lifecycle:lifecycle-common-java8:2.2.0, androidx.customview:customview:1.0.0, androidx.lifecycle:lifecycle-livedata:2.0.0, androidx.lifecycle:lifecycle-viewmodel:2.1.0, androidx.loader:loader:1.0.0, androidx.lifecycle:lifecycle-livedata-core:2.0.0, androidx.activity:activity:1.0.0, androidx.collection:collection:1.1.0, androidx.viewpager:viewpager:1.0.0, androidx.arch.core:core-runtime:2.0.0, androidx.arch.core:core-common:2.1.0, androidx.savedstate:savedstate:1.0.0, androidx.annotation:annotation:1.1.0, androidx.lifecycle:lifecycle-runtime:2.2.0 * 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 2m 21s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 142.2s (!) Exception: Gradle task assembleDebug failed with exit code 1 |
也按图索骥,打开 settings.gradle,加入了 android.useAndroidX=true
。从而直达第七关:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugJavaResource'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not download flutter_embedding_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar (io.flutter:flutter_embedding_debug:1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95) > Could not get resource 'https://storage.flutter-io.cn/download.flutter.io/io/flutter/flutter_embedding_debug/1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95/flutter_embedding_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar'. > Connection reset > Could not download arm64_v8a_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar (io.flutter:arm64_v8a_debug:1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95) > Could not get resource 'https://storage.flutter-io.cn/download.flutter.io/io/flutter/arm64_v8a_debug/1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95/arm64_v8a_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar'. > Connection reset > Could not download x86_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar (io.flutter:x86_debug:1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95) > Could not get resource 'https://storage.flutter-io.cn/download.flutter.io/io/flutter/x86_debug/1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95/x86_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar'. > Connection reset > Could not download x86_64_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar (io.flutter:x86_64_debug:1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95) > Could not get resource 'https://storage.flutter-io.cn/download.flutter.io/io/flutter/x86_64_debug/1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95/x86_64_debug-1.0.0-9d5b21729ff53dbf8eadd8bc97e0e30d77abec95.jar'. > Connection reset * 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 2m 1s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 123.0s (!) Exception: Gradle task assembleDebug failed with exit code 1 |
哦,对不起,又是下载问题,原来是在香港链路上要下载墙内资源,我改,撤梯;第八关长这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugResources'. > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade > Android resource linking failed C:\Users\dandy\.gradle\caches\transforms-2\files-2.1\76f2b6439d16df60df3177021482ee5a\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontVariationSettings not found. C:\Users\dandy\.gradle\caches\transforms-2\files-2.1\76f2b6439d16df60df3177021482ee5a\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/ttcIndex not found. * 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 4s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 5.5s Exception: Gradle task assembleDebug failed with exit code 1 |
网上搜了一下,解决方法是,把 app/build.gradle 里的 compileSdkVersion
从 27
升到 28
。第九关:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:stripDebugDebugSymbols'. > No version of NDK matched the requested version 21.0.6113669. Versions available locally: 21.3.6528147 * 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 1m 16s Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 77.8s Exception: Gradle task assembleDebug failed with exit code 1 |
大意是,编译呢,需要版本 21.0.6113669 的 NDK,但本地只找到了 21.3.6528147 版本的。最直观的解决办法就是用 SDK Manager 把它需要的版本下载安装到本地,但那个一听就有点逊,多装一份 NDK 不说,还特么是个老版本的。最后在 app/build.gradle 里的 android
节中加入了 ndkVersion "21.3.6528147"
让它强制使用本地已经安装好的版本的 NDK 即可。然后,历经这许多难,俺就愉快地玩耍了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
C:\code\zhihu-flutter>flutter run Launching lib\main.dart on SM G9650 in debug mode... Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... Done 7.8s ✓ Built build\app\outputs\flutter-apk\app-debug.apk. Installing build\app\outputs\flutter-apk\app.apk... 5.4s Waiting for SM G9650 to report its views... 12ms Syncing files to device SM G9650... 335ms Flutter run key commands. r Hot reload. 🔥🔥🔥 R Hot restart. h Repeat this help message. d Detach (terminate "flutter run" but leave application running). c Clear the screen q Quit (terminate the application on the device). An Observatory debugger and profiler on SM G9650 is available at: http://127.0.0.1:2753/yTf5DVNf6pU=/ |
记录这么详细,就是怕哪天需要返回来看,悲哀。