5

I have libs jar and *.so. I created Eclipse project as in tutorial (for this libs). I am now doing project in Android Studio, but system can't find *.so files. I make how in this - Include .so library in apk in android studio . My app does not find function in so libs. How to find them? Log:

10-16 12:16:55.965: W/dalvikvm(4386): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/atol/drivers/fptr/IFptrNative;
10-16 12:16:55.965: W/dalvikvm(4386): threadid=1: thread exiting with uncaught exception (group=0x41869438)
10-16 12:16:55.976: E/AndroidRuntime(4386): FATAL EXCEPTION: main
10-16 12:16:55.976: E/AndroidRuntime(4386): java.lang.ExceptionInInitializerError

An error occurs when creating an object using a JAR library. She in turn poked *.so files, but to find what she needs. Who is to blame and what to do?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gc986
  • 756
  • 1
  • 10
  • 24
  • add a .so file from directory outside android project: https://stackoverflow.com/questions/50713933/add-so-prebuilt-library-from-another-directory-to-apk – user1506104 Jun 11 '18 at 17:28

4 Answers4

9

Three options:

One

Copy yours *.SO libraries on your libs folder and put that on build.gradle:

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

Two

Make a new folder on src/main/jniLibs and write that on your build.gradle:

android {
    //Another code 
    sourceSets {
        main {         
            jniLibs.srcDirs = ['src/main/jnilibs']          
        }
        //Another code 
    }//sourceSets tag close
}//Android tag close

There

Make a new folder on src/main/jniLibs and write that on your build.gradle:

//Another code....

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


    task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
        destinationDir file("$buildDir/native-libs")
        baseName 'native-libs'
        from fileTree(dir: 'src/main/jnilibs', include: '**/*.so')
        into 'lib/'
    }
    
    tasks.withType(JavaCompile)
     {
          compileTask -> compileTask.dependsOn(nativeLibsToJar)
     }
Michał Leon
  • 2,108
  • 1
  • 15
  • 15
Aspicas
  • 4,498
  • 4
  • 30
  • 53
  • Does not work. I create an object using a library JAR,but it works .so libraries. In Eclipse everything works. Can AndroidStudio generates incorrect links .so library or JAR file library tied to the project structure in Eclipse? – gc986 Oct 16 '15 at 08:22
  • Thank you very much. This saved me after a week searching for solutions. Turned out that in my sourceSets.main.jniLibs.srcDirs there was just ["libs"]. Changing it to ['src/main/jniLibs'] made it work. – anunezr Jan 21 '22 at 12:30
7

Current Solution

Create the folder project/app/src/main/jniLibs, and then put your *.so files within their abi folders in that location. E.g.,

project/
├──libs/
|  └── *.jar       <-- if your library has jar files, they go here
├──src/
   └── main/
       ├── AndroidManifest.xml
       ├── java/
       └── jniLibs/ 
           ├── arm64-v8a/                       <-- ARM 64bit
           │   └── yourlib.so
           ├── armeabi-v7a/                     <-- ARM 32bit
           │   └── yourlib.so
           └── x86/                             <-- Intel 32bit
               └── yourlib.so

Deprecated solution

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(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}


Vivek Hirpara
  • 787
  • 7
  • 17
  • Remember to include this in your build.gradle sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] } } If the content between the [square brackets] is different, Android Studio will not load the so libraries correctly into the APK and may skip some libraries you need. This gave me headaches for a whole week. – anunezr Jan 21 '22 at 12:56
1

You need to put your .so files inside a folder named lib (it's not libs and should be lib). It should be in the same structure it should be in the APK file.

Structure

Project:
|--lib:
|--|--armeabi:
|--|--|--.so files.

Then an armeabi folder where insert all .so files. Then zip the folder into a .zip.Rename the .zip file into armeabi.jar and add the line compile fileTree(dir: 'libs', include: '*.jar') into dependencies {} in the gradle's build file.

For more info and alternate method please see This answer and This answer

Community
  • 1
  • 1
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
  • Does not work. I create an object using a library JAR,but it works .so libraries. In Eclipse everything works. Can AndroidStudio generates incorrect links .so library or JAR file library tied to the project structure in Eclipse? – gc986 Oct 16 '15 at 08:25
1

Create folder name "jniLibs" inside src/main folder and add All the folders containing your "*.so" file and sync and run your application. No other steps required.

venkatesh
  • 81
  • 1
  • 2