好比工程名MyApplication
android
Flutter Module
在cd到当前project下运行命令:api
E:\MyApplication
flutter create -t module my_flutter
复制代码
在工程setttings.gradle
中增长配置:bash
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir, 'my_flutter/.android/include_flutter.groovy'))
复制代码
接下来在app的build.gradle
增长依赖:app
implementation project(':flutter')
复制代码
可能会报错:less
Error: Invoke-customs are only supported starting with Android O (--min-api 26)
复制代码
在app的build.gradle
的android
标签下增长:ide
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
复制代码
最后生成的目录结构:post
通过上面3步就已经集成Flutter了,接着看看怎么在Android
工程中建立Flutter的UIgradle
Android
中建立Flutter
UIFlutter
提供两种方法引入,一个是View
,一个是Fragment
,看下View
的代码:ui
public class FlutterActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flutter);
FlutterView view = Flutter.createView(this, getLifecycle(), "new_page");
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
);
addContentView(view, layoutParams);
}
}
复制代码
第二个参数是Lifecycle
对象,第三个参数是route
,这个参数Flutter
端能够经过window.defaultRouteName
获取this
看下Flutter侧的dart
代码, 在my_flutter
->lib
目录下新建list.dart
:
import 'package:flutter/material.dart';
class ListViewTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget divider1 = Divider(color: Colors.blue);
Widget divider2 = Divider(color: Colors.green);
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text('ListTest'),
),
body: Center(
child: ListView.separated(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text("$index"),
);
},
separatorBuilder: (BuildContext context, int index) {
return index % 2 == 0 ? divider1 : divider2;
},
itemCount: 100),
),
);
}
}
复制代码
在main.dart
中导出路由new_page
:
import 'package:flutter/material.dart';
import 'list.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
routes: {
"new_page": (context) => ListViewTest(),
},
);
}
}
复制代码
hot reload
进入到flutter module目录下, 执行命令 flutter attach 便可, 在这个命令窗口按r
便可hot reload
:
E:\MyApplication\my_flutter
λ flutter attach
Waiting for a connection from Flutter on vivo x27...
Done.
Syncing files to device vivo x27... 1,125ms
🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on vivo 1819 is available at: http://127.0.0.1:55515/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
Initializing hot reload...
Reloaded 2 of 433 libraries in 969ms.
复制代码
最后对比看下继承了Flutter
和没有继承的Android
工程打出来的Release
包大小对比:
接入Flutter
以后,包大小激增5M,主要是由于flutter.so
这个so库。还有flutter_assets
里是flutter工程产生的assets文件, isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr为特定平台的数据和指令.
Refs: