Android Studio (Narwhal 3) 遇到的问题及解决方法

Android Studio (Narwhal 3) 遇到的问题及解决方法

以下是我在创建第一个手机端app时遇到的一些问题,及其解决方法,希望对你有帮助。

一、问题描述:在Android studio启用模拟机时遇到“•Turn off Hyper-V:Android Emulator is in***patible with Hyper-V.”


原因:Android模拟器Hyper-V兼容不了。
解决步骤:
1.打开命令提示符或PowerShell
按 Win + R,输入 cmd 或 powershell,然后按回车
2.导航到Android SDK的emulator目录
cmd
cd %LOCALAPPDATA%\Android\Sdk\emulator
3.查看可用的模拟器列表
cmd
emulator -list-avds
4.使用参数启动模拟器
cmd
emulator -avd 你的模拟器名称 -feature WindowsHypervisorPlatform

================ ================================


二、问题描述:The named parameter 'backgroundColor' isn't defined.


原因:在新版本的 Flutter 中,backgroundColor 参数已经被替换。
解决步骤:
backgroundColor: Colors.blue[700], 
修改为
primary: Colors.blue[700],

================ ================================


三、问题描述:widget_test.dart里面出现The constructor being called isn't a const constructor.


原因:在测试中试图使用 const 构造函数,但你的 widget 构造函数不是 const 构造函数。
解决步骤:
直接去掉const

================ ================================


四、问题描述:Flutter assets will be downloaded from https://storage.flutter-io.***. Make sure you trust this source!
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
Exception in thread "main" java.lang.RuntimeException: Timeout of 120000 reached waiting for exclusive a***ess to file: C:\Users\...\.gradle\wrapper\dists\gradle-8.4-all\56r6xik2f6skrm47et0ibifug\gradle-8.4-all.zip
    at org.gradle.wrapper.ExclusiveFileA***essManager.a***ess(ExclusiveFileA***essManager.java:61)
    at org.gradle.wrapper.Install.createDist(Install.java:48)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Retrying Gradle Build: #1, wait time: 100ms
[!] Gradle threw an error while downloading artifacts from the ***work.
Running Gradle task 'assembleDebug'...


原因:由于网络连接或文件锁定导致的Gradle 下载超时。
解决步骤:使用国内镜像源
distributionUrl=https\://mirrors.cloud.tencent.***/gradle/gradle-7.x.x-all.zip


================ ================================


五、问题描述:FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\...\Desktop\book_market\android\build.gradle' line: 26

* What went wrong:
A problem o***urred evaluating root project 'android'.
> A problem o***urred configuring project ':app'.
   > Could not create an instance of type ***.android.build.api.variant.impl.ApplicationVariantImpl.
      > Namespace not specified. Specify a namespace in the module's build file: C:\Users\...\Desktop\book_market\android\app\build.gradle. See https://d.android.***/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

        If you've specified the package attribute in the source AndroidManifest.xml, you can
 use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to
 https://d.android.***/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

* 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.

Deprecated Gradle features were used in this build, making it in***patible with Gradle 9.0. 

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they ***e from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.9/userguide/***mand_line_interface.html#sec:***mand_line_warnings in the Gradle documentation.

BUILD FAILED in 21s


原因:在 Android Gradle Plugin (AGP) 8.0+ 版本中,需要在 android/app/build.gradle 文件中明确指定 namespace。
解决步骤:
1.打开 android/app/build.gradle 文件,在 android 块中添加 namespace,
android {
    namespace "换为你项目的真实包名"  // 添加这一行
    // ... 其他现有配置
    }
2.包名来源
在 android/app/src/main/AndroidManifest.xml 中查找,
<manifest xmlns:android="http://schemas.android.***/apk/res/android"
    package="***.example. ...">  <!-- 这里就是您的包名 -->

================ ================================


六、问题描述:FAILURE: Build failed with an exception.

* Where:                                                                                   
Script 'D:\Fultter download\flutter_windows_3.0.4-stable\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 917

* What went wrong:
A problem o***urred evaluating root project 'android'.
> A problem o***urred configuring project ':app'.
   > Could not create task ':app:packLibsflutterBuildDebug'.
      > No signature of method: org.gradle.api.tasks.bundling.Jar.destinationDir() is appli
cable for argument types: (File) values: [C:\Users\...\Desktop\book_market\build\app\intermediates\flutter\debug]

* 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 58s
Running Gradle task 'assembleDebug'...                             59.4s
Exception: Gradle task assembleDebug failed with exit code 1


原因:Flutter 3.0.4 的 Gradle 插件与当前项目里 Gradle 版本不兼容
解决步骤:
1. 改 Gradle 版本,把 Gradle 降级到 7.3.3
打开文件:android/gradle/wrapper/gradle-wrapper.properties
修改为:distributionUrl=https\://mirrors.cloud.tencent.***/distributions/gradle-7.3.3-all.zip
2. 改 Android Gradle Plugin 版本
打开文件:android/build.gradle
把 dependencies 里的修改为classpath '***.android.tools.build:gradle:7.1.3'

【7.1.3 与 Gradle 7.3.3 对应,Flutter 3.0.4 官方 CI 验证过】

3.保存 → 重启 Android Studio → 重新 flutter run

================ ================================


七、清理和重建项目的指令:


1.清理:flutter clean
2.删除Gradle缓存(Windows):rmdir /s %USERPROFILE%\.gradle\caches
3.重新获取依赖:flutter pub get
4.重新运行:flutter run

================ ================================

项目可运行配置快照


Flutter SDK 版本:3.0.4
Dart SDK 版本:2.17.5
Gradle 版本:7.3.3
Android Gradle Plugin 版本:7.1.3

转载请说明出处内容投诉
CSS教程网 » Android Studio (Narwhal 3) 遇到的问题及解决方法

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买