0、创建flutter module项目
命令行执行lutter create -t module flutter_module
或者用android studio创建
1、setting.gradle添加一下代码,要注意更换成自己的项目
setBinding(new Binding([gradle:this]))
evaluate(new File(
settingsDir.parentFile,
'flutter_module/.android/include_flutter.groovy'
))
2、修改build.gradle
文件位置
minSdkVersion 改为 16
配置编译版本,在android{}里添加
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies里最后一行添加
implementation project(':flutter')
3、点击sync now同步脚本
4、run,run成功后说明混编环境配置好
如果遇到下面错误
Installation failed with message Invalid File:
参考https://mp.csdn.net/postedit/102985511
5、android mainactivity代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.test).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.someContainer,Flutter.createFragment("{name:'devio',dataList:['aa']}"));
tx.commit();
}
});
}
}
布局中代码
6、main.dart 中代码
import 'package:flutter/material.dart';
import 'dart:ui';
void main() => runApp(MyApp(androidParams: window.defaultRouteName));
class MyApp extends StatelessWidget {
final String androidParams;
MyApp({Key key,this.androidParams}):super(key:key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: this.androidParams??""),
);
}
}
7、启用flutter模块的热重载
打开一个模拟器,运行android项目
进入到flutter模块的根目录的命令行下运行flutter attach -d emulator-5554 ,这里换成你的模拟器名字
等命令行出现Waiting for a connection from Flutter on Android SDK built for x86...
打开flutter编写的页面,会出现
Syncing files to device Android SDK built for x86...
2,707ms (!)
To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Android SDK built for x86 is available at: http://127.0.0.1:61581/06WC6YnSW2w=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
此时修改flutter代码
然后命令行输入r,就可以看到代码热重载了
命令行输入R,表示热重启
8、调试dart代码
关闭app
flutter工程下点击按钮,
没有该按钮的话,需要自己添加上,实际上就是菜单栏Run->flutter attach
打开app
flutter工程里添加断点就可以调试了