美文网首页
Mac Android Studio编译Flutter项目

Mac Android Studio编译Flutter项目

作者: 落落2023 | 来源:发表于2021-01-28 10:51 被阅读0次

    Android Studio编译Flutter项目选择iOS模拟器运行ok,选择安卓模拟器编译运行的时比如这里我选择Huawei P30 Pro可能会出现一些问题。

    问题1:

    编译的时候可能会一直显示

    Launching lib/main.dart on Huawei P30 Pro in debug mode...
    Running Gradle task 'assembleDebug'...
    

    或者

    FAILURE: Build failed with an exception.
    
    * What went wrong:
    A problem occurred configuring root project 'android'.
    > Could not resolve all artifacts for configuration ':classpath'.
     > Could not resolve com.android.tools.build:gradle:3.5.0.
     Required by:
         project :
      > Could not resolve com.android.tools.build:gradle:3.5.0.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.5.0/gradle-3.5.0.pom'.
            > Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.5.0/gradle-3.5.0.pom'.
               > Connection reset
    
    * 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 10s
    Exception: Gradle task assembleDebug failed with exit code 1
    

    解决:
    需有效网络访问到Google服务器,如果不能满足就需要修改镜像为国内的,具体修改为两处:
    1.修改Flutter SDK 目录下的Flutter 打包配置文件flutter.gradle
    因我下载Flutter SDK是放在文稿目录下,所以flutter.gradle路径为

      /Users/cosmo/Documents/flutter/packages/flutter_tools/gradle/flutter.gradle
    
    图1-flutter.gradle 路径.png

    找到Flutter SDK 的Flutter 打包配置文件flutter.gradle ,可用Sublime Text打开编辑

    修改1:

    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.5.0'
        }
    }
    

    修改2:

    class FlutterPlugin implements Plugin<Project> {
        // 注释
        // private static final String DEFAULT_MAVEN_HOST = "https://storage.googleapis.com";
        //换用此行
        private static final String DEFAULT_MAVEN_HOST = "https://storage.flutter-io.cn";
    
        // The platforms that can be passed to the `--Ptarget-platform` flag.
        private static final String PLATFORM_ARM32  = "android-arm";
        private static final String PLATFORM_ARM64  = "android-arm64";
        private static final String PLATFORM_X86    = "android-x86";
        private static final String PLATFORM_X86_64 = "android-x64";
    
    

    修改3:

        void addFlutterDependencies(buildType) {
            String flutterBuildMode = buildModeFor(buildType)
            if (!supportsBuildMode(flutterBuildMode)) {
                return
            }
            String hostedRepository = System.env.FLUTTER_STORAGE_BASE_URL ?: DEFAULT_MAVEN_HOST
            String repository = useLocalEngine()
                ? project.property('local-engine-repo')
                : "$hostedRepository/download.flutter.io"
            project.rootProject.allprojects {
                repositories {
                    maven {
                        url repository
                    }
                    // 添加阿里镜像
                    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' }
                }
            }
    

    2.修改Flutter项目下的android工程目录下的build.gradle文件
    Android Studio打开你的Flutter项目工程,修改android目录下的build.gradle文件

    图2-Flutter示例her_app项目中build.gradle文件.png
    buildscript {
        ext.kotlin_version = '1.3.50'
        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.5.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    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
    }
    

    编译运行

    
    Launching lib/main.dart on Huawei P30 Pro in debug mode...
    Running Gradle task 'assembleDebug'...
    ✓ Built build/app/outputs/flutter-apk/app-debug.apk.
    Installing build/app/outputs/flutter-apk/app.apk...
    Waiting for Huawei P30 Pro to report its views...
    ...
    略之
    ...
    io.flutter.embedding.android.FlutterActivity.onCreate(android.os.Bundle) 
    I/OpenGLRenderer( 1977): Davey! duration=3542ms; Flags=1, IntendedVsync=446156254524, Vsync=449522921056, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=449523468479, AnimationStart=449523495773, PerformTraversalsStart=449523499548, DrawStart=449538335629, SyncQueued=449538955851, SyncStart=449575150045, IssueDrawCommandsStart=449575376245, SwapBuffers=449624850682, FrameCompleted=449735079179, DequeueBufferDuration=48265000, QueueBufferDuration=535000, 
    
    

    即ok

    问题2:

    Launching lib/main.dart on Huawei P30 Pro in debug mode...
    Running Gradle task 'assembleDebug'...
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
    > Failed to find Build Tools revision 28.0.3
    
    * 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 1m 1s
    Exception: Gradle task assembleDebug failed with exit code 1
    
    

    解决:

    因安装Android SDK时会自动帮我们安装一些必须的构建工具。根据报错信息

    Failed to find Build Tools revision 28.0.3

    Appearance & Behavior ->System Settings ->Anroid SDK -> SDK Tools 勾选下载Android SDK 对应需要的构建工具installed即可:

    图3-构建工具下载.png

    问题3:

    Launching lib/main.dart on Huawei P30 Pro in debug mode...
    Running Gradle task 'assembleDebug'...
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
    > Failed to find Platform SDK with path: platforms;android-29
    
    * 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 1m 10s
    Exception: Gradle task assembleDebug failed with exit code 1
    

    解决:

    根据报错信息
    Failed to find Platform SDK with path: platforms;android-29

    Appearance & Behavior ->System Settings -> Anroid SDK -> SDK Platforms 勾选下载Android SDK 对应缺失版本installed即可:

    图4-Android SDK 版本.png

    相关文章

      网友评论

          本文标题:Mac Android Studio编译Flutter项目

          本文链接:https://www.haomeiwen.com/subject/vzrttltx.html