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>
Android NDK: building static library with other static libraries
NDK has the command you need, it's called LOCAL_EXPORT_STATIC_LIBRARIES
.
include $(CLEAR_VARS)
LOCAL_EXPORT_STATIC_LIBRARIES := a-ssl a-crypto
LOCAL_C_INCLUDES += $(PATH_TO_PROJECT)/vendor/android-openssl/openssl/include
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := ../../source.c
include $(BUILD_STATIC_LIBRARY)
You could also inherit the openssl includes path from a-ssl. FInally, to clean this up a bit more, consider changing the order of modules in your Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_EXPORT_STATIC_LIBRARIES := a-ssl a-crypto
LOCAL_MODULE := project-alib
LOCAL_SRC_FILES := ../../source.c
include $(BUILD_STATIC_LIBRARY)
LOCAL_PATH := $(PATH_TO_PROJECT)/vendor/android-openssl
include $(CLEAR_VARS)
LOCAL_MODULE := a-ssl
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/include
LOCAL_SRC_FILES := $(LOCAL_PATH)/prebuilt/$(TARGET_ARCH_ABI)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := a-crypto
LOCAL_SRC_FILES := $(LOCAL_PATH)/prebuilt/$(TARGET_ARCH_ABI)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)
Android, NDK, building static library
You should be able to use ar (arm-linux-androideabi-ar
from the appropriate NDK toolchain) to achieve this:
arm-linux-androideabi-ar -xv lib1.a
arm-linux-androideabi-ar -xv lib2.a
arm-linux-androideabi-ar -xv lib3.a
arm-linux-androideabi-ar -rc libmegalib.a *.o
Build static library with asset_manager using NDK r10e
When you build a static library, you don't need to satisfy the external references, but if somebody uses your library, they must link libandroid.so
. You can put this in documentation of your library.
If you provide an Android.mk with it, you can set LOCAL_EXPORT_LDLIBS = -landroid
. BTW, you can also set LOCAL_EXPORT_INCLUDES
to the directory with public headers for your library.
At any rate, NDK 10 is obsolete. I strongly recommend to move to the current release (unless you desperately need support for android-3).
Build and use static library in Android NDK
I'm guessing C++ name mangling is problem here. Your lodepng.c file provides _lodepng_decode32
symbol (as it is compiled as C code), but your main.cpp file expects something like _lodepng_decode32$asdaASd
symbol (because it is compiled as C++ code).
You should rename lodepng.c
to lodepng.cpp
.
Or you need to put #include "lodepng.h"
in your main.cpp
file inside extern "C" { ... } block
.
Related Topics
Download Multiple Files with a Progress Bar in Listview Android
Error: Execution Failed for Task ':App:Clean'. Unable to Delete File
Asynctask: Where Does the Return Value of Doinbackground() Go
Understanding Android's <Layer-List>
Avoiding Content Type Issues When Downloading a File via Browser on Android
Using Android to Submit to a Google Spreadsheet Form
Broadcast Receiver Class and Registerreceiver Method
Why Getcontext() in Fragment Sometimes Returns Null
Change the Right Margin of a View Programmatically
How to Add a Button to a Preferencescreen
Official "Boost Library" Support for Android and iOS
Camera.Setparameters Failed in Android
How to Capture an Image in Background Without Using the Camera Application
Android:Static Variable Null on Low Memory
Run-As Package 'A.B.C' Is Unknown - Galaxy S4 Jellybean or Android 4.3