Android C++ and NOT C - android

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

Related

Fortran Compiler

I am developing an android app on a Mac.
Does anyone know of a fortran compiler for android???
I was going to call some fortran routines from a C routine. Then wrap the C routine with a java class and use the JNI to do this.
BUT, I can't get the fortran code compiled.
Haven't tried it myself, but people have successfully built gfortran for Android. See e.g. http://specificimpulses.blogspot.com/2012/08/something-borrowed-android-fortran.html and http://danilogiulianelli.blogspot.com/2013/02/how-to-build-gcc-fortran-cross-compiler.html .
If the only reason for your C code is to wrap the Fortran code, maybe you could instead just use ISO_C_BINDING (which gfortran supports), and have JNI call that directly (with ISO_C_BINDING you can make code in Fortran that is ABI compatible with C).

Are there any simple way to use c/c++ source code directly on my android application?

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.

compile Java to native Android instead of writing in C

We are looking for a way to protect our code and obfuscation is not enough.
Is it possible to compile Android java code to a native Android library?
Another option would be to write the code in c and connect with it through JNI ourselves. But the code we have is quite elaborate and well tested, a rewrite to c would start the testing all over.
PS: Before we were running on x86 hardware and used Excelsior Jet as a means for protection. As we want to move to more cost-effective (cheaper) Arm/android hardware we are looking for a simular solution as Jet, which alas only compiles to x86.
In theory you should be able to use LLVM. It has:
A Java frontend
A C backend
In principle, this could do what you need. However I imagine you'd need to distribute (in compiled form) lots of the Java class library. All in all, it sounds really hard. And someone who really wants to read the code won't have too much problem with ARM disassembly...

Connecting a C library to iOS app

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.

C++ File adding

Can anyone please tell me how to add a C++ file in to an android project? Is there any method to import classes other than java classes?
The answer is that, you can't really add a C++ file directly to a project, but you can compile it and load it into the code that runs in your process and interface to it using the JNI. This is a way to interface native code to Java. However, be aware that you can't really do that much with the JNI. Getting access to standard Android things like UI, Intents, service connections, etc.., these are all somewhat more difficult to use in native code. And you certainly can't take a UNIX app "off the shelf" and stick it on Android by using the JNI. this is a fairly good looking tutorial on the JNI with Android. However, like I said, using the JNI is not an excuse for learning java and the Android SDK. The main reasons people use native code are for utility code (like crypto stuff) and performance (for example, quite a few Android games use the NDK)..
You have to use android NDK. Just download it and refer from android official site.

Categories

Resources