Flutter混合Android

0
回复
110
查看
[复制链接]

338

主题

340

帖子

7449

安币

手工艺人

发表于 2019-5-21 10:50:01 | 显示全部楼层 |阅读模式
如果对本篇文章感兴趣,请前往,原文地址:http://www.apkbus.com/blog-822717-80100.html

### 1.新建一个Android工程比如工程名```MyApplication```### 2.创建```Flutter Module```在cd到当前project下运行命令:```E:\MyApplicationflutter create -t module my_flutter```### 3.增加配置代码在工程```setttings.gradle```中增加配置:```include ':app'setBinding(new Binding([gradle: this]))evaluate(new File(settingsDir, 'my_flutter/.android/include_flutter.groovy'))```接下来在app的```build.gradle```增加依赖:```implementation project(':flutter')```可能会报错:```Error: Invoke-customs are only supported starting with Android O (--min-api 26)```在app的```build.gradle```的```android```标签下增加:```compileOptions{        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8}```最后生成的目录结构:![图片描述](/data/attachment/album/201905/21/104850ewegyjyafygyc2ga.png)经过上面3步就已经集成Flutter了,接着看看怎么在```Android```工程中创建Flutter的UI### 4.```Android```中创建```Flutter``` UI```Flutter```提供两种方法引入,一个是```View```,一个是```Fragment```,看下```View```的代码:```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```获取看下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(),      },    );  }}```### 5.```hot reload```进入到flutter module目录下, 执行命令 flutter attach 即可, 在这个命令窗口按```r```即可```hot reload```:```E:\MyApplication\my_flutterλ flutter attachWaiting 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.```### 6.总结最后对比看下继承了```Flutter```和没有继承的```Android```工程打出来的```Release```包大小对比:![图片描述](/data/attachment/album/201905/21/104839c8688ii8ni6f6rs8.png)![图片描述](/data/attachment/album/201905/21/104843hf82zzfc8qp2affp.png)接入```Flutter```之后,包大小激增5M,主要是因为```flutter.so```这个so库。还有```flutter_assets```里是flutter工程产生的assets文件, isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr为特定平台的数据和指令.  继续阅读全文



想在安卓巴士找到更多优质博文,可移步博客区

如果对本篇文章感兴趣,请前往,
原文地址:
http://www.apkbus.com/blog-822717-80100.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )