-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mechanism for using static library on Android #75537
Comments
#33227 looks similar to your issue |
Maybe. There is a possibility that the Android side is also shaking the tree and wiping out the symbols in the .a file like Xcode does. If so, we'll need a workaround for Android Studio. However, it's also possible that this needs a bit of build.gradle and CMakeLists.txt magic to pull the .a file into scope. I don't know. And it doesn't seem like the general programming public knows either. Thus why this was filed as a Flutter bug. |
Hi @buzmeg |
Sure. It'll probably take me a day or so to get to it so that I can give you something minimal. However, right now I'm not going to be able to give you something that needs to bang on cmake as I'm fighting with #75600. So, the example will have to just be accessing the library from Flutter/Dart without external cpp files. Thanks. |
Prefix: Everything I'm doing is on Ubuntu 20.04.2 LTS (uname -a: Linux andrewl-x1 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux) Okay, I have attached a flutter project directory which sets up a baseline so that we can talk about what is going on. The project is pretty close to the default "new plugin" project as set up by flutter from the command line. If you unpack and do "flutter run", the project should compile and work. This demonstrates that basic loading of a dynamic library works as advertised. If this project doesn't work out of the box, we need to figure out what is wrong before proceeding. Background: At the top level of the project there is a libs_test directory. This directory contains the source code as well as the build commands to produce and install the dynamic libraries as well as the static libraries. That way you can check if I'm doing something wrong in actually producing the libraries themselves if you need to. There are four libraries that get installed into /android/src/main/jniLibs/: libcomplicated_square_dynamic.so, libcomplicated_square_static.a, libsimple_square_dynamic.so, and libsimple_square_static.a. These are the libraries I'm trying to access. The "simple" libraries only use clang for compilation. The "complicated" libraries use "clang++" and so invoke the C++ machinery which causes its own issues. The project should "flutter run" without error as it stands. This loads "libsimple_square_dynamic.so" from the jniLibs area and calls "simple_square(4)" in order to make sure that everything worked right. There are three flutter-only scenarios that cause bugs and need to be worked out before we can add a ".cpp" file compiled by Flutter/Dart/Gradle/CMake into the mix. Scenario Number 001: Scenario Number 002: Scenario Number 003: Let me know if you have any further questions. |
Sorry, you asked for flutter doctor as well:
|
It is not possible to lookup symbols from a freestanding static library. What you are attempting to do can't be supported. You have to use dynamic library. The documentation does specify static linking as a necessity before |
Hi, @chinmaygarde The documentation specifies:
The problem is that it doesn't give a procedure to do so. Do I need to put that .a file somewhere specific so Flutter picks it up and compiles it in? What changes do I need to make to build.gradle or CMakefile.txt to get Flutter to pick up and compile that static library? That's why I went through all the grief of creating a full project with the cases clearly enumerated so that we can talk about the specifics of "which files to edit and how" instead of a vague "library is embedded". Thanks. |
Just a ping to see what the status of this is. Thanks. |
cc Flutter tools team: @jonahwilliams @jmagman Sorry to annoy you folks, but I figure you folks can assign this to the appropriate people. This seems to have been dropped for almost 3 weeks, and I'd like to get it moving again. Thanks. |
@dcharkes THANK YOU! Okay, no procedure for static, so I can stop trying to find one. Primarily, the use case was that it might be easier to integrate a static library on the c++ side than a dynamic library as the linkage was already done and resolved. However, that seems to not be the case. At some point, I will pull the documentation and explicitly note "Here Be Dragons" on static library on Android. That having been said: the final case is how to build/use the c++ dynamic library with the appropriate machinery and get it called. Currently, when I attempt to do that, the test project I included crashes. What is the current procedure for using a dynamically linked c++ library? Thanks. |
Please file a new issue for this. (Note that |
Okay, I will redo that project to only include the c and c++ dynamic library cases and file a new bug. |
Static linking on Android will not work because there is no linking step in Android. One should use a dynamic library instead. If someone were to have a static library and needs a workaround: You can link the static library into a dynamic library and export all of its symbols. Here's a CMakeLists.txt to accomplish that: # mylib_staticlib_wrapper.c is empty.
add_library(mylib_staticlib_wrapper SHARED mylib_staticlib_wrapper.c)
set_target_properties(mylib_staticlib_wrapper PROPERTIES
PUBLIC_HEADER mylib_staticlib.h
OUTPUT_NAME "mylib_staticlib_wrapper"
)
# Link in the static library, not stripping any symbols.
target_link_libraries(mylib_staticlib_wrapper
-Wl,--whole-archive
libmylib_staticlib.a
-Wl,--no-whole-archive
) Please note that this will create a dynamic library that is larger than normal dynamic library because |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
I can install my dynamic library libopus.so into my Flutter project by placing it in the android/src/main/jniLibs/arm64-v8a folder. That works fine and DynamicLibrary.open() finds it.
However, if I try to use a static library, "libopus.a" that fails.
Where do I put the ".a" file on Android? And how do I lookup the symbol?
This is the Dart/Flutter code I am using:
For those tempted to close this immediately, please note that the case of "Static Library For Android" is missing from:
"Binding to native code using dart:ffi"
https://flutter.dev/docs/development/platform-integration/c-interop
And, having already looked rather extensively through both Google and StackOverflow, it seems like nobody who asked this question actually got any solution.
At minimum, this is a documentation fault. At maximum, this can't be done and is a genuine bug.
I know that I certainly don't have enough knowledge about Flutter to determine which category this falls into.
Refiling because @pedromassangocode closed #75492 immediately and then didn't reopen like he said he would.
Thanks.
The text was updated successfully, but these errors were encountered: