Flutter 模块 aar依赖依赖实践

以下模块均基于flutter 1.9.1版本进行讨论,更高版本可能不适用

一、我们面临了什么?

目前,我们团队有六个人,真正参与Flutter开发仅少数同学。

项目集成Flutter采用工程依赖,导致团队每一个人都需要安装flutter环境(对新人不友好),并且flutter项目夹杂在android工程里,导致git经常会出现修改提示(很容易导致误删)。对于flutter的更新非参与人员无感知(其他成员的老包拿不到flutter的最新代码)

正因为如此,我们借助这次机会让flutter模块由原来的工程依赖变为aar依赖,顺便开一个组件化的头。

二、对于方案的选择?

1.Flutter集成方案:

目前市面上flutter集成到项目里有两种方案:

官方解决方案:就是将flutter作为module让原生native工程直接依赖 (当前方案,再赘述)

闲鱼flutter集成方案:将flutter项目打包成aar上传至maven,原生native工程通过maven引用
优点:
1.不开发flutter的同学无需安装环境与工程。
2.flutter更新,所有同学都可以看到新页面。
3.更有利于组件化开展
缺点:
1.调试包与正式包都需要单独编译,测试较为麻烦
2.上线前需要主动更新版本号。

2、Flutter测试编译方案

(1)本地maven库依赖编译

将flutter工程打出的aar包,放在本地maven库。原生工程本地maven依赖,找到aar并集成到主工程去

(2)文件传递编译

将flutter工程打出的aar包,通过脚本移动到原生工程,把aar通过git方式添加到项目中去。工程依赖lib文件夹下的aar

(3)远程maven库依赖编译

将本地aar放到远端maven仓库,本地通过gradle方式引用。

最终,我们选择线上环境非开发人员采用(3)方式依赖。测试与开发采用(1)方式依赖。(2)方式由于aar包过于大(50m左右),对于git下载不利,所以就放弃了。

三、需要做哪些步骤?

第一部分:flutter部分准备

1.迁移flutter工程位置,独立出flutter工程

2.配置module报名,撰写相关打包脚本

(1)本地依赖脚本

(2)远端maven库上传脚本

第二部分:配置原生端相关依赖

1.创建flutterComponent组件

2.配置本地模块依赖

3.配置远端模块依赖

第三部分:maven库相关配置(本文略)

四、具体工作

第一部分:flutter配置

1.将flutter工程从原有的工程文件夹中移除,具体位置与Android项目平齐,图示如下


文件摆放.png

2.配置包名(主要是以前太随意了)
//在yaml文件下
module: androidPackage: cn.yemanlin.flutter

3.撰写python脚本(略)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys

__build_name = "-type"
__version_name = "-version"
__maven_name = "-maven"

__maven_config = "apply plugin: 'maven-publish'\n" \
                 "apply plugin: 'maven'\n" \
                 "uploadArchives [\n" \
                 "repositories [\n" \
                 "mavenDeployer [\n" \
                 "repository(url: {mavenReleasesUrl}) [\n" \
                 "authentication(userName: {maven_local_username}, password: {maven_local_password})\n" \
                 "]\n" \
                 "snapshotRepository(url: {mavenSnapshotUrl}) [\n" \
                 "authentication(userName: {maven_local_username}, password: {maven_local_password})\n" \
                 "]\n" \
                 "pom.project [\n" \
                 "version {version}\n" \
                 "artifactId {maven_pom_artifactId}\n" \
                 "groupId {maven_pom_groupId}\n" \
                 "packaging {maven_pom_packaging}\n" \
                 "description {maven_pom_description} \n" \
                 "]]]]\n"


def start(args):
    build_type = ""
    version = ""
    maven = ""
    index = 1
    while index < len(args):
        if args[index] == __build_name:
            index += 1
            build_type = args[index]
        elif args[index] == __version_name:
            index += 1
            version = args[index]
        elif args[index] == __maven_name:
            index += 1
            maven = args[index]
        else:
            index += 1
    if build_type == "" and version == "" and maven == "":
        print('格式错误,应参照:\n'
              ' 例1:build_android_auto.py -type release -version 0.0.1 -maven snapshot\n'
              ' 例2:build_android_auto.py -type debug\n')
        exit(0)
    assert build_type == "release" or build_type == "debug"
    assert maven == "" or ((maven == "release" or maven == "snapshot") and version != "")
    print(f'your args: buildType: {build_type}  version:{version} mavenType:{maven}')

    need_upload = (maven == "release" or maven == "snapshot") and version != ""

    # 清理工作
    if need_upload:
        out = os.system('git stash')
        out = os.system('flutter clean')
        out = os.system('git reset --hard')
        out = os.system('git stash apply')

    #构建开始
    build(build_type)
    if need_upload:
        # 上传maven库
        upload(version, maven)
    return


