Search code examples
javaandroidcandroid-ndklame

How to add Lame 3.99.5 to Android Studio using NDK?


I am new in Android NDK. I need to add the Lame library to Android Studio using NDK. I start my research and find out some useful link:

Lame MP3 Encoder compile for Android

How to link the “lame” mp3 encoder shared object to an Android studio project

I successful but only with build the "armeabi". How can I build the other like: "arm64-v8a", "armeabi-v7a", "mips", "mips64", "x86", "x86_64"?

Thank you in advance!


Solution

  • After 3 days getting stuck with the Android Studio + Lame + NDK. I figure out how to do this:

    Step 1:

    Download NDK: http://developer.android.com/ndk/downloads/index.html

    Download Lame library: http://lame.sourceforge.net/download.php

    My Android Studio 1.5.1

    My NDK: android-ndk-r10e (you need to set path in order to use it)

    My Lame library: 3.99.5

    Note: lame library after download may have format .gz instead of .tar.gz. In that case, feel free to change it to .tar.gz and extract it by using 7zip (two times).

    Step 2:

    For example, your Project is "AudioRecorder". Create a folder name "jni" in side it. AudioRecorder/jni.

    Copy all the libmp3lame folder (inside lame 3.99.5) to jni (you should replace its name to lame-3.99.5_libmp3lame).

    Copy lame.h to AudioRecorder/jni/libmp3lame.

    Step 3:

    Create the Android.mk in AudioRecorder/jni:

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LAME_LIBMP3_DIR := lame-3.99.5_libmp3lame
    LOCAL_LDLIBS    := -llog
    LOCAL_MODULE    := mp3lame
    LOCAL_CFLAGS    += -ffast-math -I $(LAME_LIBMP3_DIR)
    LOCAL_SRC_FILES := $(LAME_LIBMP3_DIR)/bitstream.c \
                       $(LAME_LIBMP3_DIR)/fft.c \
                       $(LAME_LIBMP3_DIR)/id3tag.c \
                       $(LAME_LIBMP3_DIR)/mpglib_interface.c \
                       $(LAME_LIBMP3_DIR)/presets.c \
                       $(LAME_LIBMP3_DIR)/quantize.c \
                       $(LAME_LIBMP3_DIR)/reservoir.c \
                       $(LAME_LIBMP3_DIR)/tables.c \
                       $(LAME_LIBMP3_DIR)/util.c \
                       $(LAME_LIBMP3_DIR)/VbrTag.c \
                       $(LAME_LIBMP3_DIR)/encoder.c \
                       $(LAME_LIBMP3_DIR)/gain_analysis.c \
                       $(LAME_LIBMP3_DIR)/lame.c \
                       $(LAME_LIBMP3_DIR)/newmdct.c \
                       $(LAME_LIBMP3_DIR)/psymodel.c \
                       $(LAME_LIBMP3_DIR)/quantize_pvt.c \
                       $(LAME_LIBMP3_DIR)/set_get.c \
                       $(LAME_LIBMP3_DIR)/takehiro.c \
                       $(LAME_LIBMP3_DIR)/vbrquantize.c \
                       $(LAME_LIBMP3_DIR)/version.c \
    include $(BUILD_SHARED_LIBRARY)
    

    Step 4:

    Remove Makefile.am, Makefile.in, logoe.ico, depcomp, lame.rc and i386 directory.

    Step 5:

    Edit file jni/lame-3.99.5_libmp3lame/util.h, and replace definition

    extern ieee754_float32_t fast_log2(ieee754_float32_t x);

    with this

    extern float fast_log2(float x);

    Step 6:

    Move out, press shift + right click on the folder AudioRecorder choose open command line window:

    "ndk-build" (to run NDK build)

    "ndk-build clean" (to clean all built of NDK)

    Step 7:

    You may have some error like this:

    "Cannot recognize <lame.h>"
    

    replace all "#include " to "#include "lame.h""

    "incompatible implicit declaration of built-in function 'xyz'"
    

    add and to any files which have the problems (try to add at the top of the file below the first comment).

    "undefined reference to 'index'"
    

    go to files "id3tag.c" and "machine.h" comment the "#define strchr index"

    Step 8:

    Create the Application.mk in AudioRecorder/jni:

    APP_ABI := all
    

    If there is not Application.mk with the "APP_ABI := all", NDK only build the "armeabi" Add this file the application will build up:

    "arm64-v8a"

    "armeabi"

    "armeabi-v7a"

    "mips"

    "mips64"

    "x86"

    "x86_64"

    Step 9:

    After successfully build NDK, now you should have 2 new folder

    "AudioRecorder/libs"

    "AudioRecorder/obj"

    Create the "jniLibs" in "AudioRecord\app\src\main" and copy all folders in "AudioRecorder/libs" to it.

    Hope it may help. :)