刚刚在Android项目 混编了flutter特意记录一下
具体的操作可以参考该链接flutter 与Android原生的混编
这次简要介绍下具体的流程,以及实际使用用遇到的问题
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'flutter_staff/.android/include_flutter.groovy'
))
其中需要注意的是:这段代码是针对你的项目名称是app的设置,如果你的项目名称不是app则需要下面的设置,需要制定你的 app项目名称,不然的话编译一直会出错
setBinding(new Binding([gradle: this, mainModuleName: 'airport']))
evaluate(new File(
settingsDir.parentFile,
'flutter_staff/.android/include_flutter.groovy'
))
implementation project(path: ':flutter')
View flutterView = Flutter.createView(
MainActivity.this,
getLifecycle(),
"route1"
);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 800);
layout.leftMargin = 100;
layout.topMargin = 400;
addContentView(flutterView, layout);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_flutter_view, Flutter.createFragment("route1"));
fragmentTransaction.commit();
FlutterBoostPlugin.init(object : IPlatform {
override fun getApplication(): Application {
return this@MyApplication
}
/**
* get the main activity, this activity should always at the bottom of task stack.
*/
override fun getMainActivity(): Activity {
return BaseKTActivity.sRef?.get()!!
}
override fun isDebug(): Boolean {
return false
}
/**
* start a new activity from flutter page, you may need a activity router.
*/
override fun startActivity(context: Context, url: String, requestCode: Int): Boolean {
return PageRouter.openPageByUrl(context, url, requestCode)
}
override fun getSettings(): Map<*, *>? {
return null
}
})
其中 pageRoute是一个页面路由
public class PageRouter {
public static final String FLUTTER_PAGE_URL = "sample://flutterPage";
public static boolean openPageByUrl(Context context, String url) {
return openPageByUrl(context, url, 0);
}
public static boolean openPageByUrl(Context context, String url, int requestCode) {
try {
if (url.startsWith(FLUTTER_PAGE_URL)) {
context.startActivity(new Intent(context, FlutterPageActivity.class));
return true;
} else {
return false;
}
} catch (Throwable t) {
return false;
}
}
}
具体的FlutterpageActivity
class FlutterPageActivity : BoostFlutterActivity() {
/**
* 页面传参
*/
override fun getContainerParams(): MutableMap {
var params = mutableMapOf()
params.put("opEmployeeID", LoginInfoUtils.getEmployeeID())
params.put("deptID",LoginInfoUtils.getDeptID())
params.put("token", LoginInfoUtils.getToken())
params.put("registrationId", "")
params.put("dataCommission", LoginInfoUtils.getDataCommission())
return params
}
override fun onRegisterPlugins(registry: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(registry)
}
/**
* 路由-该名字配置在flutter中,会根据返回的名字打开相应的flutter页面
*/
override fun getContainerName(): String {
return "VipBindingState"
}
}
至此,也就全部搞定了
总结下需要注意的问题: