首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在预构建的GO .so库上使用DynamicLibrary.open()而不需要编写本机代码(Java/Swift)

在预构建的GO .so库上使用DynamicLibrary.open()而不需要编写本机代码(Java/Swift)
EN

Stack Overflow用户
提问于 2019-10-11 09:02:44
回答 1查看 3.9K关注 0票数 11

我创建了一个GO库,并将其构建为一个.so库。正在用System.loadLibrary()加载Java中的库,但我无法使用DynamicLibrary.open()管理它直接从flutter/dart加载。

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

DynamicLibrary.open仅在dev通道v1.10.14上可用。

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

我的代码:

代码语言:javascript
运行
复制
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();

错误:

代码语言:javascript
运行
复制
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)

看起来是找不到文件/函数。

我的等级:

代码语言:javascript
运行
复制
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'
            }
        }
    }
}
}

更新:

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

Update2:

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

通过简化代码:

代码语言:javascript
运行
复制
var addStuff = nativeAddLib.lookup("addStuff").asFunction();

我知道错误:

代码语言:javascript
运行
复制
Error: Expected type 'NativeType' to be a valid and instantiated subtype of 'NativeType'.
- 'NativeType' is from 'dart:ffi'.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 07:28:23

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58337756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档