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.
Related
I have to compile a library (library BPG from Bellard.org) to create a .so or a dll that I can use with android/iOS.
I'm working with Visual Studio. With some researches, I found the project "Visual C++ -> Cross Platform -> Shared Library (Android, iOS)". But I am totally lost and can't do anything.
The downloaded library is organised with some folders but Visual don't allow to make tree, all files are sorted by filters (one for header and one for sources). So I can't build, I have more than 300 errors, "can't open source file", "undefined variable"...
Secondly, the README file from project says :
The following packages need to be installed: mingw64-gcc mingw64-libpng mingw64-libjpeg-turbo mingw64-SDL mingw64-SDL_image yasm
I found installed for mingw 32 bits but no 64 bits so I don't know if build can perform. I don't know how to find the libraries.
So my question is, what is the best way to compile a C/C++ library for android/iOS ? And where can I find a tutorial for beginners ?
Thank you
I have worked as cross compiling engineer for several years. The most suitable IDE for you I think, is the CLion with CMake inside.
CMake is a tool which can cross-compile the C/C++ library into ios\android\linux\etc.. using only one config file: "CMakeList.txt".
The main task of CMake is to translate CMakeList.txt to Makefile on every platform and provide you the .a and .so files.
CLion is very powerful IDE in code editing and debugging.
Furthermore, Android needs JNI (or JNA if performance is not concerned) to wrap your c++ interfaces to java classes. Here I would recommend SWIG. SWIG is a tool to wrap C++ interfaces to other languages, that means, not only java on android you can support , other days your lib can also support python\tcl\Go\etc.
Which os are u using to build the lib? macOS or win ?
For iOS : .a file
For Android: .so file
First you should check the README file
Edit the Makefile to change the compile options (the default
compile options should be OK). Type 'make' to compile and 'make
install' to install the compiled binaries.
Use 'make -j N' where N is the number of CPU cores to compile faster.
The following packages must be installed: SDL-devel
SDL_image-devel yasm. It is recommended to use yasm version >= 1.3.0
to have a faster compilation.
Only a 64 bit target is supported because x265 needs it for bit
depths > 8.
check this to install SDL packages
https://wiki.libsdl.org/Installation
I have a large amount of c++ code needs to be run on Android.
This code compiles with Visual C++ compiler, but it doesn't compile with gcc (which is used by the Android NDK).
The problem is that source contains a lot of pieces what generates error under gcc. Is it possible to compile the source under VC++ and make it run on Android?
Thanks.
The only way to run native C/C++ code in java (and so Android) is with JNI (Java Native Interface).
This is the best tutorial to date that helps you setting JNI with Android Studio and the Android NDK: http://ph0b.com/new-android-studio-ndk-support/
In your case you have to extract the source code of your C++ project (meaning all .cpp ; .h ; .hpp files) and add them in the JNI folder (once you've setup your JNI environment with the aforementioned tutorial (or any other guide). Gradle will compile them as long as you properly include the library.
NB:
You need not a Makefile : instead use the cflags in your Gradle build file ;
Make sure to understand correctly JNI so that you create the proper header files that will link your native code with your java code ;
I hope it helps !
I am trying to learn NDK, and I'd like to use external library (libopus). After reading some articles, I got these steps:
git clone https://android.googlesource.com/platform/external/libopus
mv libopus jni
NDK_PROJECT_PATH=. ndk-build
It crated libs/armeabi/libopus.so file. Yay, awesome! ... And now what? How can I use this library? How can I call its functions and methods?
Also, will my app run on non-ARM architectures (x86, MIPS), because armeabi suggests it'll be ARM only.
You can not just use standard Linux libraries. Java/Android uses the Java Native Interface (JNI) which is special C code that builds the bridge between Java part and native part.
It looks like you already have NDK installed. Look into the project samples, e.g. the "hello-jni" project. In this example you can see what JNI C code you have to write and how to access the self written functions from within your Java code.
Regarding the architectures: Yes, an ARM library is for the ARM platform only. If you want to create a cross-platform App you have to compile all native libraries for each supported platform (usually ARM, ARMv7, x86 and MIPS).
I want to use some function calls(commands) designed for linux. I can use them by enter the key words in adb(Android CML).
Here I found some works some people did.
wget (because it isn't included in most Android device )
Iperf
But after reading their methods or suggestions, I can only understand that I need to use Android NDK and write the correct makefile. I have no idea about building others source code (most of them are C/C++) for linux(only need to use 'make' command mentioned in their README file). The official NDK document is for Java environment to call C lib mainly.
Are there some HOWTO, courses or suggestions for this. Thanks!
I have compiled single cpp file program. I try to compile a alternative version iperf
https://github.com/tierney/iperf
It seems to be relative to lib ,some header files, and multiple c files. I failed to compile by enter all c files normally. Is there anything I missed?
yeah you need the NDK, it offers an C/C++ compiler for Android.
In general the steps are all the same:
Setting up the NDK (I wrote a small how-to, but it's for Arch-Linux and the fish-shell, Windows how-to)
Adjusting your make file (instead of gcc compiler use Android NDK compiler, and so on)
Remember that Android uses Bionic C library, so only use functions supported by it
Run make, push the program to your device and make it executable
Of course, this is just an overview how it is done. You should try it and then ask specific questions if you run into troubles.
NDK is mostly intended to extend the Java apps, but you can download NDK and create a standalone toolchain from it (see http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html). Now you have a cross-compilation environment which is very similar to standard Linux dev environment.
In addition, for small executables and for testing only, you can also cross-compile and link statically to the libc. This way you don't have to worry about Bionic which could be a loss of time.
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.