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.
Related
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.
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();
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'm trying to integrate this specific library to my Android project, and the library is written in C/C++. I've miraculously gotten ndk-build to give me the needed .so file.
However, looking at it, there's a sample in the project, and they use a mysterious .jar with the API bindings of the .c/c++ files.
How do i either
create this special .jar file that has the API, based on the .so?
OR
directly add a method to the main c++ file and then call it from Java?
I've tried to re-wrap things using JNI, but it definitely doesn't seem to work. i keep getting UnsatisfiedLinkError.
A lot of the documentation online uses jni as the tutorial. i'm happy with just a few links to tutorials on JNA.
JNA provides a stub native library, libjnidispatch.so for a variety of platforms. You can build this library yourself, or extract one of the pre-built binaries from the project's lib/native/<platform>.jar packages.
You include libjnidispatch.so in your Android project the way you would any other JNI library. This is required; you cannot rely on JNA to dynamically unpack and use its native library automatically like on other platforms. The JNA project includes details for doing so (as well as instructions for building libjnidispatch.so yourself).
You then use jna.jar as you would any other Java jar file, and write your own (Java) mappings to match the native library you're trying to access. There's also a jna-min.jar which omits all the native platform libraries that are normally bundled in jna.jar.
Do go to project properties and build paths and remove JNA 4.0 and related classes.
This will work!
I have read the overview for Android Library project. I have in mind to develop an Android project library containing native libraries and JNI wrapper which would be included into the Android project. However, the doc does not explicitly mention if Android library project can include native libraries.
Can anyone confirm/infirm support for native libraries in Android library projects ?
As Android Document said:
An Android library project is a development project that holds shared
Android source code and resources.
An Android Library Project, in fact, isn't so different from normal Android project. You can make a normal android project as library project, except asset folder. They're just different when you declare in Eclipse Buid Path or something similar in other IDEs.
Native support simply, just a call to native layer (as in your post, Native Library) such as C/C++ library file (*.so file). This function is not specific to Android, but in normal Java project also support this feature: Calling functions from another language inside Java.
So, in short answer, yes :)