简体   繁体   中英

Android NDK - write only in C/C++

是否有可能用C / C ++编写一个完整的NDK应用程序而没有像hello-jni示例项目(HelloJni.java)中的Java“启动”类 - 以某种方式创建一个HelloJni.c,它会做同样的事情吗?

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.

Since all UI, graphics and other interesting classes are in Java you will have to use Java one way or another .

By another I mean interfacing with all Java classes through JNI. There is a library for that: https://github.com/DmitrySkiba/itoa-jnipp . Check HelloJNIpp sample - it shows that it is indeed possible to write Android apps completely in C++, the only catch is that you will need to implement all needed wrappers first (some are in samples/common folder).

However, I recommend using Java. Its simple and elegant, has great libraries and tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM