AnthonyS
AnthonyS

Reputation: 103

Can't build against functions in system library

I'm going crazy here.

Basically, I'm trying to access some functions in a shared library located in system/lib/ on the android platform, using the NDK.

The library I'm trying to make use of is libsonivox.so. My main goal is to use it from a NativeActivity. I understand this can complicate things because I would need to load this library (statically) before I load my own library which depends on it. Therefore I am trying to get it to work through a regular Activity and JNI.

In the Activity I load the libraries like so:

static {
  System.loadLibrary("sonivox");
  System.loadLibrary("native-audio-jni");
}

The "native-audio-jni" library is from the NDK samples, but I am modifying it in a rudimentary attempt to access the sonivox functions.

Without any calls to libsonivox from libnative-audio-jni, everything compiles fine. This line from the LogCat output gives me hope:

  04-26 15:01:14.973: D/dalvikvm(691): No JNI_OnLoad found in /system/lib/libsonivox.so   
  0x412a1100, skipping init

So the library is loaded.

Then I add this function to native-audio-jni.c:

void Java_jay_enn_eye_JNImidiActivity_createMidi(JNIEnv* env,
        jclass clazz)
{
  pLibConfig = EAS_Config();
}

pLibConfig is declared like this:

static const S_EAS_LIB_CONFIG* pLibConfig = NULL;

When that is declared, without the addition of the above function, it compiles fine. So at least the header files are... there.

When I plop that function into the code, this is the output of ndk-build:

  Compile thumb  : native-audio-jni <= native-audio-jni.c
  SharedLibrary  : libnative-audio-jni.so
  ./obj/local/armeabi/objs/native-audio-jni/native-audio-jni.o: In function
  `Java_jay_enn_eye_JNImidiActivity_createMidi':
  /home/anthony/Documents/eclipse/JNImidi/jni/native-audio-jni.c:202: undefined   
  reference
  to `EAS_Config'
  collect2: ld returned 1 exit status
  make: *** [obj/local/armeabi/libnative-audio-jni.so] Error 1

I'm not sure if libnative-audio-jni just can't access libsonivox, or if I need to dlsym() or sym() link the sonivox functions to use them. I haven't been able to try either of those since the library is found in system/lib/ and not provided by me, so I don't have a full path to provide to do that.

Another option I am considering is grabbing libsonivox.so, copying it into a directory in the project, and changing the Android.mk so that it includes the lib as a prebuilt shared library. I was thinking maybe the library needs to be included at compile time.

EDIT: Here's the Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native-audio-jni
LOCAL_SRC_FILES := native-audio-jni.c
# for native audio
LOCAL_LDLIBS    += -lOpenSLES
# for logging
LOCAL_LDLIBS    += -llog
# for native asset manager
LOCAL_LDLIBS    += -landroid

include $(BUILD_SHARED_LIBRARY)

This must be where something isn't right. I'd think the sonivox library would need to be present when compiling everything. However, the library won't load no matter what changes I make to this file, which have mainly involved trying to include it as a PREBUILT_SHARED_LIBRARY or PREBUILT_STATIC_LIBRARY. Also, the OpenSL stuff works perfectly fine. I wish sonivox would work if I were to just include it as -lsonivox, but no.

Upvotes: 3

Views: 2360

Answers (1)

AnthonyS
AnthonyS

Reputation: 103

I got carried away with other things, but I figured out the answer to this recently...
libsonivox.so can be loaded dynamically. You need to copy the .so from your android device and place it in $NDK-ROOT/platforms/android-14/arch-arm/usr/lib, "android-14" can be whatever version instead.
That way it can be compiled against, and when the app is running on a device it uses the library on that device.

Upvotes: 2

Related Questions