Is there any way to compile visual C++ code to Android? - android

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 !

Related

Include GStreamer in android ndk project that uses CMake (CMakeLists.txt) and not ndk-build (Android.mk)

I have an android project that currently uses Cmake for including all .cpp /.c code. Now I want to add the GStreamer native libraries such that I can use them in my native code.
But the gstreamer docs https://gstreamer.freedesktop.org/documentation/installing/for-android-development.html
only document using ndk-build to use gstreamer on android.
Now I don't want to totally refactor my project to use ndk-build and Android.mk because
CMake does its job and i never had problems with it
as stated here cmake is the default for android ndk https://developer.android.com/studio/projects/add-native-code
I also need to include the googlevr ndk library that uses cmake.
So I need to figure out a workaround and therefore need your help. Here are some ideas I came up with
Each module can have ether cmake or ndk-build support. Therefore,
I probably could add a new module using ndk-build and include gstreamer there (but then gstreamer is only available in this module)
Compile gstreamer for android using cmake inside android studio - but I don't see evidence that has been done before or is possible for someone without strong cmake knowledge.
Any other ideas/ improvements ? thanks
Even though you have already moved away from GStreamer but in case someone else is facing this issue please have a look at following github repository
https://github.com/henkeldi/gstreamer-android
For myself, I had to make two modifications
I modified the definition of GSTREAMER_ROOT to include ANDROID_ABI so it looks like following
#GStreamer
set(GSTREAMER_ROOT $ENV{GSTREAMER_ROOT_ANDROID}/${ANDROID_ABI})
I had to rename the folders in GStreamer pre-built binaries to be according to ANDROID_ABI. So, my Gstreamer pre-built binaries folder look like following
After the above changes, I am able to successfully build with APK with GStreamer using CMake
At some point I gave up on gstreamer and am now using ffmpeg instead.
There is a nice tutorial for compiling ffmpeg that makes including native ffmpeg inside an android project really easy:
Tutorial on Medium
Github project

Android Studio C++ NDK library example

I would like to download some working example of Android Studio NDK project with C or C++ which will be compiled to .so library (or APK from which I can extract .so).
I have tried ndkbuild with Android.mk build and also CMake with CMakeLists.txt, official and unofficial tutorials on Windows...
If I try them on android from same app (java), it is working but I want to use NDK in Unity3D on android and I keep getting DllNotFoundException.
I uploaded my Android and Unity projects to github.
First of all, here is a sample Android Project plugin you are looking for.
It's very important to know how to do this yourself.
This used to be hard to do before but the latest Android Studio made it easier to now use C/C++ or generate .so library easily. Update Android Studio to the latest version then follow steps below to create a C++ plugin.
1.Create a new Project in Android Studio
2.Check the Include C++ suport to enable C++.
3.On the Dropdown Menu, check C++ 11 on the C++ Standard drop-down menu. You will need C++ 11 to actually use most useful C++ features. Also, enable exception or frtti if you need them.
That's really it.
When you build the plugin:
The debug library is should be at:
\app\build\intermediates\cmake\debug\obj\
The release library is should be at:
\app\build\intermediates\cmake\debug\obj\
If you only see the debug but not the release version of the plugin, check here for how to make it appear.
Possible reasons you are getting DllNotFoundException on Android:
1.You did not wrap the C/C++ function around extern "C". You must do this for each function in the .cpp file or you do it in the function in the .h file.
2.You put the plugin in the wrong Unity folder.
The armeabi-v7a and x86 plugins generated at <ProjectDirectory>\app\build\intermediates\cmake\release\obj should be placed at Assets\Plugins\Android\libs\armeabi-v7a\ and Assets\Plugins\Android\libs\x86\ in the Unity project.
Make sure to spell these correctly. See this for more information about this.
3.You are loading it incorrectly from the C# side.
Let's say that the name of the plugin is libScreenshot.a, do not include the lib prefix, also do not add the .a when loading it.
[DllImport("Screenshot", CallingConvention = CallingConvention.Cdecl)]
public static extern void takeScreenshot();

Android Studio use shared library in NDK code

Basically, I want to know how to use a c++ shared library in Android Studio in NDK code (inside jni part). There is quite a lot of questions about that but they all are based on changing Android.mk which is not a way to go because in AS it's generated automatically.
To use pre built librairies with Android Studio, you have to follow these steps:
Compile your C++ librarie with ndk-build (here you build librairies for Android architecture such like arm, x86, etc.).
Create a Java class and JNI wrapper for your C++ methods.
You have then to disable gradle for ndk-build and create your own Android.mk and Application.mk in the jni folder.
Import the .so library and pre build it in the Android.mk.
Invoke ndk-build manually, from the Android Studio console.
Then, include the header of your librarie and call its functions in your JNI part.
For any complement of information, I advise you the intel video that you can find here.

Need directions to integrate already existing c++ code to 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.

How to add C++ Library ( .a) file into C++/Java Hybrid Project

What I would like to do is to add C++ Library (.a) file into my C++ / Java Hybrid Project.
Actually, It's Android Project. I'm using C++ for native calls.
The problem is I can't find any "Tool Settings" under "C++ Build/
Settings" to add ".a" library file into the project.
I think it is because of the nature or type of the Project. (As my project is Android Project.) Any points would be much obliged.
I have .h file and .a file. I can include ".h" file. But, when I invoke the method, the compiler said "no such method".
I would like to add C++ Library (.a) file into Android Project. ( C++ / Java Hybrid Project.)
PS: I know Eclipse is poor in C++ Code Analysis. I have already turn off the Code Analysis in the project. So, It's obvious that the error is not C++ Code Analysis.
Check this link should help.
http://www.cmumobileapps.com/2011/08/31/compiling-open-source-libraries-with-android-ndk-part-1/
This explains how to compile c/c++ source to produce static library, and then use this static library.

Categories

Resources