1.首先安装flutter sdk,这个去官网安装即可。
2.然后安装Andorid Studio,都是一键安装的即可。
然后打开android studio(简称AS)新建一个flutter项目,然后AS会选择gradle构建项目,下载一些必要的package,这时候就开始有很多坑了。
第一个坑:
由于没有改镜像地址,gradle默认从maven的国外源站开始下载,所以会导致一直timeout,一直timeout,所以需要修改配置文件:
这是位于
D:\flutter\flutter_windows_3.3.9-stable\flutter\packages\flutter_tools\gradle
下的配置文件,叫做flutter.gradle
打开之后是这个样子:
大概是一千行代码
我们要改的地方是这里:
/** For apps only. Provides the flutter extension used in app/build.gradle. */
class FlutterExtension {
/** Sets the compileSdkVersion used by default in Flutter app projects. */
static int compileSdkVersion = 31
/** Sets the minSdkVersion used by default in Flutter app projects. */
static int minSdkVersion = 16
/** Sets the targetSdkVersion used by default in Flutter app projects. */
static int targetSdkVersion = 31
/**
* Sets the ndkVersion used by default in Flutter app projects.
* Chosen as default version of the AGP version below.
*/
static String ndkVersion = "21.1.6352462"
/**
* Specifies the relative directory to the Flutter project directory.
* In an app project, this is ../.. since the app's build.gradle is under android/app.
*/
String source
/** Allows to override the target file. Otherwise, the target is lib/main.dart. */
String target
}
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
/* When bumping, also update ndkVersion above. */
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
这个是默认的原配置文件的内容
那么我们要在第31行的下面新增一些地址:
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'}
这也就是说增加了一些源站点,我以为加了这三条就好了,没想到构建了半天还是会报错:
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(XXX)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols.
于是又是一顿debug
发现还是需要加上一些额外的配置:
maven {
allowInsecureProtocol = true
url 'xxx'
}
最终的配置是这样的(截取了部分):
buildscript {
repositories {
//google()
//mavenCentral()
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/nexus/content/groups/public'}
}
dependencies {
/* When bumping, also update ndkVersion above. */
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
=====
// Configure the Maven repository.
String hostedRepository = System.env.FLUTTER_STORAGE_BASE_URL ?: DEFAULT_MAVEN_HOST
String repository = useLocalEngine()
? project.property('local-engine-repo')
: "$hostedRepository/download.flutter.io"
rootProject.allprojects {
repositories {
maven {
url repository
allowInsecureProtocol = true
}
}
}
配置完了这些之后别忘了重启AS
当然还有其他各种小问题,但是这个是最致命也是最耗时的。