Android NDK

Android NDK



The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

public class MyActivity extends Activity {
  /**
  * Native method implemented in C/C++
  */
  public native void computeFoo();
}


ABI Management


Different Android devices use different CPUs, which in turn support different instruction sets. Each combination of CPU and instruction sets has its own Application Binary Interface, or ABI. The ABI defines, with great precision, how an application's machine code is supposed to interact with the system at runtime. You must specify an ABI for each CPU architecture you want your app to work with.

Each ABI supports one or more instruction sets:
  • armeabi
  • armeabi-v7a
  • arm64-v8a
  • x86
  • x86_64

ABI Management on the Android Platform


Native code in app packages

Both the Play Store and Package Manager expect to find NDK-generated libraries on filepaths inside the APK matching the following pattern:

/lib/abi/libname.so

Here, abi is one of the supported ABIs, and name is the name of the library as you defined it for the LOCAL_MODULE variable in the Android.mk file.
In a fat APK, each library resides under a directory whose name matches a corresponding ABI. For example, a fat APK may contain:

/lib/armeabi/libfoo.so
/lib/armeabi-v7a/libfoo.so
/lib/arm64-v8a/libfoo.so
/lib/x86/libfoo.so
/lib/x86_64/libfoo.so


Android Platform ABI support


he Android system knows at runtime which ABI(s) it supports, because build-specific system properties indicate:

The primary ABI for the device, corresponding to the machine code used in the system image itself.
Optionally, secondary ABIs, corresponding to other ABI that the system image also supports.
This mechanism ensures that the system extracts the best machine code from the package at installation time.

For best performance, you should compile directly for the primary ABI. For example, a typical ARMv5TE-based device would only define the primary ABI: armeabi. By contrast, a typical, ARMv7-based device would define the primary ABI as armeabi-v7a and the secondary one as armeabi, since it can run application native binaries generated for each of them.


References

留言

熱門文章