NDK: how include *.so files in AndroidStudio

Multi tool use
NDK: how include *.so files in AndroidStudio
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?
4 Answers
4
Three options:
One
Copy yours *.SO libraries on your libs
folder and put that on Build.gradle
:
libs
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
:
src/main/jniLibs
Build.gradle
android {
//.................Another code
sourceSets
{
main
{
jniLibs.srcDirs = ['src/main/jnilibs']
}
//Another code
}//Android tag close
There
Make a new folder on src/main/jniLibs
and write that on your Build.gradle
:
src/main/jniLibs
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)
}
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 8:22
option two worked for me .
– Dipak Sonnar
Feb 23 '16 at 12:55
I'm glad to listen @DipakSonnar
– Aspicas
Feb 23 '16 at 13:06
After 2 days of searching, you are the best!! Thank man!
– Eli Elezra
Aug 18 '16 at 8:01
I'm glad to listen too @EliElezra =)
– Aspicas
Aug 18 '16 at 8:45
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)
}
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.
.so
compile fileTree(dir: 'libs', include: '*.jar')
dependencies {}
For more info and alternate method please see This answer and This answer
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 8:25
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.
does not work that way
– Vikas Pandey
Jul 5 '17 at 14:10
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
add a .so file from directory outside android project: stackoverflow.com/questions/50713933/…
– user1506104
Jun 11 at 17:28