1

I have two projects. One of them is Android app with native code, which just loads dynamically library android_lib.so.

Another project is that library (android_lib.so). Code of this library is simple just for learning how things are going. It has only one function. Here is it:

int calculate (int x, int y)
{
  return (x*x+y*y);
}

In Makefile I use cross-platform compilator from Android 2.2 toolchains (/usr/local/android-ndk-r8/toolchains/arm-linux-androideabi4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc):

NDK_DIR=/usr/local/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
ANDROID_GCC=$(NDK_DIR)/arm-linux-androideabi-gcc
NDK_ROOT=/usr/local/android-ndk-r8

CPPFLAGS = $(NDK_ROOT)/platforms/android-8/arch-arm/usr/include
CFLAGS =-nostdlib
LDFLAGS = -Wl,-rpath-link=$(NDK_ROOT)/platforms/android-8/arch-arm/usr/lib/ -L$(NDK_ROOT)/platforms/android-8/arch-arm/usr/lib/
LIBS=-lc

android_lib.so: calculate.o
    $(ANDROID_GCC) $(CFLAGS)  -shared -o android_lib.so calculate.o 

calculate.o: calculate.c
    $(ANDROID_GCC) $(CPPFLAGS) $(CFLAGS) -c -fPIC calculate.c
clean:
    rm -f *.o *.so 

So I type make and get android_lib.so. In native part of android project (it is mixed Java/C++ Eclipse project) I call dlopen()

dl_handle = dlopen( lib, RTLD_LAZY );
  if (!dl_handle) {
      error = (char *) dlerror();
      if (error != NULL) {
          __android_log_print(ANDROID_LOG_INFO,"nativeCode",error);
          return -1;
      }
      else {
          sprintf(sError,"%s is not found",lib);
          __android_log_print(ANDROID_LOG_INFO,"nativeCode",sError);
          return -2;
      }
  }

It succesfully loads android_lib.so. But if I am trying to call function calculate() I get error.

func = (int (*)(int, int)) dlsym( dl_handle, "calculate" );
  error = (char *) dlerror();
  if (error != NULL) {
      __android_log_print(ANDROID_LOG_INFO,"nativeCode",error);
      return -3;
  }

Function returns -3 and in LogCat of Eclipse I can see

01-01 07:00:44.624: I/nativeCode(8696): Symbol not found:

Please help me to solve this issue.

Update:

$ cd /usr/local/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin 
$ sudo ./arm-linux-androideabi-objdump -T /home/chet/workspace/dinLib/android_lib.so

/home/chet/workspace/dinLib/android_lib.so:     file format elf32-littlearm

DYNAMIC SYMBOL TABLE:
00000234 l    d  .text  00000000 .text
00000274 g    D  *ABS*  00000000 __exidx_end
000012e0 g    D  *ABS*  00000000 _bss_end__
00000234 g    DF .text  00000040 calculate
000012e0 g    D  *ABS*  00000000 __bss_start__
00000274 g    D  *ABS*  00000000 __exidx_start
000012e0 g    D  *ABS*  00000000 __bss_end__
000012e0 g    D  *ABS*  00000000 __bss_start
000012e0 g    D  *ABS*  00000000 __end__
000012e0 g    D  *ABS*  00000000 _edata
000012e0 g    D  *ABS*  00000000 _end
000012e0 g    D  .got   00000000 __data_start
9
  • 1
    Can you show output of "arm-linux-androideabi-objdump.exe -T android_lib.so" (to see if library have the "calculate" symbol)? Also - have you tried using ndk-build instead of inoking gcc manually to build shared library? Does that work? May 28, 2012 at 20:01
  • @Mārtiņš Možeiko I have updated my post with output of this tool. I will try ndk-build. Thanks!
    – Myosotis
    May 29, 2012 at 7:31
  • @Mārtiņš Možeiko I have compiled library using ndk-build. I get that error again(
    – Myosotis
    May 29, 2012 at 10:51
  • 1
    Where are you putting this android_lib.so file on the device? Are you calling System.loadLibrary("android_lib.so") from Java? May 29, 2012 at 15:25
  • android_lib.so is situated in /data/data/<app name>/lib I am loading it with dlopen(). After that, I am calling function with dlsym(). There is code in my question's post above.
    – Myosotis
    May 30, 2012 at 6:43

1 Answer 1

3

Some important rules:

  1. you should use ndk-build instead directly arm-linux-androideabi-gcc
  2. in Android.mk of library type next options: LOCAL_LDFLAGS += -Wl,--export-dynamic
  3. if dlerror() returns error it doesn't mean that dlsym() failed. Check if dlsym() really returns NULL. Because it may return a valid address but dlerror() may return error "Symbol not found" at the same time %)) I had this stupid mistake!!! See code.
  4. You do not need call System.LoadLibrary(your_lib.so). Only dlopen("your_lib.so") from native code with RTLD_NOW or RTLD_LAZY no matter!
  5. Be sure that your_lib.so is situated in /data/data/app_name/libs

    func = (int (*)(int, int)) dlsym( dl_handle, "calculate" );
    error = (char *) dlerror();
    if (error != NULL) { //replace it with if ((func == NULL)&& (error != NULL))
      __android_log_print(ANDROID_LOG_INFO,"nativeCode",error);
      return -3; 
    }
    

That's all :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.