android studio配置flutter的几个问题

本文档详细介绍了在使用Flutter进行项目开发时遇到的多种构建问题及其解决方案,包括项目创建卡顿、Gradle配置、内存分配不足等问题,并提供了具体的修改步骤和参数调整建议。

1.参考
https://blog.youkuaiyun.com/qq_37199105/article/details/81007683

2.一直卡在creating flutter project
在Android Studio安装目录bin文件夹下找到 idea.properties在最后加入disable.android.first.run=true

3.一直卡在gradle

配置Android文件夹下build.gradle代码

    maven { url 'https://maven.aliyun.com/repository/google' }
    maven { url 'https://maven.aliyun.com/repository/jcenter' }
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }


如果还是一直通不过gardle,那就再修改flutter.gradle中代码(文件路径E:\androidflutter\flutter\packages\flutter_tools\gradle)

// google()
// jcenter()
maven{
url ‘https://maven.aliyun.com/repository/google’
}
maven{
url ‘https://maven.aliyun.com/repository/jcenter’
}
maven{
url ‘http://maven.aliyun.com/nexus/content/groups/public’
}

在这里插入图片描述
3.1 如果Runing错误日志为

Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on TNY AL00 in debug mode...
Running Gradle task 'assembleDebug'...

这表示仓库不安全,不被信任
可以在仓库中添加配置 allowInsecureProtocol = true

maven {
            allowInsecureProtocol = true
            url 'https://maven.aliyun.com/repository/google'
        }
        maven {
            allowInsecureProtocol = true
            url 'https://maven.aliyun.com/nexus/content/groups/public'
        }
        maven {
            allowInsecureProtocol = true
            url 'https://maven.aliyun.com/repository/jcenter'
        }
        maven {
            allowInsecureProtocol = true
            url 'https://download.flutter.io'
        }

4.内存不足无法分配虚拟器

部分错误内容:

Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at https://docs.gradle.org/4.10.2/userguide/gradle_daemon.html
Please read the following process output to find out more:

Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap

解决方式:新建环境变量:_JAVA_OPTIONS:-Xmx512M (我用AS2.3中设置阈值的方式在AS3.2设置无效,不知何故)
(关于Error occurred during initialization of VM Could not reserve enough space for 2097152KB object heap问题,其实就是内存分配不足,上边的方法是在实在没有办法时用的单独分配内存)

*Error running Gradle:
ProcessException: Process “G:\androidflutter_workspace\test_fu\android\gradlew.bat” exited abnormally:
Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details
FAILURE: Build failed with an exception.

  • What went wrong:
    Unable to start the daemon process.
    This problem might be caused by incorrect configuration of the daemon.
    For example, an unrecognized jvm option is used.
    Please refer to the user guide chapter on the daemon at https://docs.gradle.org/4.4/userguide/gradle_daemon.html
    Please read the following process output to find out more:

Error occurred during initialization of VM
Could not reserve enough space for 1572864KB object heap

  • 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
    Command: G:\androidflutter_workspace\test_fu\android\gradlew.bat app:properties
    Finished with error: Please review your Gradle project setup in the android/ folder.*

解决方法:
在Android目录下的gradle.properties文件中将1536改成512或你想分配的值,一定要小于这个值(1572864KB➗1024=1536 ),当时没去除一下,我也很无奈,如果报守护进程无法开启,就在用户目录下的.gradle目录下新建个gradle.properties文件在这里插入图片描述
并在项目中Android的gradle中添加 org.gradle.daemon=false
在这里插入图片描述
更多的仓库配置:

maven{ url = "https://maven.aliyun.com/repository/public" }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }

        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'http://download.flutter.io' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        // 配置HMS Core SDK的Maven仓地址。
        maven {url 'https://developer.huawei.com/repo/'}

在这里插入图片描述

<think>嗯,用户正在寻找解决Android Studio新建Flutter项目时闪退的问题。根据提供的引用内容,这个问题可能涉及多个方面,需要系统性地排查。 用户引用的资料里提到了几个关键点:引用[1]和[3]指出环境变量配置错误可能导致flutter doctor闪退,特别是PowerShell路径缺失的情况。引用[2]提到模拟器闪退可能由JSON解析错误引起,引用[4]强调Android Studio版本过低(需3.0+)是常见坑点,而引用[5]则提供了国内镜像修改方案。 考虑到用户明确提到"新建项目时闪退",应该重点排查三个方向:首先是开发环境基础配置,包括Flutter SDK路径、环境变量(尤其是PowerShell路径)和Android Studio版本;其次是项目级配置,检查gradle文件和镜像设置;最后是硬件兼容性,比如模拟器配置和GPU渲染模式。 用户可能已经尝试过基本操作(如flutter doctor),但忽略了版本兼容性这种深层因素。需要特别提醒国内用户注意网络问题,比如gradle下载失败导致的间接闪退。解决方案应该按优先级排序,从最基础的版本检查开始,逐步深入到文件修改层面,这样用户能阶梯式排查问题。 </think> 根据引用内容,Android Studio新建Flutter项目闪退的解决方案需综合以下步骤排查: ### 🔧 一、环境配置检查 1. **Android Studio版本验证** - 确保使用 **Android Studio 3.0 或更高版本**(引用[^4])。低版本会导致兼容性问题,建议升级至最新稳定版。 - 操作:`Help` > `Check for Updates` 或从[官网](https://developer.android.com/studio)下载 2. **Flutter SDK路径配置** - 删除原有冲突的Flutter SDK(引用[^1]),重新下载[官方版本](https://flutter.dev/docs/get-started/install) - 配置环境变量: ```bash # 用户变量 Path 新增 FLUTTER_HOME = C:\path\to\flutter\bin # 系统变量 Path 追加 %FLUTTER_HOME% ``` 3. **PowerShell路径修复(针对flutter doctor闪退)** - 按系统位数添加路径(引用[^3]): - 64位:`C:\Windows\SysWOW64\WindowsPowerShell\v1.0` - 32位:`C:\Windows\System32\WindowsPowerShell\v1.0` --- ### ⚙️ 二、项目级修复 1. **修改Gradle仓库镜像(国内用户必需)** - 文件:`flutter_sdk/packages/flutter_tools/gradle/flutter.gradle` - 替换代码(引用[^5]): ```gradle private static final String MAVEN_REPO = "https://storage.flutter-io.cn/download.flutter.io"; // 替换原URL ``` 2. **清理缓存与重建** ```bash flutter clean flutter pub get ``` --- ### 🖥️ 三、模拟器/设备问题 1. **Android模拟器闪退修复** - 若报错 `Caused by: java.lang.IllegalStateException...`(引用[^2]): - 关闭模拟器硬件加速:`AVD Manager` > 选择设备 > `Edit` > `Emulated Performance` > `Graphics` → 改为 `Software` - 或更新模拟器至最新版 2. **真机调试** - 优先使用真机测试:开启USB调试模式,执行 `flutter run` --- ### ✅ 验证步骤 1. 终端执行: ```bash flutter doctor ``` 确保无报错(特别是Android工具链项✅) 2. 重启Android Studio后新建Flutter项目 > **关键点总结**:80%的闪退由环境配置错误引起,重点检查SDK路径、AS版本及镜像配置。若问题仍存,查看 `idea.log`(路径:`Help` > `Show Log in Explorer`)定位具体错误[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值