Ehsan
Ehsan

Reputation: 2781

Use .So file in android studio

I'm developing an application for a device that has an SDK for itself.

When I try to import its SDK as a library in my application I will get an error like below:

E/MultiWindowProxy: getServiceInstance failed!
E/MultiWindowProxy: getServiceInstance failed!
E/AndroidRuntime: FATAL EXCEPTION: Thread-571
    Process: cards.mena.app, PID: 9484
    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/cards.mena.app-2/base.apk"],nativeLibraryDirectories=[/data/app/cards.mena.app-2/lib/arm, /vendor/lib, /system/lib]]] couldn't find "libserial_port.so"
        at java.lang.Runtime.loadLibrary(Runtime.java:367)
        at java.lang.System.loadLibrary(System.java:1076)
        at android_serialport_api.SerialPort.<clinit>(Unknown Source)
        at com.rt.printerlibrary.driver.serialport.ComDriver.a(Unknown Source)
        at com.rt.printerlibrary.driver.serialport.ComDriver.run(Unknown Source)

I put the .Jar file and .so into libs folder in my application like below:

enter image description here

And this is my Gradle file:

android {

    compileSdkVersion 28
    defaultConfig {
        applicationId "cards.mena.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 10002
        versionName "1.0.2"
        setProperty("archivesBaseName", "MenaCards-v$versionName")
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    dataBinding {
        enabled = true
    }


    sourceSets {
        main {
//            manifest.srcFile 'AndroidManifest.xml'
            jniLibs.srcDirs = ['libs']
            //    java.srcDirs = ['src']
//            resources.srcDirs = ['src']
            assets.srcDirs = ['assets']
        }


    }

}

androidExtensions {
    experimental = true
}

kapt {
    generateStubs = true
}


dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.anko:anko-commons:0.10.8"

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "com.android.support:design:$supportLibraryVersion"

    /*----------------/ Printer Libs \---------------------------*/
    implementation files('libs/printer_library_ap02.jar')

Update: I downgrade my Gradle form 3.5.0 to 2.3.2 and my application is working now!

Then the problem is Gradle! What I have to do with that?

Upvotes: 2

Views: 6060

Answers (4)

Adetayo
Adetayo

Reputation: 11

I had the same problem, as @Ehsan suggested it worked.

I would like to point out something though, you don't need to downgrade your gradle.

If you'd like to put your .so files in the libs folder then you'd need to add

sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
            assets.srcDirs = ['assets']
        }
    }

Its not necessary if you already added them in the jniLibs folder.

Then adding

defaultConfig {
    ndk {
        abiFilters 'armeabi'
    }
}

made it all work out well.

Upvotes: 0

Antoine Luckenson
Antoine Luckenson

Reputation: 7

I found the solution, you don't have to as a gradle dependency add it in the gradle app file.

android{

sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
            assets.srcDirs = ['assets']
        }
    }

}

Upvotes: 0

Ehsan
Ehsan

Reputation: 2781

I found the problem and its answer.

The problem was about Gradle version and I had to add this line:

defaultConfig {
    ndk {
        abiFilters 'armeabi'
    }
}

Upvotes: 2

georgij
georgij

Reputation: 2200

I put my .so files in src > main > jniLibs and there in the architecture dependent subfolders arm64-v8a, armeabi-v7a, ... . Don't need additional configuration then. May be this helps you.

Upvotes: 0

Related Questions