Friday, July 02, 2010

Build Static Library for Android

http://stackoverflow.com/questions/2943828/how-to-compile-a-static-library-using-the-android-ndk

As I understand it, the correct method is to use ndk-build and not invoking the compiler directly.

In Android.mk you need to specify a module for each static library you want to compile, and then specify that your shared library should use it.

Example of a modified Android.mk file of the hello-jni sample project:

LOCAL_PATH := $(call my-dir)  # Define vars for library that will be build statically.

include $
(CLEAR_VARS)

LOCAL_MODULE
:= <module_name>

LOCAL_C_INCLUDES
:= <header_files_path>

LOCAL_SRC_FILES
:= <list_of_src_files> # Optional compiler flags.

LOCAL_LDLIBS
= -lz -lm LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g

include $
(BUILD_STATIC_LIBRARY) # First lib, which will be built statically.
include $
(CLEAR_VARS)
LOCAL_MODULE
:= hello-jni
LOCAL_STATIC_LIBRARIES
:= <module_name>
LOCAL_C_INCLUDES
:= <header_files_path>
LOCAL_SRC_FILES
:= hello-jni.c

include $
(BUILD_SHARED_LIBRARY)

If you want control over which modules to compile when you run ndk-build you can create create a Application.mk file (in the same directory as Android.mk) and list all the modules as in the following example:

APP_MODULES := <module_name_1> <module_name_2> ... <module_name_n> 

3 comments:

  1. As I understand it, the correct method is to use ndk-build and not invoking the compiler directly.

    In Android.mk you need to specify a module for each static library you want to compile, and then specify that your shared library should use it.

    Example of a modified Android.mk file of the hello-jni sample project:

    LOCAL_PATH := $(call my-dir)

    # Define vars for library that will be build statically.
    include $(CLEAR_VARS)
    LOCAL_MODULE :=
    LOCAL_C_INCLUDES :=
    LOCAL_SRC_FILES :=

    # Optional compiler flags.
    LOCAL_LDLIBS = -lz -lm
    LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g

    include $(BUILD_STATIC_LIBRARY)

    # First lib, which will be built statically.
    include $(CLEAR_VARS)
    LOCAL_MODULE := hello-jni
    LOCAL_STATIC_LIBRARIES :=
    LOCAL_C_INCLUDES :=
    LOCAL_SRC_FILES := hello-jni.c

    include $(BUILD_SHARED_LIBRARY)

    If you want control over which modules to compile when you run ndk-build you can create create a Application.mk file (in the same directory as Android.mk) and list all the modules as in the following example:

    APP_MODULES := ...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Anonymous1:33 PM

    I added you to my blog as a friend, does this mean I will not be able to get you anymore if I don't sign up for the Cricut Circle?
    windows 7 key

    ReplyDelete