I am using the latest Android Studio 0.2.6 and the latest ZBar Android SDK. What I have done so far:
- Created a brand new project called QRTest
- Created a folder called libs in my project
- Put the content of the Zbar libs directory into my project folder
- Right clicked the zbar.jar file in my project and Added as a Library
In my build.gradle file, edited it so that it is now:
dependencies { compile 'com.android.support:support-v4:13.0.0' compile files('libs/zbar.jar') }
- Basically copied the example code from the ZBar android SDK example to my own project.
- The project compiles and runs fine (0 errors, 0 warnings), but it crashes upon running. The logs show:
09-01 16:06:02.026: W/dalvikvm(7050): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lnet/sourceforge/zbar/ImageScanner;
09-01 16:06:02.036: E/AndroidRuntime(7050): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]: 209 could not load needed library 'libiconv.so' for 'libzbarjni.so' (load_library[1093]: Library 'libiconv.so' not found)
09-01 16:06:02.036: E/AndroidRuntime(7050): at net.sourceforge.zbar.ImageScanner.(Unknown Source)
Looking at the code in my MainActivity, it appears to be dying here:
/* Instance barcode scanner */
scanner = new ImageScanner();
Which seems to suggest to me that somehow I havent imported my Zbar library files correctly. Any help on how to get this very basic example up and going would be appreciated!
Just to note, if I use Android Studio to import a new project from the ZBar Android SDK example, it compiles and runs fine. However, it is using the old ant build system. I would rather use the new gradle build system, so thats why I'm trying to import ZBar into a fresh Android 0.2.6 project. Thanks!
Another note: I tried modifying my build gradle to the accepted answer here: How to change libs directory in Gradle? and it did not work.
You should add *.so libraries to your project. If you use Android Studio and Gradle it's a little complicated because you need to pack libraries into jar. Check my answer here: ZBar - QR Code Scanner, crashing in Android Studio to see more details.
Solution:
Add zbar.jar into app/libs
Create a folder called "jniLibs"-Java Native Interface (JNI) under src/main and copy folder armeabi-v7a, armeabi, and x86 into src/main/jniLibs.
The file structure like below:
src/main/jniLibs/armeabi-v7a
src/main/jniLibs/armeabi
src/main/jniLibs/x86
- Go to File > Project Structure> app > Dependencies > Add support-v4 into project
来源:https://stackoverflow.com/questions/18556114/android-studio-0-2-6-and-zbar-project-setup