android 原生混编 flutter 并使用 flutter_boost进行跳转的步骤以及注意事项

导入步骤: 

1.将flutter_module放在项目中,与app module平级。

2.在项目build.gradle里面,加入以下代码:

include ':app'
// 加入下面配置
setBinding(new Binding([gradle: this]))
evaluate(new File(
        settingsDir.parentFile,
        'inininhelper/flutter_module/.android/include_flutter.groovy'
))

3.在Build →  Rebuild Project 之后,在app的build.gradle里面添加

dependencies {
    ...
    implementation project(':flutter')
    implementation project(':flutter_boost')
}

4.在Build →  Rebuild Project 之后,将以下activity添加到AndroidManifest.xml:



    
    
    
    
    

5.在Application中初始化flutter_boost:

package com.example.mixdemo;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.Platform;
import com.idlefish.flutterboost.Utils;
import com.idlefish.flutterboost.interfaces.INativeRouter;

import java.util.Map;

import io.flutter.embedding.android.FlutterView;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.view.FlutterMain;

public class BaseApplication extends Application {

    private String TAG = "test";

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化
        initFlutterBoost();
    }

    private void initFlutterBoost() {
        //路由,Flutter 启动Native页面的时候回调这里
        INativeRouter router = new INativeRouter() {
            @Override
            public void openContainer(Context context, String url, Map urlParams, int requestCode, Map exts) {
                Log.i(TAG, "路由--INativeRouter:url=" + url);
                Log.i(TAG, "路由--INativeRouter:requestCode=" + requestCode);
                Log.i(TAG, "路由--INativeRouter:urlParams=" + urlParams);
                Log.i(TAG, "路由--INativeRouter:exts=" + exts);

                String assembleUrl = Utils.assembleUrl(url, urlParams);
                PageRouter.openPageByUrl(context, assembleUrl, urlParams);
            }
        };
        //插件注册
        FlutterBoost.BoostPluginsRegister pluginsRegister = new FlutterBoost.BoostPluginsRegister() {
            @Override
            public void registerPlugins(PluginRegistry mRegistry) {
                GeneratedPluginRegistrant.registerWith(mRegistry);

            }
        };
        //配置
        Platform platform = new FlutterBoost.ConfigBuilder(this, router)
                .isDebug(true)
                .whenEngineStart(FlutterBoost.ConfigBuilder.ANY_ACTIVITY_CREATED)
                .renderMode(FlutterView.RenderMode.texture)
                .pluginsRegister(pluginsRegister)
                .build();

        //初始化flutter_boost
        FlutterBoost.instance().init(platform);

    }
}

6.android 调用flutter页面:

val params: MutableMap = HashMap()
params["test1"] = "v_test1"
params["test2"] = "v_test2"
findViewById

7.PageRouter页面

package com.example.mixdemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.idlefish.flutterboost.containers.BoostFlutterActivity;

import java.util.HashMap;
import java.util.Map;

public class PageRouter {

    public final static Map pageName = new HashMap() {{

        put("first", "first");
        put("second", "second");
        put("tab", "tab");
        put("sample://abnormalOrderPage", "abnormalOrderPage");
    }};

    public static final String NATIVE_PAGE_URL = "sample://nativePage";
    public static final String FLUTTER_PAGE_URL = "sample://abnormalOrderPage";
    public static final String FLUTTER_FRAGMENT_PAGE_URL = "sample://flutterFragmentPage";

    public static boolean openPageByUrl(Context context, String url, Map params) {
        return openPageByUrl(context, url, params, 0);
    }

    public static boolean openPageByUrl(Context context, String url, Map params, int requestCode) {

        String path = url.split("\\?")[0];

        Log.i("openPageByUrl", path);

        try {
            if (pageName.containsKey(path)) {
                //打开指定url的flutter页面
                Intent intent = BoostFlutterActivity.withNewEngine().url(pageName.get(path)).params(params)
                        .backgroundMode(BoostFlutterActivity.BackgroundMode.opaque).build(context);
                if (context instanceof Activity) {
                    Activity activity = (Activity) context;
                    activity.startActivityForResult(intent, requestCode);
                } else {
                    context.startActivity(intent);
                }
                return true;
            } else if (url.startsWith(FLUTTER_FRAGMENT_PAGE_URL)) {
                //打开flutter创建的fragment
//                context.startActivity(new Intent(context, FlutterFragmentPageActivity.class));
                return true;
            } else if (url.startsWith(NATIVE_PAGE_URL)) {
                //打开原生Activity
//                context.startActivity(new Intent(context, NativePageActivity.class));
                return true;
            }
            return false;
        } catch (Throwable t) {
            return false;
        }
    }
}

注意事项(重要)

1.注意flutter_boost的版本和与之对应的flutter sdk

Flutter Boost Release Version Support Flutter SDK Version Description Support AndroidX?
1.9.1+2 1.9.1-hotfixes Rename the version number and start supporting androidx by default Yes
1.12.13+3 1.12.13-hotfixes 支持androidx Yes
1.17.1 1.17.1 支持androidx Yes

 

Flutter Boost Branch Support Flutter SDK Version Description Support AndroidX?
v1.9.1-hotfixes 1.9.1-hotfixes for androidx Yes
v1.12.13-hotfixes 1.12.13-hotfixes for androidx Yes
v1.17.1-hotfixes 1.17.1 for androidx Yes

如果不注意这个会出现异常,版本不一致导致库方法不一致。

2.注意flutter module 的pubspec.yaml文件版本信息

如果出现了

The current Dart SDK version is 2.5.0.

Because yybb_flutter depends on flutter_screenutil >=1.1.0 which requires SDK version >=2.6.0 <3.0.0, version solving failed.
pub get failed (1)

类似报错信息,那么需要将pubspec.yaml里的flutter_screenutil:^2.3.0改成flutter_screenutil:any,重新编译后查看pubspec.lock的版本信息,并且修改成该版本:

类似这样:

flutter_screenutil:
  dependency: "direct main"
  description:
    name: flutter_screenutil
    url: "https://pub.flutter-io.cn"
  source: hosted
  version: "1.0.2"

修改为1.0.2,如此类推。

你可能感兴趣的:(安卓开发,android,移动开发,flutter)