Android NativeActivity
Asked Answered
U

4

12

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?

Urgent answered 7/12, 2010 at 2:36 Comment(0)
H
8

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.

Hexahydrate answered 15/2, 2011 at 0:7 Comment(2)
What if you are creating an OpenGL game that does not need dialogs, and has its UI drawn in 100% OpenGL. Would the use of NativeActivity make more sense?Blister
In that scenario it would make perfect sense.Hexahydrate
D
4

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.

Degrease answered 7/12, 2010 at 2:40 Comment(0)
D
4

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.

Draughtsman answered 7/12, 2010 at 3:36 Comment(0)
K
3

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?

Kendyl answered 7/12, 2010 at 4:42 Comment(2)
Yes the process still has a Dalvik VM in it.Warmblooded
So what does this get you over writing your own thin java wrapper? Seems like the real news, if any, would be a few more public native apis?Kendyl

© 2022 - 2024 — McMap. All rights reserved.