I have already compiled libraries (for linux(.a) and windows(.dll)). I need to import one of those (I guess for Android I will have to use .a) libraries into my android project.
So I have read some tutorials on NDK, but here is a question - do I need to have source files in order to add libraries to android or I can add already compiled?
You have to use the Android NDK to recompile the library. The ARM architecture is completely different from the x86 architecture. The system calls are different on Linux and Windows. It just cannot be done.
Related
I am compiling a c++ library to be used on my Android device.
In compiling the library I did not take into account the architecture I was building the library for.
As a result I have a 64 bit dynamically linked shared library x86_64 which only works on 64 bit systems.
I intend to link this library to my android device using the JNA tool.
What is the appropriate way to compile my c++ library for android architecture and JNA.
You must use the Android NDK.
Depending on the target architecture, you must select the appropriate toolchain/cross-compiler, e.g. ARM, MIPS or x86.
See also Getting Started with the NDK
JNI or JNA have nothing to do with how the library is built. You must build the library for your target architecture, so it can be used on your phone or tablet.
To access this library from Java, you may use either JNI or JNA.
With JNI, you must implement glue code in C/C++. With JNA on the other side, you do more or less the same, but you use an existing library (libffi) and implement the glue code in Java. This is done dynamically at runtime and may be thought of something like reflection for a library.
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
I have some C++ code (interacts with micro controllers) written already by someone else. I learnt android & NDK and comfortable writing small sample programs. Now I need to integrate both.
So, How should I start proceeding on the integration part? How does the NDK actually works? Assuming I have 3 parts now A - C++ code, B - NDK native interface code, C - Android Activity/Class .
1) Should I compile A (g++ linaro) and then place the object file in Android project to be called by C through B?
(or)
2) Should I compile the A & B together using g++ (linaro) and then copy the .so file into the Android Eclipse project? (Not sure how complex it will be to mimic NDK-build command in normal eclipse).
(or)
3) Copy A into Android Eclipse project and generate java.h file, then generate .so file using the both A & B. (In this method I need to find the right place to put the whole CPP project files in the Android/NDK eclipse project).
PS: I tried to find examples that does this, but only seem to find the simple basic examples, which I am pretty comfortable creating already. I need help in the integration part, please post me tutorial if you know (Android/NDK/Eclipse/already_existing_C++_code).
You should compile A using the Android toolchain. Note that Android supports not only ARM (a.k.a. armeabi) but also armv7a, x86, mips, and recently - armeabi-v7a-hard. Soon, x86-64 will be released.
You can compile A with Android standalone toolchain, no need to adopt the NDK build system.
You can compile B as part of A, or separately. In the latter case, simply load A before B in your Java static constructor:
{
loadLibrary("A");
loadLibrary("B");
}
because libB.so will have dependencies on libA.so.
You can pack both libA.so and libB.so in the APK (in folders libs/armeabi, libs/x86, etc.)
First of all, I recommend you to read Android NDK documents. Android.mk is not hard to write in order to compile C++ code into shared library for JNI using NDK toolchain. The most difficult part might be that Android libc (bionic) is not the same as ordinary Linux libc.
So, try to compile A - C++ code using NDK toolchain first. If you failed it, you should port it to Android libc, or you should compile it and link statically it using linaro toolchain. Take a look at the documents to link static elf library using NDK toolchain. But the binary wouldn't work on Linux because Android Linux kernel is not the same as linaro.
Anyway if you got to compile a shared library, easy to integrate it to Android project. just put the shared library to libs/[arch], like libs/armeabi-v7a/libfoo.so.
So , I have installed Ubuntu(but still use the Android kernel) on my Android phone with the intention of using it to compile native code for the device using the glibc.
I made a library, which I plan to use it in an app which I want to make, using JNI, but I am using the math library. My question is:
Is there a way to include these libraries into the library that I
created so that my phone can use the glibc library instead of the
bionic(which is what the NDK uses) library?
NOTE: I have tried making a test program that uses the library and statically links to make sure that the compiled code would work on the android OS and not just Ubuntu, and I came to the conclusion that it does :) So any answer pertaining to my question would be a great help.
You can use ar to extract the original .o files from any .a files:
ar x libm.a
You can then select whichever .o files you like and link them into your own code.
.a library files can be viewed as tar files with extra symbol-index features (although I don't know how similar the internals are).
WARNING: in general, you can only use .o files from static libraries in your main application. If you try to link them into a shared library (.so file) you may be out of luck. The .o file needs to have been created with -fPIC for that to work, and this is often not the case for static libraries.
I have a cross-platform C library that I need to compile for Android as a *.so file. The library consist of many .c and .h files, and it use autotools as it's buid system. (./configure && make dep && make). Afaik, the library does not depend on other libraries, other than libc and OpenSSL (which should be present on Andriod).
I'm trying to find the simplest (read fastest in terms or not needing to read hundreds of pages of manuals and then apply try && fail brute-force approaches to complete the task) way of getting the library off my machine in source code form, and into the Android phones as a .so. The library will eventually be accessed from Java's native library interface. For development, I have both Windows and Debian machines on my desk.
If you're lucky and the autotooled project is set up correctly, you can cross-compile by running (this example is cross-compiling for windows using mingw, I do not know what the prefix is for Android):
./configure --host=i586-pc-mingw32
This will then try to find compilers with a prefix of i586-pc-mingw32-, so i586-pc-mingw32-gcc will likely be the first one found and used. For your Android devkit, have a look at what your compiler binary is called and guess the host value from that.
We ended up manually creating an Android NDK project with all the required files.