Android build x86 ndk

ndk-build

On this page

The ndk-build file is a shell script introduced in Android NDK r4. Its purpose is to invoke the right NDK build script.

Internals

Running the ndk-build script is equivalent to running the following command:

$GNUMAKE points to GNU Make 3.81 or later, and points to your NDK installation directory. You can use this information to invoke ndk-build from other shell scripts, or even your own make files.

Invoking from the Command Line

The ndk-build file lives in the top level the NDK installation directory. To run it from the command line, invoke it while in or under your application project directory. For example:

In this example,

points to your project’s root directory, and is the directory where you installed the NDK.

Options

All parameters to ndk-build are passed directly to the underlying GNU make command that runs the NDK build scripts. Combine ndk-build and options in the form ndk-build . For example:

The following options are available:

clean Remove any previously generated binaries. V=1 Launch build, and display build commands. -B Force a complete rebuild. -B V=1 Force a complete rebuild, and display build commands. NDK_LOG=1 Display internal NDK log messages (used for debugging the NDK itself). NDK_DEBUG=1 Force a debuggable build (see Table 1). NDK_DEBUG=0 Force a release build (see Table 1). NDK_HOST_32BIT=1 Always use the toolchain in 32-bit mode (see 64-bit and 32-bit Toolchains). NDK_APPLICATION_MK= Build, using a specific Application.mk file pointed to by the NDK_APPLICATION_MK variable. -C

Build the native code for the project path located at

. Useful if you don’t want to cd to it in your terminal.

Invoking from Eclipse

To build from Eclipse, make sure that you have configured it as described in Setup. If you wish to build using the default ndk-build command, with no options, you can just build your project just as you would any Android project. To get Eclipse to add any of the options described above, follow these steps:

  1. In the Project Explorer pane, right-click your project name.
  2. Select Properties.
  3. Click C/C++ Build.
  4. Under the Builder Settings tab, uncheck Use default build command.
  5. In the Build command field, enter the entire build string as if you were typing it on the command line.
  6. Click OK.

Figure 1 shows an example of an entered string.

Figure 1. Specifying a debug build from within Eclipse

Debuggable versus Release builds

Use the NDK_DEBUG option and, in certain cases, AndroidManifest.xml to specify debug or release build, optimization-related behavior, and inclusion of symbols. Table 1 shows the results of each possible combination of settings.

Table 1. Results of NDK_DEBUG (command line) and android:debuggable (manifest) combinations.

NDK_DEBUG=0 NDK_DEBUG=1 NDK_DEBUG not specified
android:debuggble=»true» Debug; Symbols; Optimized*1 Debug; Symbols; Not optimized*2 (same as NDK_DEBUG=1)
android:debuggable=»false» Release; Symbols; Optimized Release; Symbols; Not optimized Release; No symbols; Optimized*3

*1: Useful for profiling.
*2: Default for running ndk-gdb .
*3: Default mode.

Note: NDK_DEBUG=0 is the equivalent of APP_OPTIM=release , and complies with the GCC -O2 option. NDK_DEBUG=1 is the equivalent of APP_OPTIM=debug in Application.mk , and complies with the GCC -O0 option. For more information about APP_OPTIM , see Application.mk.

The syntax on the command line is, for example:

If you are using build tools from prior to SDK r8, you must also modify your AndroidManifest.xml file to specify debug mode. The syntax for doing so resembles the following:

From SDK r8 onward, you do not need to touch AndroidManifest.xml . Building a debug package (e.g. with ant debug or the corresponding option of the ADT plugin) causes the tool automatically to pick the native debug files generated with NDK_DEBUG=1 .

Читайте также:  Excellent bracelet app android

64-Bit and 32-Bit Toolchains

Some toolchains come with both 64-bit and 32-bit versions. For example, directories /toolchain/ /prebuilt/ and /prebuilt/ may contain both linux-x86 and linux-x86_64 folders for Linux tools in 32-bit and 64-bit modes, respectively. The ndk-build script automatically chooses a 64-bit version of the toolchain if the host OS supports it. You can force the use of a 32-bit toolchain by using NDK_HOST_32BIT=1 either in your environment or on the ndk-build command line.

Note that 64-bit tools utilize host resources better (for instance, they are faster, and handle larger programs), and they can still generate 32-bit binaries for Android.

Requirements

