Android Flutter混合開發問題總結(二)

Android Flutter混合開發問題總結
Android Flutter混合開發問題總結(二)android

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

混合開發的依賴方式

打包成獨立aar

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

./gradlew flutter:assembleDebug
複製代碼

打debug的release包:app

./gradlew flutter:assembleRelease
複製代碼

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

aar的包打好後能夠直接複製到Android工程去依賴便可。
可是若是使用第三方框架會致使第三方框架的沒法打入aar,咱們可使用fat-aar-android來解決這一問題:
在flutter工程的build.gradle加入:post

classpath 'com.kezong:fat-aar:1.2.7'
複製代碼

在Flutter的build.gradle加入以下配置: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中加入以下配置:ui

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。this

Flutter工程做爲module依賴

以module方式依賴以前介紹過,能夠直接查看文章Android Flutter混合開發問題總結lua

混合開發的調試填坑

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

image

Flutter項目:

image

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

開發模式參數配置

咱們能夠經過一個變量來控制當前開發環境是Android仍是Flutter,效果以下:

image

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

具體操做以下:
一、講Flutter部分打包成aar放入libs文件夾下
二、Android工程的project的build.gradle加入以下依賴:

classpath 'com.kezong:fat-aar:1.2.7'
複製代碼

三、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')
    }
}
複製代碼

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

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

五、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開發的坑還有很多,以此記錄方便你們來解決問題。

相關文章
相關標籤/搜索