I'm trying to use Tesseract with Android, everything is fine: I compiled the source and I have the .so files ready. Every .so file is in a folder for the different architecture and is correctly inserted in the APK file by Android Studio (If I open the .apk file i can see lib/ folder with all the directories inside + the .so files inside them)
They are named lept.so and tess.so and in the code it uses
System.loadLibrary("lept");
System.loadLibrary("tess");
But when I launch the application I get this exception
1-02 16:01:20.824 16384-16384/x.x.x.ocrapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: x.x.x.ocrapp, PID: 16384
java.lang.UnsatisfiedLinkError: Couldn't load lept from loader dalvik.system.PathClassLoader[dexPath=/data/app/x.x.x.ocrapp-2.apk,libraryPath=/data/app-lib/x.x.x.ocrapp-2]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:44)
at x.x.x.ocrapp.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Which says that it's unable to load lept library.
It seems like it searchs the library inside libraryPath=/data/app-lib/x.x.x.ocrapp-2 app-lib folder, while mine is inside lib folder.
It doesn't work neither in emulator nor in real device. (Samsung Galaxy S4)
How can I fix this exception and let Android load the native library?
They are named lept.so and tess.so
Those are not standard names, as they lack the lib prefix.
System.loadLibrary("lept");
System.loadLibrary("tess");
This causes a search for liblept.so which is not the file you have.
Either give your library the standard name, or specify an actual file name including the path where it ends up installed on the device to System.load() rather than System.loadLibrary().
Related
I have a really simple android app. I want to load a shared library called 'libcamera.so' and then call methods via JNI.
I do not own this library and I do not have access to the source code. It was originally compiled for ARM.
An x86 version is not available.
The project can detect the library and correctly shows it in the jniLibs folder.
Within my main activity I am trying to load the 3rd party library like so:
static {
System.loadLibrary("camera");
}
When this line executes, logcat shows the following:
02-06 14:11:38.517 4455-4455/uk.co.test.myApp E/art: dlopen("/data/app/uk.co.test.myApp-2/lib/x86/libcamera.so", RTLD_LAZY) failed: dlopen failed: "/data/app/uk.co.test.myApp-2/lib/x86/libcamera.so" has unexpected e_machine: 40
02-06 14:11:38.519 4455-4455/uk.co.test.myApp E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.test.myApp, PID: 4455
java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/uk.co.test.myApp-2/lib/x86/libcamera.so" has unexpected e_machine: 40
at java.lang.Runtime.loadLibrary(Runtime.java:371)
at java.lang.System.loadLibrary(System.java:988)
at uk.co.test.myApp.MainActivity.<clinit>(MainActivity.java:17)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1603)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Other 3rd party apps which use this library can run on my x86 device (Epson BT350) so I feel like this should be possible.
I have become aware of something called 'Houdini' which allows ARM apps to run on x86 - I have checked and my device has this library so im confused why its not doing its job. But its also quite likely I dont really understand how Houdini works!
I have tried running on an x86 emulator - same error.
Any ideas would be greatly appreciated.
I solved the problem by forcing my app to only build an armeabi version. I did this by using an abifilter within gradle. I added the following to my defaultConfig
ndk {
abiFilters "armeabi"
}
This will then run on my x86 device because Houdini will kick in and do the translation. Its not ideal because this translation affects performance - but its a start and gets me to the next stage.
i am creating a chat application by using Alljoyn framework and am getting the following error can somebody please help me. The alljoyn jars are already set up!!!
FATAL EXCEPTION: main
Process: com.example.vikesh.chat_application, PID: 4326
java.lang.UnsatisfiedLinkError: Couldn't load alljoyn_java from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.vikesh.chat_application-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.vikesh.chat_application-1, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at com.example.vikesh.chat_application.AlljoynService.<clinit>(AlljoynService.java:1252)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
You need to put the AllJoyn liballjoyn_java.so file within your project.
For example, in Android Studio, the usual location for this file is:
[your_project_dir]/app/src/main/jniLibs/armeabi/
The armeabi portion of the above path is assuming that the AllJoyn .so file that you have previously built is targeted for the 'arm' CPU (the armeabi instruction set).
Note: When I deleted the .so file from my app, I got the same error that you are reporting. When I put the .so file back I still got the same error. I had to uninstall the app from my Android device, and in Android Studio I had to clean and rebuild the app, before I was able to get past this error.
Some other things to consider...
Make sure in your App that you are calling
static {
System.loadLibrary("alljoyn_java");
}
Make sure that the liballjoyn_java.so you previously built matches the cpu instruction set of your Android device. You can verify your Android's instruction set by using an app such as Droid Info, for example. Assuming that it is armeabi, then when you build the AllJoyn core project, you need to specify at a minimum the following switches in your scons build command....
scons BINDINGS="cpp,java,c" OS=android CPU=arm VARIANT=release
Finally, the following Stackoverflow link discusses general reasons the error you are seeing can occur:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader
In my Android Studio project I use a third-party library (which I cannot edit) which requires several native libraries. When I access this third-party library in my app, I get the following error:
java.lang.UnsatisfiedLinkError: Couldn't load avutil-52 from
loader dalvik.system.PathClassLoader
[dexPath=/data/app/com.myApp.debug.apk,
libraryPath=/data/app-lib/com.myApp.debug]:
findLibrary returned null
I use Android Studio 1.2.2, so Gradle supports using a jniLibs folder. My Gradle build file specifies the jniLibs folder, for safety:
android {
sourceSets {
main {
java {
srcDir 'src/main/src'
}
jniLibs {
srcDirs 'src/main/jniLibs'
}
}
}
}
And here I have my libraries:
src/main/jniLibs/armeabi/*.so
The folder includes some other libraries, which are working in the app, so I know the folder is being included.
When I inspect the generated APK, all the *.so files are included in the libs folder. However, when I check the installed app, I only find three of the expected six libraries:
adb shell ls /data/app-lib/com.myApp
first.so
second.so
third.so
I've also checked the /data/data/com/myApp.debug/app_lib/ folder, and none of the other three dependencies are there.
Not being a native Android developer, I find this very puzzling. What could be causing some dependencies to be included, and others to be excluded? Is there a way of forcing Android to recognise the three missing files, and making sure they're installed? Without having to edit the third party library which uses them, of course.
EDIT 1:
Could this code from the third-party library be causing a problem?
public StartFunction() {
String arch = System.getProperty("os.arch");
this.ARM = arch.toUpperCase().contains("ARM") || arch.toUpperCase().contains("AARCH64");
if(this.ARM) {
this.LoadLibraries();
}
}
private void LoadLibraries() {
System.loadLibrary("first");
System.loadLibrary("second");
System.loadLibrary("third");
System.loadLibrary("fourth");
System.loadLibrary("fifth");
System.loadLibrary("sixth");
}
EDIT 2:
This is the full exception from logcat. Package names slightly changed to protect the guilty.
06-30 09:19:08.505 15069-15069/com.myApp.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myApp.debug, PID: 15069
java.lang.UnsatisfiedLinkError: Couldn't load avutil-52 from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.myApp.debug-1.apk,libraryPath=/data/app-lib/com.myApp.debug-1]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at com.thirdPartyApp.embedded.Call.LoadLibraries(Call.java:289)
at com.thirdPartyApp.embedded.Call.<init>(Call.java:214)
at com.thirdPartyApp.embedded.thirdPartyCall$29.run(thirdParty.java:963)
at com.thirdPartyApp.embedded.DownloadFile.onPostExecute(DownloadFile.java:56)
at com.thirdPartyApp.embedded.DownloadFile.onPostExecute(DownloadFile.java:16)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
EDIT 3:
I've managed to get this working, but in a kind of dirty way. I don't know exactly why this happened, but I do know what happened:
I had all six the compiled libraries needed by the third party SDK I'm using placed in src/main/jniLibs/armeabi. Conventional wisdom dictates that this should work for all ARM devices. However, when installing the app on an ARM device only three of the six libraries were installed.
Dirty solution: place all six libraries in the src/main/jniLibs/armeabi-v7a folder as well. This scattershot approach meant that all six libraries are installed, and available.
I'll update this once we figure out why this happened.
I'm developing an Android app which uses a native library called liballjoyn_java.so (available here in the Android Core SDK). I'm using Android Studio as IDE and Maven as build/dependency system (not Gradle). With Android KitKat everything worked like a charm and this is how I added the library to my project:
1) Added the library to my local Maven repo
mvn install:install-file -Dfile=./alljoyn/liballjoyn_java.so -DgroupId=org.alljoyn -DartifactId=liballjoyn_java -Dversion=14.06.00 -Dscope=runtime -Dpackaging=so
2) Defined a dependency in the POM file:
<dependency>
<groupId>org.alljoyn</groupId>
<artifactId>liballjoyn_java</artifactId>
<scope>runtime</scope>
<type>so</type>
<version>14.06.00</version>
</dependency>
3) Called it statically from my code:
static {
try {
System.loadLibrary("alljoyn_java");
Log.d("AllJoynManager", "static - Loaded AllJoyn native library");
} catch (Exception exception) {
Log.d("AllJoynManager", "static - Error loading AllJoyn native library");
exception.printStackTrace();
}
}
This was working perfectly under KitKat in my Nexus 4 phone but now I installed the official Android 5.0 OTA update and I get the following error on runtime:
11-28 17:57:39.988 30068-30068/com.avispalabs.kiihome E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.avispalabs.kiihome, PID: 30068
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.avispalabs.kiihome-2/base.apk"],nativeLibraryDirectories=[/data/app/com.avispalabs.kiihome-2/lib/arm, /vendor/lib, /system/lib]]] couldn't find "liballjoyn_java.so"
at java.lang.Runtime.loadLibrary(Runtime.java:366)
at java.lang.System.loadLibrary(System.java:989)
at com.avispalabs.kiihome.helpers.network.alljoyn.AlljoynManager.<clinit>(AlljoynManager.java:38)
at com.avispalabs.kiihome.ui.activities.MainActivity.<init>(MainActivity.java:38)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-28 17:57:55.432 30068-30068/com.avispalabs.kiihome I/Process﹕ Sending signal. PID: 30068 SIG: 9
I suspect the .so library might have been compiled in way which is somehow incompatible with the new Android 5.0 ART (?). The message that says it can't find the library is probably misleading (the exception is also seen when a library fails to load) but I'm not sure (another possibility is the .so is not correctly extracted or placed).
The library comes precompiled and is advertised to be compatible with JellyBean. I thought previous dynamic libraries would be compatible with new versions of Android, otherwise a lot of apps would break. If I install the same APK in a Nexus 4 with KitKat it just works.
Any advice is highly appreciated.
UPDATE: I have tested my project in a KitKat based device and switched the runtime to ART rather than Dalvik, and the project works fine. This problem seems to be tied to Android 5 rather than ART itself.
Answering my own question here. You can solve it by compiling liballjoyn_java as PIE as explained here:
https://jira.allseenalliance.org/browse/ASACORE-1208
This is a workaround until the AllJoyn guys publish a new Android build. Keep an eye here to get the updated release:
https://build.allseenalliance.org/ci/view/Core%20RB14.12%20SDK/job/branch-android-sdk/
I tested the renderscript on my devices (Nexus 4/5/S, Samsung I747). But when I release the beta to people, I collected crashes from various devices, and they're all ARM chip.
Please see the affected devices:
http://crashes.to/s/b7578ea44e5
This is the error:
android.support.v8.renderscript.o: Error loading RS jni library: java.lang.UnsatisfiedLinkError: Couldn't load RSSupport: findLibrary returned null
at android.support.v8.renderscript.RenderScript.shouldThunk()
at android.support.v8.renderscript.RenderScript.shouldThunk()
at com.xyang.android.timeshutter.model.imageprocessing.BaseTwoPassFilterEdgeDetector.()
at com.xyang.android.timeshutter.model.imageprocessing.SobelEdgeDetector.()
at com.xyang.android.timeshutter.model.BaseBitmapPersistentHelper.getBitmap()
at com.xyang.android.timeshutter.model.BitmapPersistentHelper.cropAndSave()
at com.xyang.android.timeshutter.model.BitmapPersistentHelper.readCompressedImageDataFromUri()
at com.xyang.android.timeshutter.app.capture.FreeCropFragment.onClick()
at android.view.View.performClick(View.java:4100)
at android.view.View$PerformClick.run(View.java:17021)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4788)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:541)
at dalvik.system.NativeStart.main(NativeStart.java)
I know there're some bug for the Intel x86 devices, but why ARM devices also get this link error?
It looks like you didn't include the JNI libraries as part of your application package. Please check that you didn't accidentally strip them. You should have libRSSupport.so, librsjni.so, as well as a librs..so for each user script you have in your application. These should exist under the lib/armeabi-v7a directory for any ARM device. There should also be similar subdirectories for x86/mips.