Android studio新建Flutter项目 打开Andorid Studio ,会出现下面的界面,我们选择第二项,新建Flutter项目。 (如果你第一次新建项目,可能会有些慢,这个可能跟技术胖700元的电脑有关,土豪请无视这句话。) 打开第二个窗口后,选择第一个选项Flutter Application(flutter应用)。 这步完成后,系统就会自动为我们创建一个Flutter项目(新建项目的过程也是很慢的,它要去谷歌下载gradle,这个东西很容下载失败,如果失败,可以多反复试几次)。 如果你到了这一步,坚持住,马上成功了。 安装AVD虚拟机让Flutter跑起来虚拟机运行以后,可以点击debug按钮,让Flutter程序跑起来。如果你幸运的话,你的Flutter程序经过编译后,就会跑起来了。(这种幸运的机会很小,总会碰到一些小错误,我在这里介绍两个常见的错误)。 Error runing Gradle 错误解决(1.x版本已经修复)在Debug项目的时候,应该最常见的错误就是类似下面这样的错误了。 - Launching lib/main.dart on Android SDK built for x86 in debug mode...
- Initializing gradle...
- Resolving dependencies...
- * Error running Gradle:
- ProcessException: Process "/Users/rabbit/develop/android/flutter_app/android/gradlew" exited abnormally:
- Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.
- FAILURE: Build failed with an exception.
- * Where:
- Build file '/Users/rabbit/develop/android/flutter_app/android/app/build.gradle' line: 25
- * What went wrong:
- A problem occurred evaluating project ':app'.
- > Could not resolve all files for configuration 'classpath'.
- > Could not find lint-gradle-api.jar (com.android.tools.lint:lint-gradle-api:26.1.2).
- Searched in the following locations:
- https://jcenter.bintray.com/com/android/tools/lint/lint-gradle-api/26.1.2/lint-gradle-api-26.1.2.jar
- * 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 0s
- Command: /Users/rabbit/develop/android/flutter_app/android/gradlew app:properties
- Finished with error: Please review your Gradle project setup in the android/ folder.
这个问题的产生的原因,还是中国特有的问题,解决方案是改位阿里的链接(1.0已经修复了这个问题,不用再重新设置了)。 第一步:修改掉项目下的android目录下的build.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' }
全部代码: - buildscript {
- repositories {
- // 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'}
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:3.1.2'
- }
- }
- allprojects {
- repositories {
- // 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' }
- }
- }
- rootProject.buildDir = '../build'
- subprojects {
- project.buildDir = "${rootProject.buildDir}/${project.name}"
- }
- subprojects {
- project.evaluationDependsOn(':app')
- }
- task clean(type: Delete) {
- delete rootProject.buildDir
- }
注意是有两个部分进行了修改,不要只修改一处。 第二步:修改Flutter SDK包下的flutter.gradle文件,这个目录要根据你的SDK存放的位置有所变化。比如我放在了D盘Flutter目录下,那路径就是这个。 - D:\Flutter\flutter\packages\flutter_tools\gradle
打开文件进行修改,修改代码如下(其实也是换成阿里的路径就可以了)。 - buildscript {
- repositories {
- //jcenter()
- // maven {
- // url 'https://dl.google.com/dl/android/maven2'
- // }
- maven{
- url 'https://maven.aliyun.com/repository/jcenter'
- }
- maven{
- url 'http://maven.aliyun.com/nexus/content/groups/public'
- }
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:3.1.2'
- }
- }
然后再重新Debug一下,就基本可以启动起来。 还有一种错误是说硬件没有启动GPU,需要下载安装一个程序,这个提示的很清楚,不做过多的介绍了。 |