You need GNU Make 3.81 or later to use ndk-build or the NDK in general. The build scripts will detect a non-compliant Make tool, and generate an error message.

If you have GNU Make 3.81 installed, but the default make command doesn’t launch it, define GNUMAKE in your environment to point to it before launching ndk-build. For example:

You can override other host prebuilt tools in $NDK/prebuilt/ /bin/ with the following environment variables:

Источник

Android ABIs

Different Android devices use different CPUs, which in turn support different instruction sets. Each combination of CPU and instruction set has its own Application Binary Interface (ABI). An ABI includes the following information:

  • The CPU instruction set (and extensions) that can be used.
  • The endianness of memory stores and loads at runtime. Android is always little-endian.
  • Conventions for passing data between applications and the system, including alignment constraints, and how the system uses the stack and registers when it calls functions.
  • The format of executable binaries, such as programs and shared libraries, and the types of content they support. Android always uses ELF. For more information, see ELF System V Application Binary Interface.
  • How C++ names are mangled. For more information, see Generic/Itanium C++ ABI.

This page enumerates the ABIs that the NDK supports, and provides information about how each ABI works.

ABI can also refer to the native API supported by the platform. For a list of those kinds of ABI issues affecting 32-bit systems, see 32-bit ABI bugs.

Supported ABIs

Table 1. ABIs and supported instruction sets.

