So I am trying to make c++ apps in android, and with now good tutorials I can find how to start from the ground up, I decided to start by learning everything about android its self. This is a good explanation that I have found.
Let me know if this is correct.
So to make an app with c++ you need to use the ndk and jni to interface with java calls, and the native libraries in android were written in c/c++. SO to use the native c/c++ libraries you have to make calls to java in c++???
If you want to write everything in C/C++, use the NativeActivity
there is an official tutorial about this.
But, I don't think that's a good idea to write code in this way unless you are a game developer, and really fluent about everything about OpenGL. Becuase it is much convenient to write the UI part in OpenGL by pure C/C++.
And, in most common cases, we use part of our code by combine JNI and C/C++ to porting C/C++ dependencies to Java space.
Not to use native libraries. But yes, some functionality does require you to call back into Java. The Android framework is written in Java, and not all parts of it are exported to C.
Really I don't suggest writing an entire app in C or C++. I suggest writing only computationally heavy parts in C++, and doing the UI in Java calling down to C++ as needed. It just ends up cleaner that way. THe only time I'd consider writing the GUI in C++ would be if it was pure OpenGL.
Java GUI/system interface with C++ logic is fairly common- it allows you to write a common library between iOS/Android/desktop for the business logic and customize the UI to the platform.
Related
two years ago i developed an Augmented Reality framework on android-7 (Eclair). Since AR application are computationally intensive task, I developed a JNI c++ library used by a Java activity to render and register the virtual environment. The sensor readings acquired in Java are passed to the underline c++ library to compute the registration of the virtual environment. Tridimensional objects are rendered by a native draw function called from a GLSurfaceView. This results in a lot of JNI call.
Now I would like to port the application to android-15(Ice Cream Sandwich). Starting from android-9(Gingerbread) Android allows to use NativeActivity.
I would like to understand which is the better way to develop an AR application. Since every JNI calls introduce an overhead it would be much better to avoid them. Is it possible using NativeActivity? I didn't find an exaustive guide that explains how NativeActivity works but reading this document it seems that it results in a lot of JNI calls anyway. Is there any architectural document that explains how NativeActivity works? Is NativeActivity just a "JNI wrapper" to avoid java code? Concerning performances,are there any advantages using NativeActivity instead of a JNI library as I done before?
Thanks a lot.
NativeActivity will not give a performance boost to your framework. It still uses JNI to communicate with the System, only under the cover.
Moreover, there are good reasons not to use it. If I understand your purpose correctly, you want other applications to take advantage of your code. By forcing them to use NativeActivity you seriously reduce their freedom, and require that they struggle with a less familiar environment. There is a number of limitations with NativeActivity, e.g. it cannot load more than one JNI library.
Finally, I would suggest a completely different direction if you look for optimization of your AR framework: you can use the new setPreviewTexture() API.
As far as I understand it you still are bound to JNI also when using NativeActivity. This class can be used as starting point and encapsulates some functionalities for your convenience but the underlying technology to access native code has not changed and ist still JNI. So in my opinion you only can do some benchmarks to check if NativeActivity is more efficient for some reason (may be the guys at Google do know some hacks that make it faster than your solution).
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.
In order for reuse ability on iOS, I would like to write the logic for my Android game in C++ rather than java. How can I create a C++ class in eclipse and integrate it into my application?
I have read "native C++ code can be used on Android as well using the Native Development Kit (NDK)". What is the latest and greatest way to do this? I am writing a simple OpenGL app? Are there any tutorials out there that people have found useful?
Thanks very much.
Investigate using the JNI and NDK here:
http://developer.android.com/sdk/ndk/index.html
I have written a multi platform 2D engine that runs on a fair amount of platforms. It's possible to do though not relatively easy to implement.
The way I did it was split it up in 2 parts where I used Java for the Activity lifecycle and some additional assist functionality and library encapsulated C++ code for all the rest.
For the C++ I used the JNI where I had two shared libraries. One library held the core logic that ran the entire game and then the other library was a sort of passthrough library with a couple of methods that were called from Java. This way, I could just recompile the core library on each platform without a lot of difficulties and I could rewrite the UI section for each platform. On iOS, I wrote it in Obj-C, on android in Java and on Windows in C/C++.
Is there anyway I could write 100% native C code for Android? I know there are ways to write some C code inside Java code, but I don't know any Java and I hate Java anyway.
Is there anyway I could write pure C code that will run under Android?
There is, as of Android 2.3: NativeActivity. But you don't get access to any of the niceties of Android's Java libraries; you're on your own in the wild west. This is really intended for people writing high-performance games.
Yes, there is support for writing completely native activities. You can check out the native-activity sample application.
I would not recommend this path, though, as in my experience applications that are heavy in NDK code are very difficult to debug. I would rate the Android native debugging experience as lacking.
Edit - one caveat is that you will still be doing plently of Java--just through the JNI.
Might be more trouble than it's worth, you could possibly write your logic in C code and import that to java using extern or external (I forget which now) and then do the GUI in java. There's really no point to using straight C in android unless you want to REALLY optimized your logic. Most of the calls you can make are wrapped anyways so you would have to make those calls to access certain things on an android device. Long story short : not a good idea unless you need faster logic.
You can write most of the application in C around a NativeActivity. However, some Android features can only be used from Java, so you'll need to use JNI instead of pure native. See the official overview.
The Android NDK has just been significantly expanded to include support for writing android applications entirely in native C/C++ code. One can now capture input events on the keyboard and touch screen using native code, and also implement the application lifecycle in C/C++ using the new NativeActivity class.
Given all the expanded native capabilities, would it be worthwhile to completely bypass Java and write Android application in native code?
The NDK is not native per-se. It is to a large extent a JNI wrapper around the Android SDK. Using NativeActivity gives you a convenient way of dealing with certain app-life cycle events, and add your own native code on top. ALooper, AInputQueue etc. are all JNI wrappers of the Java SDK counterparts, some with additional code that is private and unaccessible for real apps.
When it comes to Android development, there's no such thing as writing an application entirely in native C++ - you will (in every real App case that I can think of) always need to use the Android API:s, which are to a huge extent pure Java. Wether you use these through wrappers provided by the NDK or wrappers that you create yourself doesn't really change this.
So, to answer your question: No, it wouldn't be worthwhile, because you would end up writing JNI wrappers for SDK calls instead of writing JNI wrappers to your own Java methods that do the same thing, with less code, simpler code and faster code. For example, showing a dialog using "pure c++", involves quite many JNI calls. Just calling a Java method through JNI that does the same thing will give you faster code (one JNI call), and, arguably, code that is easier to maintain.
To fully understand what you can do, you really must examine the Android source code. Start with native_app_glue.c, which is available in the NDK, then continue with the OS implementation of AActivity, ALooper, AInputQueue etc. Google Code Search is a great help in this. :-)
If it is easy to do in Java, and includes many calls, call a method through JNI that does it all, rather than writing all the extra code to do it with multiple JNI calls. Preserve as much of your existing C++ code as is reasonable.
Not if you are just making a standard application. The Java SDK is more complete than its Native counterpart right now so you would still be making things more difficult for yourself.
If you are not doing something that requires the NDK (read: real time performance sensitive) then stick with Java.
Just some food for thought but if you have an app on iOS and Android, some C/C++ code might be shareable. Obviously the iOS Obj-C and platform specific code wouldn't work elsewhere. (Ditto for the Android specific stuff). But you might be able have some shared code that's platform neutral.
If you can, stick with the java style apps until versions of Android supporting native activities constitute a significant fraction of the installed base.
For things that were hard to do before - particularly ports of existing code - this will probably be a big help.
It's not entirely clear yet what has changed vs. just writing your own thin java wrapper. For example, is there still a copy of the dalvik VM hanging around?