小编one*_*mus的帖子

在 Flutter 应用程序中托管可执行文件

我有一个在 android 上运行的基本 flutter 项目,当应用程序启动时,我编写了一个捆绑在我的资产中的可执行文件。

  static String appInternalPath = '/data/data/com.maksimdan.face_merger';

  void writeExecutable() async {
    var executablePath = join(appInternalPath, 'main');

    if (await File(executablePath).exists()) {
      File(executablePath).delete();
      print('deleted old executable');
    } else {
      print('not executable exists');
    }

    ByteData data = await rootBundle.load('lib/py/dist/main');
    List<int> bytes =
        data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    await File(executablePath).writeAsBytes(bytes);
    print('wrote new executable');
  }
Run Code Online (Sandbox Code Playgroud)

稍后在我的代码中我尝试运行它。

  void invokeExecutable() async {
    String executablePath = join(appInternalPath, 'main');
    Process.run('chmod', ['u+x', executablePath]).then((ProcessResult results) {
      Process.run(executablePath, []).then((ProcessResult results) {
        print(results.stdout);
      });
    });
  }
Run Code Online (Sandbox Code Playgroud)

但获取权限被拒绝错误。

E/flutter (31825): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: ProcessException: …
Run Code Online (Sandbox Code Playgroud)

dart flutter

11
推荐指数
1
解决办法
2415
查看次数

标签 统计

dart ×1

flutter ×1