146

I am trying my hands on developing a simple android application in which I am trying to use sqlcipher, which uses .so libraries internally. I have read the documentation on how to use sqlcipher with android app. I have followed the steps and it compiles without any error. But, at runtime it throws UnsatisfiedLinkError.

Googling around for it, I found that, gradle doesn't support .so libraries yet, but I found a hack here which I am trying to use. But it throws compile time error at line #40 on the gist which is,

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

saying

Could not find property 'com' on Project 'MyProject'

Here I am posting code from my build.gradle file.

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/commons-codec.jar')
    compile files('libs/guava-r09.jar')
    compile files('libs/sqlcipher.jar')
}

targetCompatibility = 1.6
sourceCompatibility = 1.6

android {
    target = 'android-14'

    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 16
    }    

}

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

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

clean.dependsOn 'cleanCopyNativeLibs'

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

Can, anybody please help me on what I have done wrong or what should I do to include those .so libraries in my apk?

As I am new to android development and gradle, please apologize me if I have misunderstood something.

0

3 Answers 3

258

I've tried the solution presented in the accepted answer and it did not work for me. I wanted to share what DID work for me as it might help someone else. I've found this solution here.

Basically what you need to do is put your .so files inside a a folder named lib (Note: it is not libs and this is not a mistake). It should be in the same structure it should be in the APK file.

In my case it was:
Project:
|--lib:
|--|--armeabi:
|--|--|--.so files.

So I've made a lib folder and inside it an armeabi folder where I've inserted all the needed .so files. I then zipped the folder into a .zip (the structure inside the zip file is now lib/armeabi/*.so) I renamed the .zip file into armeabi.jar and added the line compile fileTree(dir: 'libs', include: '*.jar') into dependencies {} in the gradle's build file.

This solved my problem in a rather clean way.

24
  • best answer until the support in studio is there
    – dskinner
    Commented Jul 23, 2013 at 20:19
  • 1
    When I updated Android Studio to 0.3.2 (on Mac), the problem occurred. It's a strange problem, this resolution works perfectly.
    – chris
    Commented Nov 4, 2013 at 8:42
  • 12
    after wasting hours on this (stupid) thing, i can state that you SIR DESERVE A BEER!
    – user520288
    Commented Mar 3, 2014 at 1:24
  • 3
    @Richard Did you clean the project after setting it up as described? Commented Apr 2, 2014 at 16:40
  • 2
    This is working for me too but i am getting the error on marshmallow. i.e., "file not fount libtbb.so" Please help me. Commented May 19, 2016 at 9:51
69

To include native libraries you need:

  1. create "jar" file with special structure containing ".so" files;
  2. include that file in dependencies list.

To create jar file, use the following snippet:

task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    extension 'jar'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

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

To include resulting file, paste the following line into "dependencies" section in "build.gradle" file:

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
12
  • 29
    Compile task has been deprecated. Use JavaCompile instead. Commented Sep 20, 2013 at 14:06
  • 2
    I used this answer over Assaf's answer because it makes it an automated process. Additionally, there are many ways to manually make a zip file, and some of those methods introduce unnecessary files.
    – Jeremy
    Commented Sep 26, 2013 at 0:18
  • 3
    This doesn't really work for me because on a clean build (./gradlew clean), native-libs.jar isn't included in the first build after clean (inside the APK). Later builds (if not cleaned) work fine. This is somewhat of a problem for our build server script. Anyone has any solution for this? I'm currently using Gradle (wrapper) 1.8 and "com.android.tools.build:gradle:0.6.3".
    – croc
    Commented Dec 9, 2013 at 11:05
  • 37
    As of Android Gradle 0.7.2 you can put your native libraries in src/main/jniLibs and they'll be packaged correctly. Sample : github.com/commonsguy/sqlcipher-gradle/tree/master/src/main/… Commented Jun 11, 2014 at 6:28
  • 8
    Add following to build.gradle: sourceSets { main { jniLibs.srcDirs = ['libs'] } }
    – herbertD
    Commented Jun 17, 2014 at 3:49
27

I had the same problem. Check out the comment in https://gist.github.com/khernyo/4226923#comment-812526

It says:

for gradle android plugin v0.3 use "com.android.build.gradle.tasks.PackageApplication"

That should fix your problem.

9
  • Works in v0.4 as well, although I had to modify the copy command's "from" to grab the so files out of the project directory instead of the build directory.
    – J c
    Commented May 27, 2013 at 23:54
  • I had to change the project(':MyProject').buildDir to project(':MyProject').getProjectDir() to make it work, using Android studio 0.3.6 and com.android.tools.build:gradle:0.6.+
    – nilsmagnus
    Commented Nov 20, 2013 at 20:06
  • 25
    As of Android Gradle 0.7.2 you can put your native libraries in src/main/jniLibs and they'll be packaged correctly. Sample : github.com/commonsguy/sqlcipher-gradle/tree/master/src/main/… Commented Jun 11, 2014 at 6:28
  • Right answer is stackoverflow.com/questions/16667903/… Commented Jul 31, 2014 at 19:22
  • 2
    I tried this with Android Studio 1.0.1 and gradle version 2.2.1 but it did not work. Any clues?
    – Neo
    Commented Jan 7, 2015 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.