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

你可能感兴趣的:(Flutter-Android趟坑笔记)