ABI Supported Instruction Sets Notes
armeabi-v7a
  • armeabi
  • Thumb-2
  • VFPv3-D16
  • Incompatible with ARMv5/v6 devices.
    arm64-v8a
  • AArch64
  • x86
  • x86 (IA-32)
  • MMX
  • SSE/2/3
  • SSSE3
  • No support for MOVBE or SSE4.
    x86_64
  • x86-64
  • MMX
  • SSE/2/3
  • SSSE3
  • SSE4.1, 4.2
  • POPCNT
  • Note: Historically the NDK supported ARMv5 (armeabi), and 32-bit and 64-bit MIPS, but support for these ABIs was removed in NDK r17.

    armeabi-v7a

    This ABI is for 32-bit ARM-based CPUs. The Android variant includes Thumb-2 and the VFP hardware floating point instructions, specifically VFPv3-D16, which includes 16 dedicated 64-bit floating point registers.

    For information about the parts of the ABI that aren’t Android-specific, see Application Binary Interface (ABI) for the ARM Architecture

    The NDK’s build systems generate Thumb-2 code by default unless you use LOCAL_ARM_MODE in your Android.mk for ndk-build or ANDROID_ARM_MODE when configuring CMake.

    Other extensions including Advanced SIMD (Neon) and VFPv3-D32 are optional. For more information, see Neon Support.

    The armeabi-v7a ABI uses -mfloat-abi=softfp to enforce the rule that, although system can execute floating-point code, the compiler must pass all float values in integer registers and all double values in integer register pairs when making function calls.

    arm64-v8a

    This ABI is for ARMv8-A based CPUs, which support the 64-bit AArch64 architecture. It includes the Advanced SIMD (Neon) architecture extensions.

    You can use Neon intrinsics in C and C++ code to take advantage of the Advanced SIMD extension. The Neon Programmer’s Guide for Armv8-A provides more information about Neon intrinsics and Neon programming in general.

    See Arm’s Learn the Architecture for complete details of the parts of the ABI that aren’t Android-specific. Arm also offers some porting advice in 64-bit Android Development.

    On Android, the platform-specific x18 register is reserved for ShadowCallStack and should not be touched by your code. Current versions of Clang default to using the -ffixed-x18 option on Android, so unless you have hand-written assembler (or a very old compiler) you shouldn’t need to worry about this.

    This ABI is for CPUs supporting the instruction set commonly known as «x86», «i386», or «IA-32». Characteristics of this ABI include:

      Instructions normally generated by GCC with compiler flags such as the following:

    These flags target the Pentium Pro instruction set, along with the the MMX, SSE, SSE2, SSE3, and SSSE3 instruction set extensions. The generated code is an optimization balanced across the top Intel 32-bit CPUs.

    For more information on compiler flags, particularly related to performance optimization, refer to GCC x86 Performance Hints.

  • Use of the standard Linux x86 32-bit calling convention, as opposed to the one for SVR. For more information, see section 6, «Register Usage», of Calling conventions for different C++ compilers and operating systems.
  • The ABI does not include any other optional IA-32 instruction set extensions, such as:

    You can still use these extensions, as long as you use runtime feature-probing to enable them, and provide fallbacks for devices that do not support them.

    The NDK toolchain assumes 16-byte stack alignment before a function call. The default tools and options enforce this rule. If you are writing assembly code, you must make sure to maintain stack alignment, and ensure that other compilers also obey this rule.

    Refer to the following documents for more details:

    x86_64

    This ABI is for CPUs supporting the instruction set commonly referred to as «x86-64.» It supports instructions that GCC typically generates with the following compiler flags:

    These flags target the x86-64 instruction set, according to the GCC documentation. along with the MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, and POPCNT instruction-set extensions. The generated code is an optimization balanced across the top Intel 64-bit CPUs.

    For more information on compiler flags, particularly related to performance optimization, refer to GCC x86 Performance Hints.

    This ABI does not include any other optional x86-64 instruction set extensions, such as:

    You can still use these extensions, as long as you use runtime feature probing to enable them, and provide fallbacks for devices that do not support them.

    Refer to the following documents for more details:

    Generate code for a specific ABI

    Gradle

    Gradle (whether used via Android Studio or from the command line) builds for all non-deprecated ABIs by default. To restrict the set of ABIs that your application supports, use abiFilters . For example, to build for only 64-bit ABIs, set the following configuration in your build.gradle :

    ndk-build

    ndk-build builds for all non-deprecated ABIs by default. You can target a specific ABIs by setting APP_ABI in your Application.mk file. The following snippet shows a few examples of using APP_ABI :

    For more information on the values you can specify for APP_ABI , see Application.mk.

    CMake

    With CMake, you build for a single ABI at a time and must specify your ABI explicitly. You do this with the ANDROID_ABI variable, which must be specified on the command line (cannot be set in your CMakeLists.txt). For example:

    For the other flags that must be passed to CMake to build with the NDK, see the CMake guide.

    The default behavior of the build system is to include the binaries for each ABI in a single APK, also known as a fat APK. A fat APK is significantly larger than one containing only the binaries for a single ABI; the tradeoff is gaining wider compatibility, but at the expense of a larger APK. It is strongly recommended that you take advantage of either App Bundles or APK Splits to reduce the size of your APKs while still maintaining maximum device compatibility.

    At installation time, the package manager unpacks only the most appropriate machine code for the target device. For details, see Automatic extraction of native code at install time.

    ABI management on the Android platform

    This section provides details about how the Android platform manages native code in APKs.

    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:

    Here, is one of the ABI names listed under Supported ABIs, and is the name of the library as you defined it for the LOCAL_MODULE variable in the Android.mk file. Since APK files are just zip files, it is trivial to open them and confirm that the shared native libraries are where they belong.

    If the system does not find the native shared libraries where it expects them, it cannot use them. In such a case, the app itself has to copy the libraries over, and then perform dlopen() .

    In a fat APK, each library resides under a directory whose name matches a corresponding ABI. For example, a fat APK may contain:

    Note: ARMv7-based Android devices running 4.0.3 or earlier install native libraries from the armeabi directory instead of the armeabi-v7a directory if both directories exist. This is because /lib/armeabi/ comes after /lib/armeabi-v7a/ in the APK. This issue is fixed from 4.0.4.

    Android platform ABI support

    The 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.

    64-bit devices also support their 32-bit variants. Using arm64-v8a devices as an example, the device can also run armeabi and armeabi-v7a code. Note, however, that your application will perform much better on 64-bit devices if it targets arm64-v8a rather than relying on the device running the armeabi-v7a version of your application.

    Many x86-based devices can also run armeabi-v7a and armeabi NDK binaries. For such devices, the primary ABI would be x86 , and the second one, armeabi-v7a .

    You can force install an apk for a specific ABI. This is useful for testing. Use the following command:

    Automatic extraction of native code at install time

    When installing an application, the package manager service scans the APK, and looks for any shared libraries of the form:

    If none is found, and you have defined a secondary ABI, the service scans for shared libraries of the form:

    When it finds the libraries that it’s looking for, the package manager copies them to /lib/lib .so , under the application’s native library directory ( / ). The following snippets retrieve the nativeLibraryDir :

    Kotlin

    If there is no shared-object file at all, the application builds and installs, but crashes at runtime.

    Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

    Источник

    Читайте также:  Guitar hero для андроида
    Оцените статью