Android Flutter 开发一如何手动创建Flutter模块

本文是针对 Android 项目添加 Flutter 模块的情况编写的。

开发环境

  • PC with Win10 20H2
  • Android Studio Arctic Fox | 2020.3.1 Patch 2(试过小松鼠版本,太不喜欢了,电鳗就更没去尝试)
  • AGP 7.0.2

手动创建Flutter模块

Terminal执行下方命令

flutter create -t module --org com.randalldev fluttermodule

然后官方提供了两种方式添加依赖关系:

  1. AAR依赖模式

    AAR模式有个好处就是团队中的其他成员不需要安装Flutter SDK,最少只需要一个人输出AAR即可。

    但是我个人不喜欢这种方式,我更倾向于git submodule的项目管理方式,并且安装Flutter SDK的成本实在算不上高,因此,这种方式,我按下不表。

  2. 模块代码依赖模式

    这种方式确保了一步完成Android项目和Flutter模块的编译。这种方式对于你的开发同时涉及两个部分并且快速迭代很方便,但这需要团队的每个人成员都安装Flutter SDK来确保顺利编译这个混合app

    在主项目的settings.gradle中将Flutter模块作为子项目引入。

// Include the host app project.
 include ':app'                                                // 默认已有的配置
 setBinding(new Binding([gradle: this]))                                // 新增
 evaluate(new File(                                                     // 新增
   settingsDir.parentFile,                                              // 新增
   "${rootProject.name}/fluttermodule/.android/include_flutter.groovy"  // 新增
 ))                                                                     // 新增
 

此时AS会提示你gradle配置变更了,需要重新sync,别急,先别点!

排雷

此时当你点了sync会出现如下报错

* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin class 'FlutterPlugin'.
   > Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by plugin class 'FlutterPlugin'

projectsetting.gradle

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

改为

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

此时当你点了sync会出现如下报错

A problem occurred evaluating project ':app'.
> Project with path ':fluttermodule' could not be found in project ':app'.

projectsetting.gradle的末尾添加

include ':fluttermodule'

此时当你点了sync编译大概率能成功,但是会有很严重的警告

Failed to resolve: androidx.core:core-ktx:1.9.0
Add Google Maven repository and sync project
Show in Project Structure dialog
Affected Modules: app

projectbuild.gradle

task clean(type: Delete) {
    delete rootProject.buildDir
}

上方添加

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

至此,大体上一个混合的Android原生+Flutter项目的初步构建就完成了。

页面跳转

Android原生打开Flutter页面

默认的跳转方式会出现明显的白屏,体验上很不好,这里直接给出优化后的方式

使用FlutterEngine缓存并复用

  1. appAndroidManifest.xml中注册FlutterActivity


  1. app中创建一个App.kt继承Application并在AndroidManifest.xml中配置给application节点的name属性
class App : Application() {
    ···
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.randalldev.hybridflutter">

    manifest>
  1. App.kt中准备好FlutterEngine

创建FlutterEngine实例

    private val flutterEngine by lazy {
        FlutterEngine(this).apply {
            dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        }
    }

重写onCreate()并将实例存储在FlutterEngineCache

override fun onCreate() {
    super.onCreate()

    FlutterEngineCache.getInstance().put("your_engine_id", flutterEngine)
}

重写onTerminate()并将实例销毁

override fun onTerminate() {
    super.onTerminate()

    flutterEngine.destroy()
}

在业务需要的地方使用FlutterEngine中的Intent实例进行跳转

    findViewById<TextView>(R.id.textView).setOnClickListener {
        startActivity(FlutterActivity.withCachedEngine("your_engine_id").build(this))
    }

选择app进行run
Android Flutter 开发一如何手动创建Flutter模块_第1张图片
如果遇到如下Java版本问题,请进行如下配置变更

A problem occurred evaluating project ':flutter'.
> Failed to apply plugin 'com.android.internal.library'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.
  • 选择 Project Structure > SDK location > Gradle Settings 设置Gradle JDK11

Android Flutter 开发一如何手动创建Flutter模块_第2张图片

  • ./gradle.properties中添加上文中对应的java.home路径
# replace with your own jdk11 or above
org.gradle.java.home=C\:\\Softwares\\Google\\Android\\Android_Studio\\jre
  • sync后应该就可以顺利的run

获取方式点击 “此处” 即可 免费获取

你可能感兴趣的:(android,flutter)