I hope there is somebody that can help me with the following.
Currently i have an Android app that is connected to a C library using JNI.
Now i would like to connect my iOS app to that same C library, but how can i call the functions and how can i access the interfaces without JNI?
Hopefully somebody has some good tips or tutorials.
So what i have is an Android App, an iOS App and a C-library adapted to JNI (Java Native Interface). The Android part works. But now i would like to reuse as much as possible but still connect my iOS app to that (or most of that) library. (library are separate header and .c files)
Please help :)
To elaborate slightly on my comment - unlike Android, iOS apps are built using Objective C with Apple's libraries. Objective C is a superset of C, which means that any valid C code is also valid Objective C code. You should be able to add the source files directly to an iOS project in Xcode, and sprinkle library calls anywhere you want. That means there is nothing like the JNI for iOS (or OSX) development.
You have two options:
Directly use your library by adding it to project in xcode, assign a library search path, assign header search path and incude headers in .m objective-c source code that use it.
Add all .c files to be compiled and linked by xcode and incude headers in .m objective-c source code that use it.
p.s. if you have used some particular compiler switches you may need to rebuild your libraries to properly target iOS.
Related
I have c/c++ source code that was built as shared library using AOSP environment. (Using mm command). That has 4 function: add, sub, multiple, divide. Now, I want to call these function from Android Application that uses UI. It means that I want to use native library in Android Application. So my question is do I call these functions directly from Android Application? and if so, How I can do that ?
Thank A lot.
Android Applications are written in Java, or Kotlin, or whatever language that compiles to DEX (the bytecode that runs in Android JVM). You need JNI layer to connect to your C++ library. Hopefully, your language provides native methods that are implemented via JNI.
I am currently working on an android application that evaluate images in different aspects, and I found that there are lots great open source algorithms can be used.
Problem 1: Most of the algorithms are designed on c/c++/matlab languages that cannot be applied directly.
I've search that NDK is a tool that allows us develop android application by other languages, but the setup procedures are quite complicated that I stuck for days. So before I go further on it, I would like to first ask whether I can include other's c/c++ source code directly like calling java library?
Problem 2: For example, I would like to use Point Matching algorithm's source code in my application, but there are lots files inside as it's just source code but not library/plugin. What are the steps to apply the require functions in my android application?
(the most desired way is to blindly input some images to the alogrithm, and it returns the results like calling functions, which I dont have to understand the working principle of it.)
You can't directly use other C++ libraries, you have to build them for Android first using NDK. If there is a version of the library built for Android, then, of course you can use it directly by linking to it using NDK.
You have two options here. First, you create a regular Java application for Android, write a C++ wrapper for handling calls to native side and build the necessary source files using NDK. Your java application will make calls to wrapper using JNI and the wrapper will call your actual C++ functions as in Java->JNI wrapper on C++->Your actual C++ source.
Second option is going fully native, which will leave out Java, JNI calls and the wrapper. You can access your source files directly as if you were writing a desktop C++ application. In the end you will have to build using NDK but this step is required in any case.
As a suggestion, you can also take a look at OpenCV for image processing purposes. It has libraries built for Android, all you will have to do is to link them.
Short version.
Download opencv4android library. Import it in eclipse and see if everything is fine (compile errors, output, etc.).
Secondly, try to import face detection application and try to understand how it works.
It has a java part and a native part.
In order to understand how it works you must understand how java interacts with C++, more or less how NDK works.
One important aspect is to learn how to create your interfaces in C++, based on the native ones defined in java. When you got there then you can try creating your own application.
After that you can come here and ask specific questions.
I'm trying to decide if using C++ to create an Android app is sensible. I've tried to use Java for what I want, but it simply doesn't have the language features I need.
I see a bunch of things written as C/C++, but everyone of them are actually 'C' examples.
I'd like to build a simple C++ app (NOT 'C') to better understand how to do this in Android. I've read some about the NDK which seems to support C++, but I don't know to what extent.
Is there a sample C++ project out there? One that has real C++ and not C code inside a .cpp file? An "Hello World" program would be fine. Simple is good to start.
Please pass by this question if you want to answer with how C++ wraps a 'C' function. If the example doesn't address name mangling, it's not C++. No extern 'C' please. Thanks for understanding.
NDK works really fine. It is so simple that you have your .cpp and .h written in C++, compile them with ndk-build, and you have your android code including the cpp's like explained this tutorial shows:
Good luck:
Part 1
Part 2
Official documentation: Sample applications
I have my own C++ library with source code. It contains functions like this: CreateDvice, FillDevice, CloseDevice and etc. There is no dependency to any third-party libraries.
There is requirement to create application for mobile platform:
Blackberry
iPhone
Android
The application has to use logic provided by this library.
Is there chance to use existing library in mobile application or at least some of them?
Or does it require to re-implement library code for each platform?
For iOS (iPhone/iPad) you can directly compile your library and use it from a regular iOS app written in Objective-C++ and/or C++.
For Android you can directly compile your library using the NDK, then either write your app in Java and call your library via JNI, or write the whole app in C++ using the NDK.
I believe you are out of luck on Blackberry, for this platform you'll need to rewrite your library in Java, as neither apps or libs can be written in C++.
Edit: See my other answer for a completely different approach that may work for you.
Blackberry:
It's technically possible to have ASM on BlackBerry (or Android, iPhone, etc.) but 3rd-party developers are often not allowed (or not able in the case of BlackBerry) to do so.
iPhone:
Absolutely. You can statically link a C++ library. Of course it will have to be compiled with the right instruction set. There are a host of examples out there on how to do this. Translate - you will need the code.
Android:
Absolutely. There is a good book on this by Mark Murphy. Introductory material here:
http://www.androidguys.com/2009/10/14/android-beyond-java-part-one/
Your question is unclear. Do you need a cross-platform library/engine to create a mobile application?
If it is so, Cocos2D would be the best choice. Originally it's a game engine, but it is suitable for applications too. And it supports all the platforms written above.
Instead of compiling your C++ library on each target device that you intend to support, you could opt for creating a service that packs your library. You can install this library on a host you have control, then from each platform the only thing you need to do is invoke this service.
I'm not sure if this thing makes sense for the kind of library that you have, but this would be a way to maintain a single version of your library, and you'll have a guaranteed same behavior on all devices.
Good luck.
Android is not natively Java, it's natively C++. And iOS is also natively C++. So why not just leave the C++ code untouched and drop RIM's current platform (since they are switching to BBX which does support C++ as well).
For Blackberry you can use the C++/Qt Cascades; for iOS you can use C/C++ & Objective-C (a superset of C) and Android can use the C++ NDK. You can use Java on all platforms as long as the Java apps are standalone and the JRE is pre-packaged with the app (iOS). You can interface with C/C++ libs using JNI
If you want to use a Java library on all platforms, that would work.
Android and Blackberry are natively Java.
You can use a tool called XMLVM to cross-compile your Java library to Objective C for use on iOS.
http://xmlvm.org/overview/
It is not 100% perfect, but pretty darn close. We use it extensively to port common Java library code to iOS. Port the C++ library to Java and you are good to go.
I face a problem while developing a native C/C++ shared library for Android platform. As we all know that Android use Java language for the upper layer development. Now I have ported my Engine code using ASCII C/C++ to Android using its bionic library, yet when need to design the User Interface, I have to use the JNI to call my engine code.
As far as I know, that is the only choice. The problem is my engine own hundreds of export APIs. If I use the JNI tech. I need to wrap these APIs to a new shared library for use, which will cost a lot of time for development and testing.
Can somebody give some suggestions for this situation? I am not familiar with java or JNI tech by the way.
TIPS:
When I searched the internet, I found some open source for JNI generator such as JNative etc. Until now I do not know is it suitable for Android platform or not.
You can easily use SWIG www.swig.org which will generate the JNI bindings for you.
there is nothing android-specific in that operation, so it will work rather out-of-the-box.