Flutter-Android趟坑笔记

VM snapshot must be valid.

解决方式,在 flutter module 执行 flutter clean,然后 flutter run,就行了

  1. Import flutter module to your HOST app(set binding, implementation, and instantiate view/fragment)
  2. When you clean/rebuild from HOST app, flutter module does not generate properly
  3. You need to open your flutter module in a separate AS
  4. flutter clean
  5. cd .android
  6. ./gradlew clean
  7. ./gradlew assembleDebug
  8. Return to host app and run app(do not sync or rebuild) :)
    https://github.com/flutter/flutter/issues/24268

send image form android to flutter by channel

You can't pass a raw bitmap to Image.memory because it doesn't contain enough information, notably width and height. The Image constructors need a recognized file format: JPEG, PNG, GIF, Animated GIF, WebP, Animated WebP, BMP, and WBMP.

直接把bitmap传给flutter是不行的,那边没有足够的信息来decode,所以需要拼接
(stackoverflow)[https://stackoverflow.com/questions/51516310/sending-bitmap-to-flutter-from-android-platform]

so库兼容问题

        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
        }

在上述情况下,libflutter.so库默认只会打包进armeabi-v7a的包中,导致其余两种ABI架构的手机崩溃,如果只有armeabi-v7a包的情况下,arm64-v8a可以向下兼容,无特殊需求的情况下,可以将上述配置改为如下,快速规避问题

        ndk {
            abiFilters 'armeabi-v7a'
       }
通过修改flutter框架,规避解决so库问题

修改 packages/flutter_tools/gradle/flutter.gradle,给缺少libflutter.so的架构加上libflutter.so,步骤如下:
在flutter.gradle中加入如下方法:

//==========================================arm64-v8 & armeabi支持=====================================================
    private void initv8FlutterJar(Project project) {
        String targetArch = 'arm64'
        Path baseEnginePath = Paths.get(flutterRoot.absolutePath, "bin", "cache", "artifacts", "engine")
        debugFlutterJar_V8 = baseEnginePath.resolve("android-${targetArch}").resolve("flutter.jar").toFile()
        profileFlutterJar_V8 = baseEnginePath.resolve("android-${targetArch}-profile").resolve("flutter.jar").toFile()
        releaseFlutterJar_V8 = baseEnginePath.resolve("android-${targetArch}-release").resolve("flutter.jar").toFile()
        dynamicProfileFlutterJar_V8 = baseEnginePath.resolve("android-${targetArch}-dynamic-profile").resolve("flutter.jar").toFile()
        dynamicReleaseFlutterJar_V8 = baseEnginePath.resolve("android-${targetArch}-dynamic-release").resolve("flutter.jar").toFile()
    }

    private File v8flutterJarFor(buildMode) {
        if (buildMode == "profile") {
            return profileFlutterJar_V8
        } else if (buildMode == "dynamicProfile") {
            return dynamicProfileFlutterJar_V8
        } else if (buildMode == "dynamicRelease") {
            return dynamicReleaseFlutterJar_V8
        } else if (buildMode == "debug") {
            return debugFlutterJar_V8
        }
        return releaseFlutterJar_V8
    }

    private void addv8FlutterArmeabiJarApiDependency(Project project, def buildType) {
        File chosenFlutterJar = v8flutterJarFor(buildModeFor(buildType))
        File flutterARM64Jar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/arm64/${buildType.name}/flutter-arm64.jar")
        Task flutterARM64JarTask = project.tasks.create("${flutterBuildPrefix}${buildType.name.capitalize()}ARM64V8Jar", Jar) {
            destinationDir flutterARM64Jar.parentFile
            archiveName flutterARM64Jar.name
            from(project.zipTree(chosenFlutterJar).matching{
                include "lib/arm64-v8a/libflutter.so"
            }) {
                eachFile {def fcp ->
                    if (fcp.relativePath.pathString.contains("lib/arm64-v8a/libflutter.so")) {
                        fcp.path = "lib/arm64-v8a/libflutter.so"
                    } else {
                        fcp.exclude()
                    }
                }
            }
    
        }
        
        if (flutterARM64JarTask == null) {
            return
        }

        Task javacTask = project.tasks.findByName("compile${buildType.name.capitalize()}JavaWithJavac")
        if (javacTask) {
            javacTask.dependsOn flutterARM64JarTask
        }
        Task kotlinTask = project.tasks.findByName("compile${buildType.name.capitalize()}Kotlin")
        if (kotlinTask) {
            kotlinTask.dependsOn flutterARM64JarTask
        }

        project.dependencies {
            String configuration;
            if (project.getConfigurations().findByName("api")) {
                configuration = buildType.name + "Api";
            } else {
                configuration = buildType.name + "Compile";
            }
            add(configuration, project.files {
                flutterARM64JarTask
            })
        }
    }
