Android Studio(Windows)配合 Xiaomi11 Pro Flutter 真机测试,遇到了一些问题,记录一下问题和解决方法。
flutter doctor 命令
确认好 Flutter SDK 安装之后,首先在终端运行 flutter doctor 进行开发环境检查。
在终端使用 flutter doctor 命令检查后,依次按需解决了 X 的问题。每一项检查,我都添加了高亮的简要说明。我目前只需要在 Windows 上调试 Web 和 Android,上面的检测已经满足我的需求。添加说明的检查结果如下:
Doctor summary (to see all details, run flutter doctor -v):
// Flutter SDK 检查,具体配置可以参照Flutter 官网。
[√] Flutter (Channel stable, 3.7.10, on Microsoft Windows [版本 10.0.19045.2846], locale zh-CN)
// Windows 版本检查,查询得知是系统语言问题,不影响编码、调试,无视它。
[X] Windows Version (Unable to confirm if installed Windows version is 10 or greater)
// Andriod 设备支持检查。有具体细项错误,按照提示解决。
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
// Chrome 支持检查。一开始配置好了就是 OK 的,我一开始也是用 Chrome 查看运行效果的。
[√] Chrome - develop for the web
// Windows Phone 设备支持,不需要调试 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)
// 可用设备检查。4部可用设备,对应到 Android Studio 调试设备选择。
[√] Connected device (4 available)
// 很常见的网络问题,我也没管它。
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: 信号灯超时时间已到
X HTTP host "https://cloud.google.com/" is not reachable. Reason: An error occurred while checking the HTTP host: 信号灯超时时间已到
! Doctor found issues in 3 categories.
Could not resolve all files for configuration ':classpath'
选择 Web 调试很简单,只需安装好 Flutter SDK,并使用 Android Studio 就可以实现。但是,当选择真机调试后,控制台打印了一个问题,问题的关键在第一句: Could not resolve all files for configuration ':classpath' 。
经过搜索类似问题,找到解决方案:降低 JDK 版本。我本来安装的是 JDK 20,因为很多解决方法时间久远,解决问题的操作是 JDK 13 downgrade to JDK 8” 之类的操作,所以我决定先降低 JDK 一个版本,Oracle官网最方便下载的可用的低版本是 JDK 17, 所以切换到了 JDK 17,在重新配置并重启后,这个问题得到了解决。
报错信息如下:
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')
运行一直卡在 Runing Gradle task‘assembleDebug'...
出现这个问题是因为 Gradle 默认配置中依赖引用被墙导致的。
在 FlutterSDK 安装目录 flutter\packages\flutter_tools\gradle 中 flutter.gradle 文件中,以及项目生成时,andriod 文件夹下 gradle 文件目录中 build.grade 文件中,默认配置的 repositories 和 dependencies 依赖引用无法访问,需要替换可访问的镜像路径。
只要是 Gradle 涉及到依赖库和依赖包引用的地方,都进行引用链接的修改,即可解决问题。
在 flutter.gradle 中,替换 google() , mavenCentral() 为对应的阿里云镜像:
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() 为对应的阿里云镜像:
repositories {
// google()
// mavenCentral()
maven{ url 'https://maven.aliyun.com/repository/google' }
maven{ url 'https://maven.aliyun.com/repository/central' }
}
Android 真机设备连接
Android 真机运行还可能遇到开发者模式的设置问题,正确连接设备后,注意检查设备开发者选项设置:
- 开启开发者选项;
- 允许USB安装;
- 允许USB调试;
解决以上问题后,Windows + Android 真机设备,成功运行 Flutter 程序。
网友评论