jueves, 20 de enero de 2011

Compiling libsndfile for Android NDK r5

We've been trying to compile the libsndfile 1.0.23 library for Android and it was a bit tricky. I have described here how we did it.

The first step was to generate the config.h and sndfile.h that are generated by the './configure' script. This is the command line we used is (as described in here):

export NDK=path_to_ndk
export PATH=$PATH:$NDK/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin

./configure \
--host=arm-eabi \
CC=arm-eabi-gcc \
CPPFLAGS="-I $NDK/platforms/android-9/arch-arm/usr/include/" \
CFLAGS="-nostdlib" \
LDFLAGS="-Wl,-rpath-link=$NDK/platforms/android-9/arch-arm/usr/lib/ -L $NDK/platforms/android-9/arch-arm/usr/lib/" \
LIBS="-lc "

The next step is to create a Android.mk file for the library source file. All the source files are copied to a directory along with the Android.mk file. Please note all the files with a 'main' function have been excluded:


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := sndfile
LOCAL_SRC_FILES := mat5.c windows.c G72x/g723_24.c G72x/g72x.c \
G72x/g723_40.c G72x/g721.c G72x/g723_16.c \
float32.c chanmap.c test_endswap.c rf64.c sndfile.c htk.c dither.c \
test_log_printf.c txw.c ms_adpcm.c ima_adpcm.c flac.c aiff.c \
wav.c macbinary3.c mat4.c pcm.c caf.c \
audio_detect.c id3.c alaw.c macos.c file_io.c broadcast.c double64.c \
raw.c test_broadcast_var.c \
g72x.c command.c chunk.c avr.c sd2.c voc.c test_audio_detect.c \
mpc2k.c gsm610.c dwd.c \
interleave.c common.c test_strncpy_crlf.c sds.c pvf.c paf.c au.c \
test_float.c \
vox_adpcm.c ulaw.c strings.c svx.c test_conversions.c rx2.c nist.c \
GSM610/code.c GSM610/gsm_destroy.c \
GSM610/gsm_decode.c GSM610/short_term.c GSM610/gsm_create.c \
GSM610/decode.c GSM610/gsm_option.c \
GSM610/long_term.c GSM610/table.c GSM610/rpe.c GSM610/preprocess.c \
GSM610/gsm_encode.c GSM610/lpc.c \
GSM610/add.c dwvw.c wav_w64.c wve.c ogg.c w64.c test_file_io.c\
ircam.c xi.c ima_oki_adpcm.c
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

This shared library can be compiled as is or can be included in a larger project. For example, if we have the main library in a directory called jni. This library uses the libsndfile. The sndfile code in jni/sndfile the jni/Android.mk file should look like:


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := main
LOCAL_SRC_FILES := main.cpp
LOCAL_SHARED_LIBRARIES := sndfile

include $(BUILD_SHARED_LIBRARY)

include $(LOCAL_PATH)/sndfile/Android.mk

In order to use the libmain.so library from a Java application both libraries has to be included with:


System.loadLibrary ("sndfile");
System.loadLibrary ("main");

No hay comentarios:

Publicar un comentario