简体   繁体   中英

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
        }
    }
}

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

Thank you guys for helping but it was a stupid problem. When I imported my .so files under jniLibs , they were named like libSONAME.so . In these lines of code:

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

we should not use System.loadLibrary("libSONAME"); , but just System.loadLibrary("SONAME"); .

Then, just build the project and everything was OK.

Thank you all for helping. I hope this will save time to someone else.

What is your gradle version? If you have a 0.7.3 or a newer version then the next article should help you: Click here !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM