前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Win10在Android Studio中配置flutter的坑

Win10在Android Studio中配置flutter的坑

作者头像
Tom2Code
发布2023-02-14 11:27:43
2.2K0
发布2023-02-14 11:27:43
举报
文章被收录于专栏:Tom

1.首先安装flutter sdk,这个去官网安装即可。

2.然后安装Andorid Studio,都是一键安装的即可。

然后打开android studio(简称AS)新建一个flutter项目,然后AS会选择gradle构建项目,下载一些必要的package,这时候就开始有很多坑了。

第一个坑:

由于没有改镜像地址,gradle默认从maven的国外源站开始下载,所以会导致一直timeout,一直timeout,所以需要修改配置文件:

这是位于

代码语言:javascript
复制
D:\flutter\flutter_windows_3.3.9-stable\flutter\packages\flutter_tools\gradle

下的配置文件,叫做flutter.gradle

打开之后是这个样子:

大概是一千行代码

我们要改的地方是这里:

代码语言:javascript
复制
/** 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行的下面新增一些地址:

代码语言:javascript
复制
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'}

这也就是说增加了一些源站点,我以为加了这三条就好了,没想到构建了半天还是会报错:

代码语言:javascript
复制
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

发现还是需要加上一些额外的配置:

代码语言:javascript
复制
maven {
            allowInsecureProtocol = true
            url 'xxx'
}

最终的配置是这样的(截取了部分):

代码语言:javascript
复制
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

当然还有其他各种小问题,但是这个是最致命也是最耗时的。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Tom的小院 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档