Home » Android » Cannot load library: reloc_library[1285]: cannot locate 'rand'

Cannot load library: reloc_library[1285]: cannot locate 'rand'

Posted by: admin February 24, 2018 Leave a comment

Questions:

I’m trying to use PJSIP library for my Android application.
I built pjsua sample application according to this manual:
https://trac.pjsip.org/repos/wiki/Getting-Started/Android

But when sample application is launching, exception triggers:

12-06 15:03:58.043: D/dalvikvm(628): Trying to load lib /data/data/org.pjsip.pjsua2.app/lib/libpjsua2.so 0x4129d980
12-06 15:03:58.064: W/dalvikvm(628): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/pjsip/pjsua2/app/MyApp;
12-06 15:03:58.064: D/AndroidRuntime(628): Shutting down VM
12-06 15:03:58.064: W/dalvikvm(628): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-06 15:03:58.083: E/AndroidRuntime(628): FATAL EXCEPTION: main
12-06 15:03:58.083: E/AndroidRuntime(628): java.lang.ExceptionInInitializerError
12-06 15:03:58.083: E/AndroidRuntime(628):  at org.pjsip.pjsua2.app.MainActivity.onCreate(MainActivity.java:85)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.Activity.performCreate(Activity.java:4465)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.os.Looper.loop(Looper.java:137)
12-06 15:03:58.083: E/AndroidRuntime(628):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-06 15:03:58.083: E/AndroidRuntime(628):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 15:03:58.083: E/AndroidRuntime(628):  at java.lang.reflect.Method.invoke(Method.java:511)
12-06 15:03:58.083: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 15:03:58.083: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 15:03:58.083: E/AndroidRuntime(628):  at dalvik.system.NativeStart.main(Native Method)
12-06 15:03:58.083: E/AndroidRuntime(628): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1285]:    37 cannot locate 'rand'...
12-06 15:03:58.083: E/AndroidRuntime(628):  at java.lang.Runtime.loadLibrary(Runtime.java:370)
12-06 15:03:58.083: E/AndroidRuntime(628):  at java.lang.System.loadLibrary(System.java:535)
12-06 15:03:58.083: E/AndroidRuntime(628):  at org.pjsip.pjsua2.app.MyApp.<clinit>(MyApp.java:235)

It seems like appl cannot load libpjsua2.so library. I have never used the NDK before, so i don’t have any ideas about this issue, please help me…

Answers:

This happens if you’ve built your native components with the android-21 target, but are trying to run it on a device with an older Android version. Unless you take very special care, you can’t run binaries built with the android-21 target on older devices. For basic C functions, it shouldn’t matter which target version between android-3 and android-20 you use, it should work on all of them, but if you use android-21 it only works on that version and newer.

See https://stackoverflow.com/a/27093163/3115956 for more details on this issue.

Questions:
Answers:

mstorsjo answers my question. But i want write one comment:
I had correct properties files, but during pjsip building I found following logs:

sip@ubuntu:~/pjsip/trunk$ ./configure-android
configure-android: APP_PLATFORM not specified, using android-21
configure-android: TARGET_ABI not specified, using armeabi

So if you want compile the project on some specific platform version, you have to set APP_PLATFORM parameter for configure-android script.

TARGET_ABI=armeabi-v7a APP_PLATFORM=android-8 ./configure-android --use-ndk-cflags

Questions:
Answers:

I have encountered this when using HUAWEI phone with opus and pngquant jni library for android.
Finally the solution is that I add rand,srand and atof function implementions in my source file. Then the problem gone.

Solution from https://github.com/cocos2d/cocos2d-x/issues/15234
Try it!

static u_long myNextRandom = 1;

double atof(const char *nptr)

{
    return (strtod(nptr, NULL));
}

int rand(void)
{
    return (int)((myNextRandom = (1103515245 * myNextRandom) + 12345) % ((u_long)RAND_MAX + 1));
}

void srand(u_int seed)
{
    myNextRandom = seed;
}