Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - include and consume .so library

I need to use some native libraries(.so) in my android project. According to some answers here in StackOverflow about this topic, I created a jniLibs folder in app/src/main and put there my files:

armeabi/my_lib.so
armeabi-v7a/my_lib.so
x86/my_lib.so

Then, in my activity class I use:

static {
        System.loadLibrary("my_lib");
    }

But when I run the app, an UnsatisfiedLinkError exception is generated. If this is important to be noticed, I don't have an Android.mk file, and I haven't changed anything that has to do with this in my gradle files. So, the only think I did is to copy-paste my .so files in jniLibs and to write the code above in my activity. So what might be the cause of this problem? Am I missing something?

EDIT

This is my gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 15
    buildToolsVersion "23.0.3"

    compileOptions.encoding = 'ISO-8859-1'

    defaultConfig {
        applicationId "my.package"
        minSdkVersion 4
        targetSdkVersion 4

        ndk {
            moduleName "my_so_lib"
        }
    }

    sourceSets {
        main {
            jni.srcDirs = ["libs"]
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            debuggable true
        }
    }


    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a', 'mips', 'armeabi'
            universalApk false
        }
    }
}
like image 237
user19922 Avatar asked Dec 15 '22 06:12

user19922


1 Answers

Solution 1

Create Folder "jniLibs" inside "src/main/" Put all your .so libraries inside "src/main/jniLibs" folder Folder structure looks like :

|--app: 
|--|--src: 
|--|--|--main 
|--|--|--|--jniLibs 
|--|--|--|--|--armeabi 
|--|--|--|--|--|--.so Files 

Can you please confirm that you have this hierarchy ?

No extra code requires just sync your project and run your application.

Reference https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main

Solution 2

Add both code snippets in your module gradle.build file as a dependency:

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

How to create this custom jar:

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

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

source

like image 142
Guillaume agis Avatar answered Dec 17 '22 02:12

Guillaume agis