转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=812

早在2009年google就发布了android NDK,旨在用C/C++来开发android程序。但NDK并不能纯粹用C/C++来使用,而是通过JNI的形式让Java端调用。现在,这一点终于有了改观。在NDK r5上,可以独立用C/C++完成开发了!而且,OpenGL ES,Open SL ES,感应器等平台相关的API也暴露给了native,可以直接调用。对广大游戏开发者不啻为一件美事。

来自于google人员的blog中提到:“Of course, access to the regular Android API still requires Dalvik, and the VM is still present in native applications, operating behind the scenes. Should you need to do more than the NDK interfaces provide, you can always invoke Dalvik methods via JNI. But if you prefer to work exclusively in C++, the NDK r5 will let you build a main loop like this:”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void android_main(struct android_app* state) {
   // Make sure glue isn't stripped.
   app_dummy();

   // loop waiting for stuff to do.
   while (1) {
      // Read all pending events.
      int ident;
      int events;
      struct android_poll_source* source;
      // Read events and draw a frame of animation.
      if ((ident = ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) {
         // Process this event.
         if (source != NULL) {
            source->process(state, source);
         }
      }
      // draw a frame of animation
      bringTheAwesome();
   }
}

这样就能构成android程序的main loop。完整的例子NDK里就有,在http://developer.android.com/reference/android/app/NativeActivity.html。移植到android变得容易一些了。