You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成tflite_flutter_helper_plus报错:找不到libtask_text_jni.so

问题:加载libtask_text_jni.so失败导致Bert QA无法运行

环境依赖

用户使用的Flutter依赖配置:

dependencies:
  flutter:
    sdk: flutter

  tflite_flutter: ^0.10.4
  tflite_flutter_helper_plus: ^0.0.2

实现代码

用户的Bert问答功能实现:

import 'package:flutter/material.dart';
import 'package:tflite_flutter_helper_plus/tflite_flutter_helper_plus.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});


  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: TextButton(
          onPressed: () { getAnswer();},
          child: Text('click me to get answer'),
        ),
      )
    );
  }


  void getAnswer() async {
    String question = "What is the capital of Brazil?";
    String context = "Brazil is a country in South America. It is the largest country in South America and the fifth largest country in the world. The capital of Brazil is Brasília.";
    final bertQuestionAnswerer = await BertQuestionAnswerer.createFromAsset('assets/bert_qa.tflite');
    List<QaAnswer> answeres = bertQuestionAnswerer.answer(context, question);
    print(answeres);
   }
}

错误日志

运行时抛出的异常:

E/flutter (12951): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'libtask_text_jni.so': dlopen failed: library "libtask_text_jni.so" not found
E/flutter (12951): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
E/flutter (12951): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
E/flutter (12951): #2      tflitelib.<anonymous closure> (package:tflite_flutter_helper_plus/src/task/bindings/dlib.dart:32:27)
E/flutter (12951): #3      tflitelib (package:tflite_flutter_helper_plus/src/task/bindings/dlib.dart:40:2)
E/flutter (12951): #4      tflitelib (package:tflite_flutter_helper_plus/src/task/bindings/dlib.dart)
E/flutter (12951): #5      BertQuestionAnswererFromFile (package:tflite_flutter_helper_plus/src/task/bindings/text/qa/bert_qa.dart:13:32)
E/flutter (12951): #6      BertQuestionAnswererFromFile (package:tflite_flutter_helper_plus/src/task/bindings/text/qa/bert_qa.dart)
E/flutter (12951): #7      BertQuestionAnswerer.create (package:tflite_flutter_helper_plus/src/task/text/qa/bert_question_answerer.dart:42:23)
E/flutter (12951): #8      BertQuestionAnswerer.createFromAsset (package:tflite_flutter_helper_plus/src/task/text/qa/bert_question_answerer.dart:78:12)
E/flutter (12951): <asynchronous suspension>
E/flutter (12951): #9      _MyHomePageState.getAnswer (package:bert_qa_teste/main.dart:56:34)
E/flutter (12951): <asynchronous suspension>
E/flutter (12951): 

原因分析

该错误是因为tflite_flutter_helper_plus 0.0.2版本存在打包缺陷,未正确包含Android平台所需的libtask_text_jni.so动态链接库;同时旧版本的tflite_flutter(0.10.4)与该helper库的兼容性不足,导致运行时无法找到依赖的JNI库。

解决方案

  1. 升级依赖版本
    将依赖更新为兼容的最新稳定版本,修改pubspec.yaml:

    dependencies:
      flutter:
        sdk: flutter
    
      tflite_flutter: ^0.14.0
      tflite_flutter_helper_plus: ^0.2.0
    

    (注:版本号以官方最新发布为准,确保两者版本兼容)

  2. 清理缓存并重建项目
    执行以下命令清理缓存并重新获取依赖:

    flutter clean
    flutter pub get
    flutter run
    
  3. 检查模拟器ABI架构
    确保Android模拟器使用的是x86_64或arm64-v8a架构,部分旧版本库对x86架构支持不佳。可以通过Android Studio创建新的模拟器时选择对应架构。

  4. 手动添加JNI库(备选方案)
    如果上述方法无效,可手动下载TensorFlow Lite Task Library的Android预编译库,提取对应ABI的libtask_text_jni.so文件,放置到项目的android/app/src/main/jniLibs/[abi]/目录下(例如arm64-v8a、x86_64),然后重新构建项目。

内容的提问来源于stack exchange,提问作者Willer Silva de Morais

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 00:07:06