Android原生项目集成Flutter项目混合开发

本文档详细介绍了如何使用fat-aar插件将包含Flutter依赖的第三方插件打包成aar,并集成到Android原生项目中。首先在Flutter项目的build.gradle中配置fat-aar插件,然后在Android原生项目中引用打包后的aar,同时添加对应的flutter.so和flutter_embedding.jar文件。最后,在AndroidManifest.xml中声明相关Activity以实现Flutter页面的跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按照官方文档的集成方式有2种,一种的aar的打包集成方式,一种是引入model的形式,这里主要说下aar打包的方式我遇到的问题,按照官方提供的打包方式我打包下来是没有flutter依赖的第三方插件,百度和谷歌看来下,如果flutter集成了第三方插件的话 需要使用fat-aar插件来打包aar

fat-aar官方文档: https://github.com/kezong/fat-aar-android
flutter官方文档:https://flutter.dev/docs/development/add-to-app/android/project-setup

首先说下我百度谷歌搜送到的基本都是说在flutter中的.android下app中的build.gradle中配置文件,但是我按照集成下来是走不通的,后面看了下在.android下Flutter中的build.gradle来配置,主要修改这2个build.gradle配置文件

build.gradle

按照官方的配置来就行了,这里提一下,需要Open android module in Android Studio

在这里插入图片描述

在项目根目录build.gradle下载fat–aar插件

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.github.kezong:fat-aar:1.3.6'

    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

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

fultter中build.gradle配置中配置引用fat-aar:apply plugin: ‘com.kezong.fat-aar’,后面基本是按照fat-aar官方提供的文档来走了!

pply plugin: 'com.android.library'
apply plugin: 'com.kezong.fat-aar'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
dependencies {
    implementation fileTree(dir: 'libs', include: '*.jar')
    // java dependency
    embed project(path: ':lib-java', configuration: 'default')
    // aar dependency
    embed project(path: ':lib-aar', configuration: 'default')
    // aar dependency
    embed project(path: ':lib-aar2', configuration: 'default')
    // local full aar dependency, just build in flavor1
    flavor1Embed project(path: ':lib-aar-local', configuration: 'default')
    // local full aar dependency, just build in debug
    debugEmbed(name: 'lib-aar-local2', ext: 'aar')
    // remote jar dependency
    embed 'com.google.guava:guava:20.0'
    // remote aar dependency
    embed 'com.facebook.fresco:fresco:1.12.0'
    // don't want to embed in
    implementation('androidx.appcompat:appcompat:1.2.0')
}

以上就是flutter 使用fat-aar打包aar方式,下面集成到Android原生项目中

把aar直接复制到Android项目中引用aar,在app中引入,这里除了引用aar包之外,还需要添加各个 cpu 版本 flutter.so 和 flutter_embedding.jar

repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies{
 api (name: 'flutter-release', ext: 'aar')

    implementation 'io.flutter:flutter_embedding_release:1.0.0-241c87ad800beeab545ab867354d4683d5bfb6ce'
    implementation 'io.flutter:armeabi_v7a_release:1.0.0-241c87ad800beeab545ab867354d4683d5bfb6ce'
    implementation 'io.flutter:arm64_v8a_release:1.0.0-241c87ad800beeab545ab867354d4683d5bfb6ce'
    implementation 'io.flutter:x86_64_release:1.0.0-241c87ad800beeab545ab867354d4683d5bfb6ce'
    }

后面的1.0.0-241c87ad800beeab545ab867354d4683d5bfb6ce项目是对应的版本号

在这里插入图片描述
在这里插入图片描述

在build.gradle引入

 repositories {
  mavenCentral()
        maven {
            url "http://download.flutter.io"
        }
 }

之后在AndroidManifest.xml中引用就可以了

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

MyFlutterActivity是跳转到flutter 页面的 使用的时候 直接跳转到MyFlutterActivity就可以了

public class MyFlutterActivity extends FlutterActivity {
    // 定义Channel名称
    private static final String CHANNEL_NATIVE = "com.flutterToNative";

    public static void openFlutter(Activity activity, String routerUrl) {
        Intent intent = MyFlutterActivity.withNewEngine()
                .initialRoute(routerUrl)
                .build(activity);
        //设置Activity透明
        //intent.putExtra("background_mode","transparent");
        activity.startActivity(intent);
    }

    public static CCEngineIntentBuilder withNewEngine() {
        return new CCEngineIntentBuilder(MyFlutterActivity.class);
    }

    public static CCEngineIntentBuilder withNewEngine(Class<? extends FlutterActivity> activityClass) {
        return new CCEngineIntentBuilder(activityClass);
    }

    public static class CCEngineIntentBuilder extends NewEngineIntentBuilder {

        protected CCEngineIntentBuilder(Class<? extends FlutterActivity> activityClass) {
            super(activityClass);
        }
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        //此处是Flutter与原生通信方法,暂时可不关注
        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL_NATIVE);
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
//                System.out.println("MethodChannel call.method:" + call.method.toString() + "  call arguments:" + call.arguments.toString());
//                NSString *token = [UserDefUtils getUserDefValue:ACCECESSTOKEN];
				//这里是和flutter页面进行交互的,call.method是你和flutter页面约定好的method
                switch (call.method) {
                    case "backToNative":
//                        result.success(2);
                        finish();
                        break;
                    case "iOSFlutter2":
                        Map<String,String> map=new HashMap<>();
                        map.put("token",LoginInfoModel.getToken());
                        map.put("username", UserInfoModel.getNikeName());
                        map.put("serverip", Constant.Config.BASEIP+"/");
                        String newToken = LoginInfoModel.getToken();
                        result.success(map);//这里是传参数给flutter的,是一个objec 按照自己的方式来传,
                        break;
                    default:
                        result.error("404", "未匹配到对应的方法" + call.method, null);
                }
            }
        });
    }
}

注: 至此Android集成flutter 结束 ,如果需要使用缓存或带有缓存引擎的初始路由看下官方文档配置,个人记录,比较粗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值