//==========================================armeabi支持=====================================================
        /**
    * create armeabi task
    */
    private Task createArmeabiTask(Project project, String taskName, File flutterJar, File flutterArmV7Jar) {
        def flutterArmeabiJarTask = project.tasks.create(taskName, Jar) {
            destinationDir flutterArmV7Jar.parentFile
            archiveName flutterArmV7Jar.name
            from(project.zipTree(flutterJar).matching {
                //仅包含armeabi-v7a的so
                include "lib/armeabi-v7a/libflutter.so"
            }) {
                eachFile {def fcp ->
                    if (fcp.relativePath.pathString.contains("lib/armeabi-v7a/libflutter.so")) {
                        //重命名路径,否则会拷贝到v7a目录
                        fcp.path = "lib/armeabi/libflutter.so"
                    } else {
                        //非so排除
                        fcp.exclude()
                    }
                }
            }
        }
        return flutterArmeabiJarTask
    }

    /**
    * add armeabi so for prebuilt engine
    */
    private void addFlutterArmeabiJarApiDependency(Project project, def buildType, String targetArch) {
        def flutterArmeabiJarTask = null
        if (targetArch == 'arm') {
            File flutterJar = flutterJarFor(buildModeFor(buildType))
            File flutterArmV7Jar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/armeabi/${buildType.name}/flutter-armeabi.jar")
            flutterArmeabiJarTask = createArmeabiTask(project, "${flutterBuildPrefix}${buildType.name.capitalize()}ArmeabiJar", flutterJar, flutterArmV7Jar)
        }
        if (flutterArmeabiJarTask == null) {
            return
        }

        Task javacTask = project.tasks.findByName("compile${buildType.name.capitalize()}JavaWithJavac")
        if (javacTask) {
            javacTask.dependsOn flutterArmeabiJarTask
        }
        Task kotlinTask = project.tasks.findByName("compile${buildType.name.capitalize()}Kotlin")
        if (kotlinTask) {
            kotlinTask.dependsOn flutterArmeabiJarTask
        }

        project.dependencies {
            String configuration;
            if (project.getConfigurations().findByName("api")) {
                configuration = buildType.name + "Api";
            } else {
                configuration = buildType.name + "Compile";
            }
            add(configuration, project.files {
                flutterArmeabiJarTask
            })
        }
    }
    //==========================================arm64-v8 & armeabi支持=====================================================
    /**

在如下位置加上对应代码

            project.android.buildTypes.each { 
                addFlutterJarApiDependency(project, it, flutterX86JarTask) 
                //==========================================arm64-v8 & armeabi支持=====================================================
                addFlutterArmeabiJarApiDependency(project, it, targetArch)
                addv8FlutterArmeabiJarApiDependency(project, it)
                //==========================================arm64-v8 & armeabi支持=====================================================
            }
            project.android.buildTypes.whenObjectAdded { 
                addFlutterJarApiDependency(project, it, flutterX86JarTask) 
                //==========================================arm64-v8 & armeabi支持=====================================================
                addFlutterArmeabiJarApiDependency(project, it, targetArch)
                addv8FlutterArmeabiJarApiDependency(project, it)
                //==========================================arm64-v8 & armeabi支持=====================================================
            }

参考链接

flutter-engin指北
Flutter原理与实践

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,042评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,996评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,674评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,340评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,404评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,749评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,902评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,662评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,110评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,451评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,577评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,258评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,848评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,726评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,952评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,271评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,452评论 2 348

推荐阅读更多精彩内容