Calling a function inside .a file in Xamarin - android

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.

Related

Can you link static library directly in android application?

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.

Static initialization of openCV in android is not working

I'm trying to add openCV libraries into an Android project to use it in the static initialization way but when I build the application the openCV libraries are not added to the apk.
I put them under the root directory source (called app in Android Studio) in a folder called libs, and then I nested a folder for every target: armeabi, x86, etc.
Do I need to do something else?anybody knows why they are not being added to the apk?
Thanks.
Have you used the Android NDK? Because I think OpenCV is native C/C++ code so you need the NDK in order to accommodate the library into your project.
I finally found the solution to my problem. I just had to put the .so files under src/main/jniLibs and they are added to the apk when bulding. Easier than expected :)

How can I use a static library built using Code Blocks in Android Studio?

I have a .a static library file that i have created using Code Blocks. It contains the function definition of a function that adds two numbers. I want to use this library in Android using NDK. I want to know how this can be done in detail. I am beginner at this, and therefore step-by-step explanation would be very helpful.
You have to recompile your static library for Android using the NDK first. A static lib compiled for a Linux system will certainly not work (different C and system libraries).
Plus, Android runs on 7 different architectures as of now, so if you want to properly support all of them, you should end up with 7 different versions of your .a static library.
You can then reference your library from another NDK module (.so files) that you can load from an Android application. Please follow the NDK documentation to see how this works, and samples here: https://github.com/googlesamples/android-ndk

Create a .jar from a .dll

I’m currently trying to create a .jar from a .dll, in order to use it on Android applications. I’m quite new with this technology, so I’ll tried to be as clear as possible.
I have 3 distinct parts:
A first .dll which is an API, in C++ and developed with Visual studio 2013.
A second .dll that makes the link between my API and Java code (using JNI, so this is my native library), also developed with Visual studio 2013.
And my Java code that loads my native library and that implements native functions from API. I use eclipse IDE for that stuff, because I read somewhere that was the easiest way to create a .jar.
This part works pretty well. I created a main test and I get all the information from the API in Java. But now, I would like to create a .jar file that I can use on Android and here comes the crows...
I tried many ways, from the simple .jar export, to the One-Jar method, but nothing worked. I always have a link error on my android platform (functions are recognized on the .jar, but their implementations are not).
So here is my question, how to create a kind of ‘static library’ in a .jar that I can use on Android?
I hope I was clear enough, don’t hesitate to ask me more details.
Thank you for your help!
EDIT:
I tried with an Android Library project and my problem is always coming from my loadLibrary() function:
public class MyWrapper {
static {
System.loadLibrary("MyWrapper");
}
public static native int getNegative(int p_number);
public static native int getPositive(int p_number);
}
Every things works fine on my library, but when I use it in android application, I always get this error:
01-27 11:31:29.565: E/AndroidRuntime(7089): FATAL EXCEPTION: main
01-27 11:31:29.565: E/AndroidRuntime(7089): Process: com.example.wrappertest, PID: 7089
01-27 11:31:29.565: E/AndroidRuntime(7089): java.lang.UnsatisfiedLinkError: Couldn't load MyWrapper from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.wrappertest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.wrappertest-2, /vendor/lib, /system/lib]]]: findLibrary returned null
My library cound’t be created as static library, to keep the link on the .dll?
A .dll file is usually windows specific. If you're wanting to load native code on Android you'll have to look into using the Native Development Kit. The specifics of using the NDK is a bit beyond what can be explained here, but the Android Developer website provides a decent starting point at http://developer.android.com/tools/sdk/ndk/index.html
As for the jar itself, you may want to look into building an Android Library Project instead. This is essentially a means of packaging a jar with some assets (including native libraries) which can be used by an Android Application project.
Compile your librarie with ndk-build
Create an Android Library module
Create a Java class and JNI wrapper
Disable gradle for ndk-build and create Android.mk and Application.mk in the jni folder
Import the .so library and pre build it on the Android.mk
Invoke ndk-build manually and its done

How to use/incorporate CPP files to Android project?

We have developed an iPad application where the core logic is written in CPP code, so that we can use the same code files/libraries to other platforms.
Now I want to use those files and develop similar Android application, but unable to create .so files and integrate paths in Android.mk files and all. I am basically an iOS developer, this is first time I am looking into Android NDK.
Can anyone help and guide if there is any straight forward steps to it.
I have already gone through android developers site and few other tutorial sites. But none of those worked for me.
Require easy-clear steps to call cpp method in java, if I do have few cpp files and .a libraries with me already.
You aren't very specific at the step you are stuck at.
Here's a very quick explanation on how to call native code from java (android) :
first create a method to be exported by the native and called by java (this uses JNI, so google JNI , JNIEXPORT)
once you have this method defined in your native code, it's time to create a shared library (.so) file , using the compiler that comes in the NDK (because you are compiling for android ). You will need to compile for the correct architecture of the device (armeabiv7s is the most common now days).
you need to add the library file in your app.apk inside the armeabi folder (more details in NDK tutorials).
inside your java code you will need to load the shared library via the System.loadLibrary(LIBRARY_NAME);
inside your java code you will need to have defined static native methods that are in pair with the methods you exported from your CPP code
Quick tips :
use C functions,not CPP , since CPP will be mangled in the resulting shared library. In other words, you will need to create a C wrapper that will call your cpp code.
look over a hello world tutorial for NDK , and work yourself from there . Here's a link to such tutorial http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/
You will bump later on into compilation issues with the makefiles, but by then you will probably be able to be more specific with your question.
Easiest way is to use the hello-jni Android studio sample project.
There are a lot of settings and configurations, you get them from the sample that is a working unit, always easiest when starting from something working.
First run (and modify) the hello-jni and learn how the interactivity between the Java and C parts works. About everything works except environmental ANSI C/C++ stuff. You have to get things like language, country etc from Java and transfer it to the C-code. You are in US in English with "inches and gallons" in JNI.
Then to an own project you create with android studio, copy and modify from it bit by bit from hello-jni. When you have our own branded hello-JNI you can add bit by bit your own code. Often using C-dummies for testing the interactivity with the Java part is easier, then change it to the real C/C++ code of yours.
Read the Android/Android studio documentation and learn and understand. Use the Android emulators, much easier and they are good.
The project configuration stuff is by far the hardest to handle at the start. If I would make a new project today, I would start from the Hello-JNI once again.

Categories

Resources