談一談Android Flutter混合開發問題

 

之前介紹過一些Android Flutter混合開發的注意點以及一些基本知識, 接下來介紹一下實際開發過程中,目前一定會遇到的兩個坑。
在Flutter開發過程中,如果是以module的形式進行依賴,是無法進行android原生部分的調試的,無法進行attach debug to android process操作; 爲了解決這一問題,我們需要考慮使用aar的方式,以library的形式進行依賴,但是講flutter打包成library的時候,如果使用第三方框架,會導致第三方框架無法打包進入aar,因此我們需要來解決這一問題。

混合開發的依賴方式

打包成獨立aar

打包aar很簡單,進入flutter工程,然後進入.android目錄,輸入指令: 打debug的aar包:

./gradlew flutter:assembleDebug

打debug的release包:

./gradlew flutter:assembleRelease

打好的包在 flutter module/.android/Flutter/build/outputs/aar

aar的包打好後可以直接複製到Android工程去依賴即可。
但是如果使用第三方框架會導致第三方框架的無法打入aar,我們可以使用fat-aar-android來解決這一問題:
在flutter工程的build.gradle加入:

    classpath 'com.kezong:fat-aar:1.2.7'

在Flutter的build.gradle加入如下配置:

apply plugin: 'com.kezong.fat-aar'

dependencies {

    def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
    def plugins = new Properties()
    def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
    }
    plugins.each { name, _ ->
        println name
        embed project(path: ":$name", configuration: 'default')
    }
}

在Android工程下的settings.gradle中加入如下配置:

def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
    pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}

plugins.each { name, path ->
    def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
    include ":$name"
    project(":$name").projectDir = pluginDirectory
}

這樣打包出來的aar就包含了第三方的aar。

混合開發的調試填坑

Flutter android混合工程的依賴方式寫到這裏,但是實際開發中會有一個無法避免的坑,就是flutter工程無法調試android代碼,如下圖所示例:
原生Android項目:

image

 

Flutter項目:

image

 

爲了解決這一問題,我想到的解決方法就輸使用aar模式來進行原生Android的開發,用依賴module的方式來進行flutter工程的開發。

開發模式參數配置

我們可以通過一個變量來控制當前開發環境是Android還是Flutter,效果如下:

image

 

通過gradle.properties的一個值來控制進行flutter開發還是Android部分開發。

具體操作如下:
1、講Flutter部分打包成aar放入libs文件夾下
2、Android工程的project的build.gradle加入如下依賴:

    classpath 'com.kezong:fat-aar:1.2.7'

3、app的build.gradle加入如下配置:

dependencies{
    def properties = new Properties()
    def pluginsFile = new File("${rootDir.getAbsolutePath()}/gradle.properties")
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> properties.load(reader) }
    }
    def isFlutterDebug = properties.getProperty('isFlutterDebug').toBoolean()
    if (isFlutterDebug) {
        implementation project(':flutter')
    } else {
        implementation files('libs/flutter-release.aar')
//        implementation files('libs/flutter-debug.aar')
    }
}

4、gradle.properties加入如下參數來控制調試方式:

## 是否在線調試flutter代碼
isFlutterDebug=false

5、settings.gradle修改爲如下配置:
xxx_flutter爲自己flutter工程的名字。

def properties = new Properties()
def rootProjectFile = new File(settingsDir.getPath()).getAbsolutePath()
def propertiesFile = new File("${rootProjectFile}/gradle.properties")
if (propertiesFile.exists()) {
    propertiesFile.withReader('UTF-8') { reader -> properties.load(reader) }
}
def isFlutterDebug = properties.getProperty("isFlutterDebug").toBoolean()
if (isFlutterDebug) {
    setBinding(new Binding([gradle: this]))
    evaluate(new File(settingsDir.parentFile,
            'xxx_flutter/.android/include_flutter.groovy'
    ))
    include ':xxx_flutter'
    project(':xxx_flutter').projectDir = new File('../xxx_flutter')
}else {
    def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

    def plugins = new Properties()
    def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
    }

    plugins.each { name, path ->
        def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
        include ":$name"
        project(":$name").projectDir = pluginDirectory
    }
}

這樣配置完成後就可以由自己來控制到底進行Flutter開發還是Android開發。

總結

Flutter開發的坑還有不少,以此記錄方便大家來解決問題。

今年年初我花一個月的時間收錄整理了一套知識體系,如果有想法深入的系統化的去學習的,可以私信我【安卓】,我會把我收錄整理的資料都送給大家,幫助大家更快的進階。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章