Flutter,在预构建 GO .so 库上使用 DynamicLibrary.open() 无需编写 Native Code(Java/Swift)

jal*_*nga 8 shared-libraries go dart flutter

我制作了一个 GO 库并将其构建为 .so 库。正在使用 Java 加载库,System.loadLibrary()但我无法管理它使用DynamicLibrary.open().

我想跳过调用本机代码的过程,直接在flutter中加载共享库。

DynamicLibrary.open 仅在开发频道 v1.10.14 上可用。

使用 Cmake 的示例文档:https ://flutter.dev/docs/development/platform-integration/c-interop

我的代码:

static final DynamicLibrary nativeAddLib = DynamicLibrary.open("lib-mylib.so");
final int Function (String ifName, int tunFd, String settings) addStuff = nativeAddLib.lookup<NativeFunction<Int32 Function(String, Int32, String)>>("addStuff").asFunction();
Run Code Online (Sandbox Code Playgroud)

错误:

Compiler message:
lib/vpn_connection/vpn_connection_bloc.dart:20:164: Error: Expected type 'NativeFunction<Int32 Function(String, Int32, String)>' to be a valid   and instantiated subtype of 'NativeType'.
- 'NativeFunction' is from 'dart:ffi'.
- 'Int32' is from 'dart:ffi'.
final int Function (String ifName, int tunFd, String settings) addStuff = nativeAddLib.lookup<NativeFunction<Int32 Function(String, Int32, String)>>("addStuff").asFunction();
                                                                                                                                                                 ^
Exception: Errors during snapshot creation: null
#0      KernelSnapshot.build (package:flutter_tools/src/build_system  /targets/dart.dart:226:7)
<asynchronous suspension>
#1      _BuildInstance._invokeInternal (package:flutter_tools/src/build_system/build_system.dart:526:25)
<asynchronous suspension>
#2      _BuildInstance.invokeTarget.<anonymous closure>   (package:flutter_tools/src/build_system/build_system.dart:481:35)
#3      new Future.sync (dart:async/future.dart:224:31)
#4      AsyncMemoizer.runOnce (package:async/src/async_memoizer.dart:43:45)
#5      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:481:21)
Run Code Online (Sandbox Code Playgroud)

看起来没有找到文件/函数。

我的毕业证:

android {
compileSdkVersion 28

lintOptions {
    disable 'InvalidPackage'
}

sourceSets.main {
    jniLibs.srcDirs += files(extraJniDirectory)
}

defaultConfig {
    applicationId "com.custom.android"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    ndk {
        abiFilters "armeabi", "x86", "armeabi-v7a", "arm64-v8a", "x86_64"
    }
}

buildTypes {
    release {
        signingConfig signingConfigs.debug

        ndk {
            if (project.hasProperty('target-platform') &&
                    project.property('target-platform') == 'android-arm64') {
                abiFilters 'arm64-v8a'
            } else {
                abiFilters 'armeabi-v7a'
            }
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

更新:

我在颤振示例中使用 CMake 创建了一个 .so 文件,我从 apk 中提取了它并将它与我的 go build .so 文件放在同一个文件夹中,并且正在工作,但我找不到为什么我的第一个 .so文件在颤振中不起作用,但在 android 中起作用。

更新2:

DynamicLibrary.open("lib-mylib.so")已加载, nativeAddLib.lookup<NativeFunction<Int32 Function(String, Int32, String)>>("addStuff")正在返回一个指针,这意味着找到了该函数,但是在调用.asFunction()它时会中断。

通过简化代码:

var addStuff = nativeAddLib.lookup("addStuff").asFunction();
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error: Expected type 'NativeType' to be a valid and instantiated subtype of 'NativeType'.
- 'NativeType' is from 'dart:ffi'.
Run Code Online (Sandbox Code Playgroud)

eug*_*eug 5

我认为问题可能在于 Dart 的 FFI 不直接支持字符串。查看此示例以了解使用单独的 ffi 包编组字符串的方法:https : //github.com/dart-lang/samples/blob/master/ffi/system-command/linux.dart