美文网首页Flutter圈子
Flutter-Android趟坑笔记

Flutter-Android趟坑笔记

作者: 念_夕 | 来源:发表于2019-03-07 16:30 被阅读0次

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原理与实践

相关文章

  • Flutter-Android趟坑笔记

    VM snapshot must be valid. 解决方式,在 flutter module 执行 flutt...

  • HEXO 趟坑笔记

    我的blog 如何使用Hexo在github上搭建静态博客 如何使用Hexo在github上搭建静态博客 感冒两天...

  • kafka开发笔记

    最近又要用上kafka,发现原来趟过的坑又趟过了一次在这里做一下笔记,提醒以后注意: 如果连不上brokers,c...

  • 趟坑记录

    目录 Replugin的坑 相机权限 aar打包的坑 1.Replugin的坑 使用setSystemUiVisi...

  • 趟坑记录

    1.侥幸心里150滨安路,突然出错220必拿下的,果断拒绝1108少90片连续,看准碰下黑八成转折点非32,没商量...

  • 趟坑记录

    1、怎么做到不在打开当前activity之前清空activity栈的情况下,将数据通过setResult传递给前一...

  • FENICS趟坑

    一直想研究一下FENICS,最近因为要给师兄介绍了一下FENICS的使用方法,将FENICS求解2D Poisso...

  • mpvue 趟坑之旅

    坑~ 1、 class 和 style 绑定的问题:按正常的习惯 ‘:’ 左侧为布尔值,但是 mpvue 中是反过...

  • nginx django 趟坑

    直接用uwsgi 测试django uwsgi --http :8000 --module mysite.wsgi 水痕

  • 趟坑记录-FileProvider

    FileProvider 当使用FileProvider生成uri时,使用uri的intent应添加 来授予权限。...

网友评论

    本文标题:Flutter-Android趟坑笔记

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