Android NDK - write only in C/C++

后端 未结 2 1847
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 05:21

Is there a possible way to write a whole NDK app with C/C++ without a Java \"starter\" class like in hello-jni sample project (HelloJni.java) - somehow create a HelloJni.c which

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 05:40

    Since Android 2.3 (API Level 9) there is the NativeActivity which allows one to code an Android app in C++ only. There is also an example for this in the NDK package.

    A quote from the NDK Overview:

    When to Develop in Native Code

    The NDK will not benefit most applications. As a developer, you need to balance its benefits against its drawbacks; notably, using native code does not result in an automatic performance increase, but always increases application complexity. In general, you should only use native code if it is essential to your application, not just because you prefer to program in C/C++.

    Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. Simply re-coding a method to run in C usually does not result in a large performance increase. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need. The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code.

    The Android framework provides two ways to use native code:

    • Write your application using the Android framework and use JNI to access the APIs provided by the Android NDK. This technique allows you to take advantage of the convenience of the Android framework, but still allows you to write native code when necessary. You can install applications that use native code through the JNI on devices that run Android 1.5 or later.

    • Write a native activity, which allows you to implement the lifecycle callbacks in native code. The Android SDK provides the NativeActivity class, which is a convenience class that notifies your native code of any activity lifecycle callbacks (onCreate(), onPause(), onResume(), etc). You can implement the callbacks in your native code to handle these events when they occur. Applications that use native activities must be run on Android 2.3 (API Level 9) or later.

    You cannot access features such as Services and Content Providers natively, so if you want to use them or any other framework API, you can still write JNI code to do so.

    I would take C/C++ when porting code and possibly when developing cross platform games.

提交回复
热议问题