7

We have this app that loads native library, and this library provides native methods for us to call. On some phones (particularly the slower ones), this causes a problem. An UnsatisfiedLinkError occurs whenever (I think) the library is still being "loaded" and a method is called prematurely.

Is there a way to handle this issue? Like checking if a library is already loaded.

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
hadez30
  • 503
  • 5
  • 18
  • Can You post a code how your native library is loaded? I usually use static scope for that. – mar3kk Jan 22 '14 at 14:48
  • The library is loaded from a .jar file. Just to point out, though, the libraries are loaded just fine on some phones and doesn't cause the said problem. Here's the bit of code: static { System.loadLibrary("libraryBeingLoaded"); } – hadez30 Jan 22 '14 at 15:10
  • 1
    Are You sure that Your libraries are compiled for architecture You're running on? What if it only contains ARMEABI v7 version and not ARMEABI? Than it won't work on ARMEABI architecture. – mar3kk Jan 22 '14 at 15:19
  • Ah, thanks for the link. However, we are using Gradle and it's necessary to place it in a ".jar", according to this link (http://stackoverflow.com/questions/16683775/include-so-library-in-apk-in-android-studio). Also, just to add, when we delay the time that the native method is called (like 3 seconds after the app is loaded), it works fine. 3 seconds is arbitrary, so I didn't go for this hack. The iss – hadez30 Jan 22 '14 at 15:27
  • Yes, it is, actually. It does work, I'm certain, as there are libraries provided for that architecture. However, the delay is needed, as far as I've tried. – hadez30 Jan 22 '14 at 15:31

1 Answers1

4

Even on slower devices, the call to System.loadLibrary() is very fast; but if your app runs multithreaded, or if the class that involves the static constructor is only loaded in response to some UI event - then there could be a race condition: some other classes that rely on the native library being present will cause a crash.

There is no prohibition of using the static constructor which calls System.loadLibrary() for all classes that have native methods. You could see some warnings about native library being loaded multiply, but this does not have negative consequences.

Sometimes, we use custom Java code to extract the native libraries to files directory when the app is run for the first time, instead of relying on the system installer, which puts them into the lib directory. This should be done with extra care, and all activity (pun intended) should wait until extraction of the libs (typically from the assets folder of APK) is complete.

Another catch could be that for some devices, the name of your library may be used by a system library. In such case, System.loadLibrary() will load the library from /system/lib and not from your app; but this can hardly explain the effect of 3 sec delay.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Yes, there probably is a race condition. However, the strange thing is, one of the native methods called didn't cause any UnsatisfiedLinkError. And a call to another native method, right after does give this UnsatifiedLinkError. As to your third case, though, it seems that it's not the issue as it does run if there is some delay. I think my problem has to do more with the first case you mentioned. However, I'm unsure about how to proceed. Thanks for the reply, bye the way. – hadez30 Jan 22 '14 at 16:15
  • _one of the native methods called didn't cause any UnsatisfiedLinkError. And a call to another native method, right after does give this UnsatifiedLinkError_ this really sounds inetersting. Please provide the relevant piece of (filtered) `logcat -v threadtime`, which includes a log message just before and immediately after calling each of the native methods. – Alex Cohn Jan 22 '14 at 16:41
  • It seems that the issue was (but still unconfirmed), is the 2nd method(the one causing the UnsatisfiedLinkErro) is being called before the 1st. It goes something like: ApiClass.Start() - which is suppose to start the api ApiClass.DoSomething(). What I think happens is ApiClass.DoSomething() was called first. – hadez30 Jan 23 '14 at 12:54
  • @Harold.Hernandez: I hope you will find why this happens. Anyway, ApiClass static constructor should have done its work by then. – Alex Cohn Jan 23 '14 at 14:02