System.loadLibrary in Android Studio

Jordi Sipkens picture Jordi Sipkens · Jan 27, 2015 · Viewed 13.6k times · Source

I'm currently trying to load a library in my project which I need to check a certain license I'm using. However when executing it just gives an error on the system.loadLibrary.

I'm using Android Studio and I've got the library placed in my libs folder and I already added it into my dependencies.

Code :

@Override
public void onCreate() {
    Log.v( TAG, "onCreate()" );
    super.onCreate();

    //Gives the error.
    System.loadLibrary( "MobileImagingEngine" );
    try {
        Log.v( TAG, "MILicenser.getVersionInfo(): " + MILicenser.getVersionInfo() );
        final InputStream licenseStream = getAssets().open( "thelicense.License" );
        MILicenser.setLicense( licenseStream, "Android_ID" );
        Log.v( TAG, "MILicenser.setLicense() succeeded. " + MILicenser.getLicenseInfo() );
    } catch( final Exception exception ) {
        Log.e( TAG, "MILicenser.setLicense() failed", exception );
    }
    MIContext.createInstance();
}

Log :

Process: docspro.nl.docsproscanapp, PID: 10348
java.lang.UnsatisfiedLinkError: Couldn't load MobileImagingEngine from loader dalvik.system.PathClassLoader[dexPath=/data/app/docspro.nl.docsproscanapp-8.apk,libraryPath=/data/app-lib/docspro.nl.docsproscanapp-8]: findLibrary returned null
        at java.lang.Runtime.loadLibrary(Runtime.java:358)
        at java.lang.System.loadLibrary(System.java:526)
        at docspro.nl.docsproscanapp.MIApplication.onCreate(MIApplication.java:24)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4581)
        at android.app.ActivityThread.access$1600(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

Gradle file (Tried a solution from stack):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "project.name"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('lib/armeabi-v7a/libMobileImagingEngine.jar')
    compile files('libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar')
    compile files('libs/MobileImagingEngine.jar')
    compile files('libs/MobileImagingEngine_Doc.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'))
}

Answer

WearingStorm picture WearingStorm · Feb 11, 2015

Was stuck on this problem couple of days ago. Just had to add a folder called "jniLibs" in my applications folder and Android Studio tok care of the rest.

For more info on using native libs http://ph0b.com/android-studio-gradle-and-ndk-integration/