13

I want to integrate Zbar into my application but cant seem to figure out how to accomplish this using the new Android Studio.

I have looked through the example and have copied over the code without any issues. The problem I am having is adding the libs to my project I cant seem to figure out how to do it. Could someone walk me through it?

Benjamin
  • 3,217
  • 2
  • 27
  • 42
SuNnY_sYeD
  • 513
  • 1
  • 6
  • 19

4 Answers4

22

I'm not particularly familiar with IntelliJ or Gradle but I have figured it out. I used ZBarAndroidSDK-0.2.

  1. Copy the contents of the ZBar SDK libs/ folder into your project's libs/ folder.
  2. Modify your build.gradle (see below) to make sure the jar and native libs are included in your APK.
  3. To make IntelliJ aware of ZBar, add zbar.jar in your project structure. To do this, go to File > Project Structure > Libraries > + Sign > Java and find zbar.jar with the file picker. Add it to your project.

Add the following to your build.gradle (making sure to keep whatever other dependencies you've got):

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/zbar.jar')
}

task copyNativeLibs(type: Copy) {
    from(new File('libs')) { include '**' }
    into new File(buildDir, 'native-libs')
}

tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniDir new File(buildDir, 'native-libs')
}

My build.gradle is based on this gist: https://gist.github.com/khernyo/4226923.

Michael Lawrie
  • 1,534
  • 11
  • 20
  • 13
    With Gradle 0.7.2+ you can put your native libraries directly into `src/main/jniLibs` and it will work automatically. – shkschneider Apr 10 '14 at 09:02
  • @shkschneider could you elaborate on this more? Would you just put the .so files and their folders in the jniLibs/ folder (jniLibs/armeabi/file.so) etc, then put the zbar.jar back in the libs/ folder and add it to the build.gradle file? Thanks – John Shelley Jun 13 '14 at 04:08
  • 4
    @JohnShelley Exactly: `.jar` goes to `libs`, `.so` goes to `jniLibs`. See https://stackoverflow.com/a/22488155/603270 – shkschneider Jun 13 '14 at 15:10
  • Awesome @shkschneider I got it working! However, its only accurate about 50% of the time, when I used it with eclipse it wasn't this bad. Any ideas? – John Shelley Jun 14 '14 at 03:55
  • 1
    Any idea how to get this to working in eclipse. I have `*.so` files saved in libs folder but my application is unable to locate those files. – Raeesaa Jul 04 '14 at 09:14
  • I have code that's similar to this, but I'm not seeing the copied libs folder && files in my project directory. Should you see a jniLibs or native-libs folder in your project source, or only in the build output? Where in the data should you find the .so? – user1743524 Oct 22 '14 at 14:37
17

Actually, @Michael's answer is correct, it is also obsolete. Now, using gradle all you need to do is to add the lines below in the build.gradle file:

android {
...
  sourceSets {
    main.jniLibs.srcDirs = ['libs']
    test.jniLibs.srcDirs = ['libs']
  }
}

or directly put your .so libraries into:

src/main/jniLibs

This way, when you build your application or library, the jni libraries are being copied into destination .jar/.aar file.

Benjamin
  • 3,217
  • 2
  • 27
  • 42
3

If your using gradle 1.1.0 then you must do some modifications to @Michael's answer. Here is the revised code of gradle file which works for me.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile files('libs/zbar.jar')
}
task copyNativeLibs(type: Copy) {
    from(new File('libs')) { include '**' }
    into new File(buildDir, 'native-libs')
}
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn copyNativeLibs }

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}
Nithila
  • 139
  • 1
  • 9
-2

I've built ZBarAndroidSDK-0.2 example in Android Studio 2.0 by just opening CameraTest project from example folder.

It restructured the project from eclipse to Android Studio automatically. That is it.

DeniSHow
  • 1,394
  • 1
  • 18
  • 30