How can I use '.a' static library built with NDK.
Where do I attach header files( include file ) to call a function.
My Android studio version is 2.23.
Thanks in advance.
A good place to start is the formal documentation, if you haven't tried it already. Here is the general page showing how to add a prebuilt library: Using Prebuilt Libraries
Related
I want to use herumi / bls library in my application. I've successfully compiled libraries following his documentation and get a file armeabi-v7a/libbls384_256.a.
As I guess this is not the same with *.so library and I cannot use this library directly in JNI. I guess instead of this I have to create my own native code to import libbls384_256.a. Is it correct way?
There is no problem to use my own C++ code in my project and this part is already pretty described in different manuals. But I have no idea how to import libbls384_256.a to my native code using Android Studio, Gradle and JNI.
How to include *.a library in native code in Android?
Like you say, you should write a shared library that links against the prebuilt static library. The shared library should contain the exported functions that you want to call from Java, and these can in turn call functions in the static library.
If you're using nkdBuild you'd use the PREBUILT_STATIC_LIBRARY rule, and then add the library to the LOCAL_STATIC_LIBRARIES for your shared library. See https://developer.android.com/ndk/guides/prebuilts
If you're using CMake you'd do something like:
add_library(bls384_256 STATIC IMPORTED GLOBAL)
set_target_properties(bls384_256
PROPERTIES
IMPORTED_LOCATION the/path/to/libbls384_256.a
INTERFACE_INCLUDE_DIRECTORIES the/path/to/the/bls384_256/api/headers
)
I am sorry maybe this is stupid what I am asking, but I have a question about linking static/shared libraries in android.
I am creating a new C++ Native android studio project. After build, I open the apk file, and inside lib the folder there are placed libraries libnative-lib.so for every ABI. Size of APK is 3.580 KB.
But if I change inside CMakeLists.txt to build the native lib like Static lib, so now I got this:
add_library(native-lib
STATIC
native-lib.cpp)
When APK is built, I can not find the static library (libnative-lib.a). There is no lib folder in the apk. Also, the size is 2.836 KB.
Can someone please explain(or give me link for more info about this) where is the library placed in the STATIC build?
And on run I got error if I link static:
No implementation found for java.lang.String com.example.myapplication.MainActivity.stringFromJNI()
You can't use native static libraries directly in Android apps.
Android user space is basically a Java (or more precisely Dalvik VM).
So all user-facing applications must be written in Java or Kotlin (which both compile to Dalvik bytecode).
Static C/C++ libraries must be link in to a C/C++ executable or dynamic library to be used. They can not be loaded directly by Linux or Android.
Since Android app does not have a C/C++ executable in it, the only way to use a static library with an Android app is to link it with a dynamic library (*.so) that can be loaded via Java Native Interface.
Since JNI uses the system loader to load the library, it can only load dynamic libraries, and of those, only ones that export functions with proper naming conventions so they can be matched to a Java class that will be used to call the native code.
I'm trying to use OpenCv 4 on Xamarin.Android by Java Binding Template. These are the steps that i've done:
0) I've compiled opencv binaries through cmake and mingw64 to get .jar and .a
I've put the .jar and the static libraries (.a) in Jars forlder of Xamarin Java Binding Template and i've compiled the template.
1.a) .jar Build Action is EmbeddedJar
1.b) libs Build Action is EmbeddedNativeLibrary
I've added a reference to that template in my Xamarin.Android project: the opencv methods were recognized correctly!
But, when i try to execute:
Mat Source = Imgcodecs.Imread(ImagePath, Imgcodecs.ImreadGrayscale);
i get an error:
Java.Lang.UnsatisfiedLinkError: 'No implementation found for long org.opencv.imgcodecs.Imgcodecs.imread_0(java.lang.String, int) (tried Java_org_opencv_imgcodecs_Imgcodecs_imread_10 and Java_org_opencv_imgcodecs_Imgcodecs_imread_10__Ljava_lang_String_2I)'
I think that there could be a missmatch of method name, maybe due to a wrong java parsing.
I've also tried to use shared libries (.so) by loading them through JavaSystem.LoadLibrary("LibraryNameWithoutInitialLib"), but i have the same error :/
Do you know why?
You cannot link static libraries with Xamarin.Android as the Xamarin/Mono NDK-based runtime is a static main entry executable and does not dynamically get built per project. If you do not need to use a .jar/.aar high-level wrapper, then you will need to use runtime shared libraries and define DllImportAttribute entries for the exported functions that you need to call.
Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism.
Using Native Libraries
Use C/C++ libraries with Xamarin
Note: There are numerous OpenCV C# wrappers / DllImport files in open source ( i.e. a github search away 😁)
Note: If you are using a 3rd-party .jar/.aar , make sure that they are using OpenCV shared libraries and thus not requiring a gradle script to link them into an NDK-based Android app.
so, I m a bit confused here. I have an opencv project which import an image and does something with it and gives output.So I want to use this project as one part in my android project.So how do I proceed. I have SDK, NDK openCV lib all installed. Any help would be great.
Compile your project with NDK as a static library.
Create a shared library with JNI entry point that takes the necessary inputs from Java and return the result to Java. Link it with your static lib and OpenCV.
Create a Java Android project that uses this shared library.
I have a requirement to use C++ code developed using QT inside Xamarin.
The process is like (all done on Windows):
1. Configure and create a static library in QT 5.2
2. Add an Android build kit (MinGW 32) and build the static library for armeabi
3. Use the static library to do P/Invoke inside C# in Xamarin
The problem is that Step 2 above produces a .a file. This is a Linux native object file and I am very sure it will run in Android. How do I use it to perform DllImport and do a P/Invoke? Pardon my ignorence here, I have tried to rename the file to .so and it didn't really help.
Let me know if you need any clarification, your suggestion is very much appreciated.
Thanks, Manoj
You are building a static library. These are meant to be included and loaded at compile time for the programs that use them. What you want it to build a library that can be loaded dynamically, which should end in .so (no you can't just rename it).
You likely need a dynamic library in QT too.