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

Flutter Android Package调用python

操作步骤

一、创建一个Flutter Package

使用以下指令创建一个Flutter Package

flutter create --template=plugin --platforms=android,ios -a java flutter_package_python

二、修改android/build.gradle文件

在buildscript——>dependencies中添加以下内容

//导入Chaquopy插件,使Android可以使用Python
classpath "com.chaquo.python:gradle:15.0.1"

在android上方导入chaquo.python插件

apply plugin: 'com.chaquo.python'

在android——>defaultConfig中添加ndk和python配置

       ndk {abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"}python {version "3.8"pip { //根据python用到的依赖进行下载install "scipy"install "numpy" install "PyWavelets"install "scikit-image"install "tflite-runtime"install "statsmodels"}} 

三、编写python文件

在android——>src——>main目录下创建名称为calc的python文件,写一个简单的加法函数:

# calc.py
def add(a, b):return a + b

四、创建一个工厂函数,用于初始化Python以及引用Python函数

在android——>src——>main——>com——>expample——>flutter_package_python下创建PythonFactory.java文件,对Python进行初始化,以及调用Python中的加法函数

package com.example.flutter_package_python;import android.content.Context;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;public class PythonFactory {private static PythonFactory instance = null; // 静态实例private Python pythonInstance; // Python 环境实例private PyObject calcModule; // Python 模块private static boolean isInitialized = false;// 私有化构造函数,确保外部无法直接实例化private PythonFactory(Context context) {if (!isInitialized) {Python.start(new AndroidPlatform(context)); // 需要传入上下文isInitialized = true;}pythonInstance = Python.getInstance(); // 初始化 Python 环境calcModule = pythonInstance.getModule("calc"); // 加载 calc.py 模块}// 获取单例工厂实例public static synchronized PythonFactory getInstance(Context context) {if (instance == null) {instance = new PythonFactory(context); // 初始化一次}return instance;}// 工厂方法:调用 Python 中的加法函数public int addTwoNumbers(int a, int b) {PyObject result = calcModule.callAttr("add", a, b); // 调用 Python 中的 add 方法return result.toInt(); // 将结果返回为整数}
}

五、在FlutterPackagePythonPlugin中使用工厂函数

1.定义pythonFactory变量,在onAttachedToEngine中初始化工厂函数并将结果返回给pythonFactory

  private PythonFactory pythonFactory;@Overridepublic void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {// 其他代码pythonFactory = PythonFactory.getInstance(flutterPluginBinding.getApplicationContext());}

2. 在onMethodCall添加判断,当dart中调用了addTwoNumbers函数则使用Python进行相加计算

if (call.method.equals("addTwoNumbers")) {int a = call.argument("a");int b = call.argument("b");// 使用工厂类调用 Python 的加法函数int sum = pythonFactory.addTwoNumbers(a, b);result.success(sum); // 将结果返回给 Flutter 端}

六、编写dart代码调用安卓中的java

在lib/flutter_packages_audio_analysis_platform_interface.dart中添加一个addTwoNumbers函数

Future<int?> addTwoNumbers(int a, int b) {throw UnimplementedError('addTwoNumbers() has not been implemented.');}

在lib/flutter_packages_audio_analysis_method_channel.dart中重写addTwoNumbers方法

 @overrideFuture<int> addTwoNumbers(int a, int b) async {try {final result =await methodChannel.invokeMethod('addTwoNumbers', {'a': a, 'b': b});return result;} on PlatformException catch (e) {print("调用 Python 失败: '${e.message}'.");return -1;}}

3.在lib/FlutterPackagesAudioAnalysis类中添加addTwoNumbers函数

 Future<int?> addTwoNumbers(int a, int b) {return FlutterPackagesAudioAnalysisPlatform.instance.addTwoNumbers(a, b);}

七、写测试代码

import 'package:flutter/material.dart';
import 'dart:async';import 'package:flutter/services.dart';
import 'package:flutter_packages_audio_analysis/flutter_packages_audio_analysis.dart';void main() {runApp(const MyApp());
}class MyApp extends StatefulWidget {const MyApp({super.key});@overrideState<MyApp> createState() => _MyAppState();
}class _MyAppState extends State<MyApp> {String _platformVersion = 'Unknown';final _flutterPackagesAudioAnalysisPlugin = FlutterPackagesAudioAnalysis();@overridevoid initState() {super.initState();initPlatformState();}// Platform messages are asynchronous, so we initialize in an async method.Future<void> initPlatformState() async {String platformVersion;// Platform messages may fail, so we use a try/catch PlatformException.// We also handle the message potentially returning null.try {platformVersion =await _flutterPackagesAudioAnalysisPlugin.getPlatformVersion() ??'Unknown platform version';} on PlatformException {platformVersion = 'Failed to get platform version.';}// If the widget was removed from the tree while the asynchronous platform// message was in flight, we want to discard the reply rather than calling// setState to update our non-existent appearance.if (!mounted) return;setState(() {_platformVersion = platformVersion;});}int addRes = 0;add() async {int a = 8;int b = 9;addRes =await _flutterPackagesAudioAnalysisPlugin.addTwoNumbers(a, b) ?? 100;setState(() {});}add1() async {int a = 8;int b = 19;addRes =await _flutterPackagesAudioAnalysisPlugin.addTwoNumbers(a, b) ?? 100;setState(() {});}@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: AppBar(title: const Text('Plugin example app'),),body: Center(child: Column(mainAxisSize: MainAxisSize.min,children: [Text('Running on: $_platformVersion\n'),ElevatedButton(onPressed: add, child: Text('Add')),Text("相加结果:${addRes}"),ElevatedButton(onPressed: add1, child: Text('Add1')),],),),),);}
}


http://www.mrgr.cn/news/29576.html

相关文章:

  • 阅读2020-2023年《国外军用无人机装备技术发展综述》笔记_技术趋势
  • 【Unity Bug 随记】unity version control 报 xx is not in a workspace.
  • java项目-jenkins任务的创建和执行
  • Vue Cli的配置中configureWebpack和chainWebpack的主要作用及区别是什么?
  • 七大经典基于比较排序算法【Java实现】
  • wordpress站外调用指定ID分类下的推荐内容
  • SOLIDWORKS® 2025 新增功能 - SIMULATION
  • 函数题 6-12 判断奇偶性【PAT】
  • 【Vue】- 路由及传参
  • 利用WPF绘制轮廓并保存为图片
  • 文件保护措施:防止文件误删、覆盖及恶意篡改的策略探讨
  • c++设计模式
  • 珠海市自闭症寄宿学校,为孩子打造温馨成长环境
  • python队列操作
  • 李沐也看好的方向:多模态预训练!最新成果刷爆SOTA,轻松拿下顶会
  • arm
  • 蚂蚁在 RAG 与向量检索上的实践:技术应用与创新分析
  • 新书出版,大陆首本NestJS图书《NestJS全栈开发解析:快速上手与实践》
  • SDKMAN!软件开发工具包管理器
  • JS全选反选案例
  • 组合式 API 和选项式 API的区别
  • VulhubDC-4靶机详解
  • EtherCAT 转 Profinet 网关在深海钻探工作中的作用
  • Java小区物业管理系统
  • axure的下载,激活,汉化全过程,多图
  • 计算机知识科普问答--12 (56-60)