def build(build_type="debug"):
    print(f"start build {build_type}")
    if build_type == "debug":
        out = os.system('flutter build aar --debug')
        print(out)
    elif build_type == "release":
        out = os.system('flutter build aar --release')
        print(out)
    return


def upload(version, maven_type):
    print(f"start config {version} {maven_type}")

    if maven_type == "snapshot":
        full_version = f'"{version}-SNAPSHOT"'
    elif maven_type == "release":
        full_version = f'"{version}-release"'
    else:
        raise Exception("unknown maven type!")

    config = __maven_config.format(
        mavenReleasesUrl='"  自己填写 "',
        mavenSnapshotUrl='" 自己填写 "',
        maven_local_username='"  自己填写  "',
        maven_local_password='" 自己填写 "',
        version=full_version,
        maven_pom_artifactId='" 自己填写 "',
        maven_pom_groupId='" 自己填写 "',
        maven_pom_packaging='"aar"',
        maven_pom_description='"description"').replace("[", "{").replace("]", "}")

    modify_file_append(".android/Flutter/build.gradle", "apply from", config)

    print("start build .")

    os.chdir("./.android")
    out = os.system("./gradlew assembleRelease")
    print(out)

    print("start upload .")

    out = os.system("./gradlew uploadArchives")
    print(out)

    # 清理工作
    out = os.system('git reset --hard')
    print(out)
    out = os.system('git stash apply')
    print(out)
    print("\ndone.")
    return


def modify_file_append(file_path, start, content):
    with open(file_path, "r") as f:
        lines = f.readlines()
        f.close()
    with open(file_path, 'w') as f:
        for line in lines:
            f.write(line)
            if line.startswith(start):
                f.write(content)
        f.close()
    return


def modify_file(file_path, prefix, content):
    with open(file_path, "r") as f:
        lines = f.readlines()
        f.close()
    with open(file_path, 'w') as f:
        for line in lines:
            if line.startswith(prefix):
                line = content
            f.write(line)
        f.close()
    return


if __name__ == "__main__":
    start(sys.argv)

4.使用
哈哈哈哈,脚本里有,自己看吧。

第二部分:Android原生配置

1.在setting.gradle中删除下面两句话(解除以前的工程依赖)
setBinding(new Binding([gradle: this]))
evaluate(new File( settingsDir.parentFile,'flutter/yemanlin/.android/include_flutter.groovy' ))

2.创建一个新的组件(详情见另一篇文章)
//1.在项目build.gradle下进行配置

allprojects{
  repositories {       
  //flutter嵌入层         
  maven { url 'http://download.flutter.io' }
  //本地flutter maven库(这个很关键,也就是为什么要把项目放在与工程平齐的地方)
  maven { url '../../flutter/yemanlin/build/host/outputs/repo' }
  }
}

//2.在app的build.gradle下进行配置

repositories {
    flatDir {
        dirs project(':flutterComponent').file('libs')  //flutterComponent就是组件名字     
    } 
}

//3.在我们flutter的组件里进行配置

dependencies {    
   //flutter 打包后,内部不会带aar包,所以flutter依赖的aar包需要手动迁移到本地Lib文件夹
   implementation files('libs/flutter_drag_scale-release.aar') 
   .......     
   //方式一:远端依赖,把相关aar要写到这里     
   api ("cn.yemanlin.flutter:flutter_release:1.0"){
     exclude module: 'sqflite'
   }     
  //方式二:本地依赖,需要先用flutter工程进行本地编译     
  api ("cn.yemanlin.flutter.test:flutter_debug:1.0") 
}

本期分享结束

你可能感兴趣的:(Flutter 模块 aar依赖依赖实践)