Android Studio(Windows)配合 Xiaomi11 Pro Flutter 真機(jī)測(cè)試,遇到了一些問題,記錄一下問題和解決方法。
flutter doctor 命令
確認(rèn)好 Flutter SDK 安裝之后,首先在終端運(yùn)行 flutter doctor 進(jìn)行開發(fā)環(huán)境檢查。
在終端使用 flutter doctor 命令檢查后,依次按需解決了 X 的問題。每一項(xiàng)檢查,我都添加了高亮的簡(jiǎn)要說明。我目前只需要在 Windows 上調(diào)試 Web 和 Android,上面的檢測(cè)已經(jīng)滿足我的需求。添加說明的檢查結(jié)果如下:
Doctor summary (to see all details, run flutter doctor -v):
// Flutter SDK 檢查,具體配置可以參照Flutter 官網(wǎng)。
[√] Flutter (Channel stable, 3.7.10, on Microsoft Windows [版本 10.0.19045.2846], locale zh-CN)
// Windows 版本檢查,查詢得知是系統(tǒng)語(yǔ)言問題,不影響編碼、調(diào)試,無視它。
[X] Windows Version (Unable to confirm if installed Windows version is 10 or greater)
// Andriod 設(shè)備支持檢查。有具體細(xì)項(xiàng)錯(cuò)誤,按照提示解決。
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
// Chrome 支持檢查。一開始配置好了就是 OK 的,我一開始也是用 Chrome 查看運(yùn)行效果的。
[√] Chrome - develop for the web
// Windows Phone 設(shè)備支持,不需要調(diào)試 Win Phone ,忽略它。
[X] Visual Studio - develop for Windows
X Visual Studio not installed; this is necessary for Windows development.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components
// Android Studio 檢查
[√] Android Studio (version 2022.1)
// VS Code 檢查
[√] VS Code (version 1.77.3)
// 可用設(shè)備檢查。4部可用設(shè)備,對(duì)應(yīng)到 Android Studio 調(diào)試設(shè)備選擇。
[√] Connected device (4 available)
// 很常見的網(wǎng)絡(luò)問題,我也沒管它。
HTTP Host availability check is taking a long time...[!] HTTP Host Availability
X HTTP host "https://maven.google.com/" is not reachable. Reason: An error occurred while checking the HTTP host: 信號(hào)燈超時(shí)時(shí)間已到
X HTTP host "https://cloud.google.com/" is not reachable. Reason: An error occurred while checking the HTTP host: 信號(hào)燈超時(shí)時(shí)間已到
! Doctor found issues in 3 categories.
Could not resolve all files for configuration ':classpath'
選擇 Web 調(diào)試很簡(jiǎn)單,只需安裝好 Flutter SDK,并使用 Android Studio 就可以實(shí)現(xiàn)。但是,當(dāng)選擇真機(jī)調(diào)試后,控制臺(tái)打印了一個(gè)問題,問題的關(guān)鍵在第一句: Could not resolve all files for configuration ':classpath' 。
經(jīng)過搜索類似問題,找到解決方案:降低 JDK 版本。我本來安裝的是 JDK 20,因?yàn)楹芏嘟鉀Q方法時(shí)間久遠(yuǎn),解決問題的操作是 JDK 13 downgrade to JDK 8” 之類的操作,所以我決定先降低 JDK 一個(gè)版本,Oracle官網(wǎng)最方便下載的可用的低版本是 JDK 17, 所以切換到了 JDK 17,在重新配置并重啟后,這個(gè)問題得到了解決。
報(bào)錯(cuò)信息如下:
Could not resolve all files for configuration ':classpath'.
Could not resolve com.android.tools.build:gradle:7.4.2. Required by: project : >
com.android.application:com.android.application.gradle.plugin:7.4.2 project : > com.android.library:com.android.library.gradle.plugin:7.4.2 > No matching variant of com.android.tools.build:gradle:7.4.2 was found.
The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.4.2' but: - Variant 'apiElements' capability com.android.tools.
build:gradle:7.4.2 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8 - Other compatible
attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2') - Variant 'javadocElements' capability com.android.tools.
build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2') - Variant 'runtimeElements' capability com.android.tools.
build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2')- Variant 'sourcesElements' capability com.android.tools.
build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2')
運(yùn)行一直卡在 Runing Gradle task‘a(chǎn)ssembleDebug'...
出現(xiàn)這個(gè)問題是因?yàn)?Gradle 默認(rèn)配置中依賴引用被墻導(dǎo)致的。
在 FlutterSDK 安裝目錄 flutter\packages\flutter_tools\gradle 中 flutter.gradle 文件中,以及項(xiàng)目生成時(shí),andriod 文件夾下 gradle 文件目錄中 build.grade 文件中,默認(rèn)配置的 repositories 和 dependencies 依賴引用無法訪問,需要替換可訪問的鏡像路徑。
只要是 Gradle 涉及到依賴庫(kù)和依賴包引用的地方,都進(jìn)行引用鏈接的修改,即可解決問題。
在 flutter.gradle 中,替換 google() , mavenCentral() 為對(duì)應(yīng)的阿里云鏡像:
repositories {
// google()
// mavenCentral()
maven{ url 'https://maven.aliyun.com/repository/google' }
maven{ url 'https://maven.aliyun.com/repository/central' }
}
在 flutter.gradle 中,替換 DEFAULT_MAVEN_HOST:
// private static final String DEFAULT_MAVEN_HOST = "https://storage.googleapis.com";
private static final String DEFAULT_MAVEN_HOST = "https://storage.flutter-io.cn";
在 build.grade 中替換 google() , mavenCentral() 為對(duì)應(yīng)的阿里云鏡像:
repositories {
// google()
// mavenCentral()
maven{ url 'https://maven.aliyun.com/repository/google' }
maven{ url 'https://maven.aliyun.com/repository/central' }
}
Android 真機(jī)設(shè)備連接
Android 真機(jī)運(yùn)行還可能遇到開發(fā)者模式的設(shè)置問題,正確連接設(shè)備后,注意檢查設(shè)備開發(fā)者選項(xiàng)設(shè)置:
- 開啟開發(fā)者選項(xiàng);
- 允許USB安裝;
- 允許USB調(diào)試;
解決以上問題后,Windows + Android 真機(jī)設(shè)備,成功運(yùn)行 Flutter 程序。