Flutter FFI failed to lookup symbol on Android

385 views Asked by At

I have started to play around with Flutter FFI and Golang. I tested my code and everything works fine on Windows platform, but i have issue with Android. Tried several things that I found on stack but finally failed to find solution to my issue that is failing with error message:

The following ArgumentError was thrown while handling a gesture: Invalid argument(s): Failed to lookup symbol 'GetCPUs': undefined symbol: GetCPUs

Here is my code that is trying to check available cores on device:

Golang:

package main

import "C"
import "runtime"

//export GetCPUs
func GetCPUs() int32 {
    return int32(runtime.NumCPU())
}

func main() {}

I'm building this code with command:

go build -buildmode=c-shared -o ../../cbm/libs/cc.so get_cpus.go

And this puth two files in my flutter project under libs folder:

cc.h
cc.so

Then in the android/app folder I have added config to the build.gradle:

externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

And in the same folder CMakeLists.txt:

project (GetCPUs C)
cmake_minimum_required(VERSION 3.4.1)
add_library(
    cc
    SHARED
    ../../libs/cc.so
)
set_target_properties(cc PROPERTIES LINKER_LANGUAGE C)

Finally this is my Dart code that tries to load library and get the number:

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  int getGo() {
    var lib = "cc.so";

    if (Platform.isWindows) {
      lib = "libs/$lib";
    }

    if (Platform.isAndroid) {
      lib = "lib$lib";
    }

    DynamicLibrary dylib = DynamicLibrary.open(lib);
    final myFunc =
        dylib.lookupFunction<Int32 Function(), int Function()>('GetCPUs');
    return  myFunc();
  }

  void _incrementCounter() {
    setState(() {
      _counter = getGo();
    });
  }

As i mentioned above this works fine on Windows so I assume that I have missed something on android configuration and/or something that cmake is doing with the library that I'm not aware of. Thanks in advance for any help or suggestions what might be an issue.

0

There are 0 answers