Prakash
Prakash

Reputation: 372

Error while loading Native Library in Android

I am trying to load native libraries in android studio. My Project build and runs but throws error when trying to load the .so file.

My Project structure is:

enter image description here

And my Gradle File is:

    import com.android.build.gradle.tasks.PackageApplication

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}
apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:22.1.1'
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
/*
    signingConfigs {
        release {
            storeFile file(System.console().readLine("\n\$ Enter keystore path: "))
            storePassword System.console().readLine("\n\$ Enter keystore password: ")
            keyAlias System.console().readLine("\n\$ Enter key alias: ")
            keyPassword System.console().readLine("\n\$ Enter key password: ")
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
*/
    getNdkDirectory()

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 22
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
    buildTypes {
        debug {
            debuggable true
            jniDebuggable true
        }
    }
}



/*
 * JNI Hack for gradle - works alright, copies native libs over into build folder no problem
 * https://gist.github.com/khernyo/4226923
 */

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

tasks.withType(JavaCompile) { options.encoding = "UTF-8" }

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(PackageApplication) { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))
}

I am trying to load the files like:

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

I Receive following error:

java.lang.UnsatisfiedLinkError: Couldn't load Plumble from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/mnt/asec/com.morlunk.mumbleclient-1/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/com.morlunk.mumbleclient-1/lib, /vendor/lib, /system/lib, /system/lib/arm]]]: findLibrary returned null

I tried many refrences but unable to solve the issue. Using Android Studio and Gradle Version: 1.2.3

Upvotes: 1

Views: 2890

Answers (1)

Ilya Polenov
Ilya Polenov

Reputation: 362

Unlike Eclipse with ADT, gradle with Android plugin looks for native libraries in src/main/jniLibs folder by default.

You can change that folder location in the following source set:

android {
    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
    }
}

Upvotes: 1

Related Questions