Android 原生项目混编 flutter module

509 阅读2分钟

跨平台 flutter 技术愈发稳定,生态愈发丰富,是时候入坑了,记录一下我的 Android 原生项目混编 flutter module

  1. 调整本地项目文件目录,将 Android 项目源码单独存放一个文件夹内,便于同级目录添加 flutter module,调整后的结构为下图: image.png 注:Android 文件夹为 Android 原生项目源码

  2. 新建 flutter module, 可以通过命令行,或者android studio 创建

    通过命令行新建
    flutter create --template module flutter_module
    flutter create -t module --org com.wan.xf flutter_module   //指定包名
    注意:要切换到x-frame目录下 执行命令,因为我们要在 android 的同级目录下创建 flutter module
    

    image.png

  3. 将 Flutter 库打包成由 AAR 切换到 flutter_module 目录下,执行命令:flutter build aar 完整执行如下: 重点关注 Consuming the Module 之后的 1.2.3.4步骤,原生 Android代码配置要用到的

    admin@bogon x-frame % cd flutter_module 
    admin@bogon flutter_module % ls
    README.md                       flutter_module.iml              lib                             pubspec.yaml
    analysis_options.yaml           flutter_module_android.iml      pubspec.lock                    test
    admin@bogon flutter_module % flutter build aar
    
    💪 Building with sound null safety 💪
    
    Running Gradle task 'assembleAarDebug'...                          17.9s
    ✓ Built build/host/outputs/repo.
    Running Gradle task 'assembleAarProfile'...                        28.2s
    ✓ Built build/host/outputs/repo.
    Running Gradle task 'assembleAarRelease'...                        25.5s
    ✓ Built build/host/outputs/repo.
    Consuming the Module
    1. Open <host>/app/build.gradle
    2. Ensure you have the repositories configured, otherwise add them:
      String storageUrl = System.env.FLUTTER_STORAGE_BASE_URL ?: "https://storage.googleapis.com"
      repositories {
        maven {
            url '/Users/admin/Documents/SporeLab/x-frame/flutter_module/build/host/outputs/repo'
        }
        maven {
            url "$storageUrl/download.flutter.io"
        }
      }
    
    3. Make the host app depend on the Flutter module:
    
    dependencies {
      debugImplementation 'com.wan.xf.flutter_module:flutter_debug:1.0'
      profileImplementation 'com.wan.xf.flutter_module:flutter_profile:1.0'
      releaseImplementation 'com.wan.xf.flutter_module:flutter_release:1.0'
    }
    
    
    4. Add the `profile` build type:
    
    android {
      buildTypes {
        profile {
          initWith debug
        }
      }
    }
    To learn more, visit https://flutter.dev/go/build-aar
    admin@bogon flutter_module % 
    
  4. 以上完成了 flutter module 部分工作,

    下面开始原生 Android 项目的配置 引入 Java8 和 设置 NDK, 如官网说明 image.png image.png 在 Android app module 下的 build.gradle 中添加 flutter 相关配置

    android {
       defaultConfig {
            //......省略部分配置
            ndk {abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'}
         } 
       compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
         }
        buildTypes { 
            //......省略部分配置
            profile { initWith debug } //这是Consuming the Module 中的第四点提到的
        }
    }
    
    dependencies {
      debugImplementation 'com.wan.xf.flutter_module:flutter_debug:1.0'
      profileImplementation 'com.wan.xf.flutter_module:flutter_profile:1.0'
      releaseImplementation 'com.wan.xf.flutter_module:flutter_release:1.0'
    }//还记得 Consuming the Module 中的第三点吗
    
  5. 打开android 根目录的 settings.gradle 进行相关配置 还记得 Consuming the Module 提到的第二点吗?我们将这部分代码添加到 settings.gradle中,最终效果如下 image.png 完成上面配置后,试着编译运行原生 Android 项目,No error 表示配置完成;

  6. 下面开始添加代码完成原生 Activity 跳转 flutter 界面的逻辑 AndroidManifest.xml中添加声明代码

    <activity
    android:name="io.flutter.embedding.android.FlutterActivity"  
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    />
    

    在需要跳转的按钮事件内,调用如下代码:

     private fun goFlutterModule() {
        //缓存设置
        // Instantiate a FlutterEngine.
        var flutterEngine = FlutterEngine(requireContext())
        // Start executing Dart code to pre-warm the FlutterEngine.
        flutterEngine!!.dartExecutor.executeDartEntrypoint(
        DartExecutor.DartEntrypoint.createDefault())
        // Cache the FlutterEngine to be used by FlutterActivity.
        FlutterEngineCache
        .getInstance()
        .put("my_engine_id", flutterEngine)
        startActivity(
        //缓存
        FlutterActivity
            .withCachedEngine("my_engine_id")
            .build(requireContext())
        )
      }
    
以上就完成了 Android 原生项目混编 flutter module了,下一步开始 flutter 的相关学习