当前位置: 首页 > news >正文

如何在Flutter工程中添加Android AAR文件

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

为了方便Android和iOS移动开发,Google推出了Flutter。Flutter和Xamarin,Cordova的概念相似 - 使用一种编程语言为多个平台开发。Flutter使用Dart。不过当使用第三方SDK的时候,Flutter依然需要用到Java或者Objective-C来写后台代码。

添加Android AAR

下载Flutter源码。

选择一个aar文件,我这里用DynamsoftBarcodeReader.aar。

把目录flutter/examples/hello_services/android/导入到Android Studio中。

点击File > New > New Module,选择Import .JAR/.AAR Package,添加AAR文件。打开工程属性,添加依赖模块就可以了。

flutter android aar

Flutter UI与Java后台

打开AndroidManifest.xml 添加权限。

使用Java代码调用aar中的接口,然后把结果通过消息的形式发送到Flutter UI。

private String onGetBarcode(String json) {
        String filename;
        try {
            JSONObject message = new JSONObject(json);
            filename = message.getString("filename");
        } catch (JSONException e) {
            Log.e(TAG, "JSON exception", e);
            return null;
        }
 
        String locationProvider;
        String barcodeResult = "No barcode detected";
        File file = new File(filename);
        if (!file.exists()) {
            barcodeResult = "No file exists: " + file.toString();
            Toast.makeText(BarcodeReaderActivity.this, barcodeResult, Toast.LENGTH_LONG).show();
 
            return null;
        }
        else {
            Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
            BarcodeReader reader = new BarcodeReader("license");
            ReadResult result = reader.readSingle(bitmap, Barcode.QR_CODE);
            Barcode[] all = result.barcodes;
            if (all != null && all.length == 1) {
                barcodeResult = all[0].displayValue;
            }
            else {
                barcodeResult = "no barcode found: " + file.toString();
            }
 
            bitmap.recycle();
 
        }
 
        JSONObject reply = new JSONObject();
        try {
            if (barcodeResult != null) {
              reply.put("result", barcodeResult);
            } else {
              reply.put("result", "No barcode detected");
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSON exception", e);
            return null;
        }
 
        return reply.toString();
    }

创建Flutter Input, Button以及Text widgets:

@override
  Widget build(BuildContext context) {
    if (_isExisted) {
      return new Material(
          child: new Center(
              child: new Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    new Text('Barcode Reader'),
                    new Input(
                      labelText: 'Please input the image path',
                      value: new InputValue(text: _filename),
                      onChanged: onTextChanged,
                      autofocus: true,
                    ),
                    new Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          new RaisedButton(
                              child: new Text('Read'),
                              onPressed: _getBarcode
                          ),
                          new RaisedButton(
                              child: new Text('Reset'),
                              onPressed: _resetResult
                          ),
                        ]
                    ),
                    new Image.file(new File(_filename)),
                    new Text('$_result'),
                  ]
              )
          )
      );
    }
    else {
      return new Material(
          child: new Center(
              child: new Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    new Text('Barcode Reader'),
                    new Input(
                      labelText: 'Please input the image path',
                      onChanged: onTextChanged,
                      autofocus: true,
                    ),
                    new Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          new RaisedButton(
                              child: new Text('Read'),
                              onPressed: _getBarcode
                          ),
                          new RaisedButton(
                              child: new Text('Reset'),
                              onPressed: _resetResult
                          ),
                        ]
                    ),
                    new Text('$_result'),
                  ]
              )
          )
      );
    }
  }
 
  Future<Null> _readBarcode() async {
    final Map<String, String> message = <String, String>{'filename':_filename};
    final Map<String, dynamic> reply = await HostMessages.sendJSON('getBarcode', message);
    // If the widget was removed from the tree while the message was in flight,
    // we want to discard the reply rather than calling setState to update our
    // non-existent appearance.
    if (!mounted)
    return;
    setState(() {
    _result = reply['result'].toString();
    });
  }

运行程序:

flutter barcode reader

参考资料

  • accessing platform and third-party services in Flutter.
  • Example of embedding Flutter using FlutterView

安装包问题

Flutter工程编译之后,会把libsky_shell.so打包到APK的armeabi-v7a目录中。我使用的aar文件还包含了arm64-v8a,编译之后会发现arm64-v8a目录中没有libsky_shell.so。这个时候如果APK安装到64位CPU的安卓设备上,会因为找不到libsky_shell.so导致程序崩溃无法启动。

flutter armeabi-v7a

flutter arm64-v8a

解决的方法就是只保留armeabi-v7a的动态连接库,其余都删掉。

源码

https://github.com/yushulx/flutter-android-aar

 

转载于:https://my.oschina.net/yushulx/blog/830022

相关文章:

  • Confluence-企业知识管理与协同软件安装步骤
  • Mac下关闭Sublime Text 3的更新检查
  • AngularJS实现跨域请求
  • 云计算学习(3-3)云计算的由来-应运而生
  • 1095. Cars on Campus (30)
  • Java之戳中痛点 - (1)易变业务使用脚本语言编写
  • CENTOS查看系统磁盘使用情况
  • PHP.ini配置文件(转载)
  • ie中存在的兼容问题及解决办法
  • Elasticstack 5.1.2 集群日志系统部署及实践
  • Roslyn如何实现简单的代码提示
  • 计划
  • msfconsole邮件收集器模块
  • linux grep命令详解
  • Reverse Linked List II
  • 2017-09-12 前端日报
  • css属性的继承、初识值、计算值、当前值、应用值
  • Django 博客开发教程 16 - 统计文章阅读量
  • Facebook AccountKit 接入的坑点
  • Git初体验
  • JavaScript类型识别
  • Java到底能干嘛?
  • JS基础之数据类型、对象、原型、原型链、继承
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Promise面试题,控制异步流程
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • Vue 重置组件到初始状态
  • Vue组件定义
  • 初探 Vue 生命周期和钩子函数
  • 对超线程几个不同角度的解释
  • 记一次删除Git记录中的大文件的过程
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 前端设计模式
  • 入门级的git使用指北
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 协程
  • 移动端解决方案学习记录
  • 最简单的无缝轮播
  • nb
  • No resource identifier found for attribute,RxJava之zip操作符
  • HanLP分词命名实体提取详解
  • Semaphore
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​渐进式Web应用PWA的未来
  • # C++之functional库用法整理
  • #传输# #传输数据判断#
  • $jQuery 重写Alert样式方法
  • (003)SlickEdit Unity的补全
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (windows2012共享文件夹和防火墙设置
  • (第一天)包装对象、作用域、创建对象
  • (二)WCF的Binding模型
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214