page.title=Using Prebuilt Libraries @jd:body

On this page

  1. Declaring a Prebuilt Library
  2. Referencing the Prebuilt Library from Other Modules
  3. Debugging Prebuilt Libraries
  4. Selecting ABIs for Prebuilt Libraries

The NDK supports the use of prebuilt libraries, both static and shared. There are two principal use cases for this functionality:

This page explains how to use prebuilt libraries.

Declaring a Prebuilt Library

You must declare each prebuilt library you use as a single independent module. To do so, perform the following steps:

  1. Give the module a name. This name does not need to be the same as that of the prebuilt library, itself.
  2. In the module's {@code Android.mk} file, assign to {@code LOCAL_SRC_FILES} the path to the prebuilt library you are providing. Specify the path relative to the value of your {@code LOCAL_PATH} variable.

    Note: You must make sure to select the version of your prebuilt library appropriate to your target ABI. For more information on ensuring library support for ABIs, see Selecting ABIs for Prebuilt Libraries.

  3. Include {@code PREBUILT_SHARED_LIBRARY} or {@code PREBUILT_STATIC_LIBRARY}, depending on whether you are using a shared ({@code .so}) or static ({@code .a}) library.

Here is a trivial example that assumes the prebuilt library {@code libfoo.so} resides in the same directory as the {@code Android.mk} file that describes it.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
include $(PREBUILT_SHARED_LIBRARY)

In this example, the name of the module is the same as that of the prebuilt library.

The build system places a copy of your prebuilt shared library into {@code $PROJECT/obj/local}, and another copy, stripped of debug information, into {@code $PROJECT/libs/}. Here, {@code $PROJECT} is the root directory of your project.

Referencing the Prebuilt Library from Other Modules

To reference a prebuilt library from other modules, specify its name as the value of the {@code LOCAL_STATIC_LIBRARIES} or {@code LOCAL_SHARED_LIBRARIES} variable in the {@code Android.mk} files associated with those other modules.

For example, the description of a module using {@code libfoo.so} might be as follows:

include $(CLEAR_VARS)
LOCAL_MODULE := foo-user
LOCAL_SRC_FILES := foo-user.c
LOCAL_SHARED_LIBRARIES := foo-prebuilt
include $(BUILD_SHARED_LIBRARY)

Here, {@code LOCAL_MODULE} is the name of the module referring to the prebuilt; {@code LOCAL_SHARED_LIBRARIES} is the name of the prebuilt, itself.

Exporting Headers for Prebuilt Libraries

The code in {@code foo-user.c} depends on specific declarations that normally reside in a header file, such as {@code foo.h}, distributed with the prebuilt library. For example, {@code foo-user.c} might have a line like the following:

#include <foo.h>

In such a case, you need to provide the header and its include path to the compiler when you build the {@code foo-user} module. A simple way to accomplish this task is to use exports in the prebuilt module definition. For example, as long as header {@code foo.h} is located under the {@code include} directory associated with the prebuilt module, you can declare it as follows:

include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

The {@code LOCAL_EXPORT_C_INCLUDES} definition here ensures that the build system exports the path to the prebuilt library's {@code include} directory, prepending that path onto the value of the {@code LOCAL_C_INCLUDES} for the module dependent on it.

This operation allows the build system to find the necessary headers.

Debugging Prebuilt Libraries

We recommend that you provide prebuilt shared libraries containing debug symbols. The NDK build system always strips the symbols from the version of the library that it installs into {@code $PROJECT/libs//}, but you can use the debug version for debugging with {@code ndk-gdb}.

Selecting ABIs for Prebuilt Libraries

You must make sure to select the right version of your prebuilt shared library for your targeted ABI. The {@code TARGET_ARCH_ABI} variable in the {@code Android.mk} file can point the build system at the appropriate version of the library.

For example, assume that your project contains two versions of library {@code libfoo.so}:

armeabi/libfoo.so
x86/libfoo.so

The following snippet shows how to use {@code TARGET_ARCH_ABI} so that the build system selects the appropriate version of the library:

include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

If you have specified {@code armeabi} as the value of {@code TARGET_ARCH_ABI}, the build system uses the version of {@code libfoo.so} located in the {@code armeabi} directory. If you have specified {@code x86} as the value {@code TARGET_ARCH_ABI}, the build system uses the version in the {@code x86} directory.