How to change libs directory in Gradle?

浪尽此生 提交于 2019-11-27 10:59:23

问题


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?


回答1:


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.




回答2:


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.




回答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'))
}



回答4:


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.



来源:https://stackoverflow.com/questions/16952388/how-to-change-libs-directory-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!