android现有项目集成flutter

作者: JasmineBen | 来源:发表于2019-03-19 14:35 被阅读12次

随着flutter技术的发展,越来越多的开发者开始选择用Flutter开发一些功能,基于现有的项目,通常有两种方式引入Flutter:
1、为项目创建一个flutter module,通过library方式引入flutter功能。
2、创建一个独立的flutter app,改造成module,然后将module打包成aar让主工程引用。
第一种方式是官方推荐的引用方式,通过官方文档 Add-Flutter-to-existing-apps 即可了解,这里主要讲解第二种形式。
第二种方式首先需要搭建maven远程仓库,这里就先假设已经搭建好了maven仓库。下面首先需要创建一个Flutter App工程并修改为module。
1、创建Flutter Application

flutter application.png
2、修改打包方式为library
image.png
3、上传aar到maven仓库
uploadArchives {
    configuration = configurations.archives
    repositories {
        mavenDeployer {
            android.libraryVariants.all { variant ->
                def _flavorBuildTypeName = "release"
                addFilter(_flavorBuildTypeName) { artifact, file ->
                    true
                }
                repository(url: "maven仓库地址") {
                    authentication(userName: maven上传用户名, password: maven上传密码)
                }
                pom(_flavorBuildTypeName).groupId = "xxx"
                pom(_flavorBuildTypeName).artifactId = "xxx"
                pom(_flavorBuildTypeName).version = "xxx"
                pom(_flavorBuildTypeName).name = 'xxx'
                pom(_flavorBuildTypeName).packaging = 'aar'
                pom(_flavorBuildTypeName).withXml {
                    def root = asNode()
                    def depsNode = root["dependencies"][0] ?: root.appendNode("dependencies")
                    def addDep = {
                        if (it.group == null) return // Avoid empty dependency nodes
                        def dependencyNode = depsNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        if (it.hasProperty('optional') && it.optional) {
                            dependencyNode.appendNode('optional', 'true')
                        }
                        //生成依赖包的mvn 部署
                        def dir = file('../../build/')
                        def repoUrl = "maven仓库地址"
                        dir.eachFileRecurse { file ->
                            if (file.name.contains(it.name) && file.name.endsWith("-release.aar")) {
                                def command = "cmd /k mvn deploy:deploy-file"
                                        .concat(" -DgroupId=${it.group}")
                                        .concat(" -DartifactId=${it.name}")
                                        .concat(" -Dversion=${it.version}")
                                        .concat(" -Dpackaging=aar")
                                        .concat(" -Dfile=${file.path}")
                                        .concat(" -Durl=${repoUrl}")
                                        .concat(" -DgeneratePom=true")
                                //将该flutter module依赖的其余的library上传到maven
                                Process p = Runtime.getRuntime().exec(command)
                            }
                        }
                    }
                    configurations.api.allDependencies.each addDep
                    configurations.implementation.allDependencies.each addDep
                }
            }
        }
    }

4、第3步中mvn deploy需要在本地安装maven并配置用户名密码
4.1、安装maven
4.2、配置用户名密码

<server>
<id>remote-repository</id>
<username>xxx</username>
<password>xxx</password>
</server>

5、设置Flutter SDK的目录
进入android文件夹下设置local.properties
设置flutter.sdk=xxx 为当前机器本地的flutter目录

6、拷贝icudatl.dat到对应目录


icudatl.png

7、拷贝 ${flutter_sdk}/packages/flutter_tools/templates/module/android/library/Flutter.tmpl/src/main/java/io/flutter/facade到目录


image.png

8、windows下dos窗口执行 gradle clean uploadArchives --info(当然要在mac下运行,修改build.gradle脚本,不会的可以评论区留言)

9、最后,可以在主工程中依赖该aar

相关文章

网友评论

    本文标题:android现有项目集成flutter

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