I read many threads how to add a *.so library to Android Studio, but none of them works, especially when it comes to the point of text: This does not work with the newer xxx (Android Studio, gradle, ...)
Can we make a fresh start please. I got:
Android Studio 0.6.0
From Project Structure I see:
SDK Location:
/usr/share/android-studio/data/sdk
/usr/lib/jvm/default-java
Project:
Gradle version 1.10
Android Plugin Version 0.11.+
Modules/app: Properties:
Compile Sdk Version 19 Build Tools Version 19.1.0
Dependencies:
{dir=libs, include=[*.jar]} Compile
{dir=libs, include=[*.so]} Provided
m com.android.support: appcompat -v7:19.+ Compile
I got the *.so files pre-compiled and at the demo app they are working. I have to change the source code of the app, so I need to rebuild with the same *.so files.
This is my build.gradle file, Please note the line
This will include libs's *.so file to apk.
*.so library in Android Studio
You have to generate jniLibs folder inside main in android Studio projects and put your all .so files inside. You can also integrate this line in build.gradle
compile fileTree(dir: 'libs', include: ['.jar','.so'])
It's work perfectly
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
This is the project structure.
Adding .so Library in Android Studio 1.0.2
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
|--|--|--|--|--x86
|--|--|--|--|--|--.so Files
Reference
https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main
Android NDK official
hello-libs
CMake examplehttps://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs
Just worked for me on Ubuntu 17.10 host, Android Studio 3, Android SDK 26, so I strongly recommend that you base your project on it.
The shared library is called
libgperf
, the key code parts are:hello-libs/app/src/main/cpp/CMakeLists.txt:
app/build.gradle:
Then, if you look under
/data/app
on the device,libgperf.so
will be there as well.on C++ code, use:
#include <gperf.h>
header location:
hello-libs/distribution/gperf/include/gperf.h
lib location:
distribution/gperf/lib/arm64-v8a/libgperf.so
If you only support some architectures, see: Gradle Build NDK target only ARM
The example git tracks the prebuilt shared libraries, but it also contains the build system to actually build them as well: https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs/gen-libs
Solution 1 : Creation of a JniLibs folder
Create a folder called “jniLibs” into your app and the folders containing your *.so inside. The "jniLibs" folder needs to be created in the same folder as your "Java" or "Assets" folders.
Solution 2 : Modification of the build.gradle file
If you don’t want to create a new folder and keep your *.so files into the libs folder, it is possible !
In that case, just add your *.so files into the libs folder (please respect the same architecture as the solution 1 : libs/armeabi/.so for instance) and modify the build.gradle file of your app to add the source directory of the jniLibs.
You will have more explanations, with screenshots to help you here ( Step 6 ):
http://blog.guillaumeagis.eu/setup-andengine-with-android-studio/
EDIT It had to be jniLibs.srcDirs, not jni.srcDirs - edited the code. The directory can be a [relative] path that points outside of the project directory.
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.,Deprecated solution
Add both code snippets in your module gradle.build file as a dependency:
How to create this custom jar:
Same answer can also be found in related question: Include .so library in apk in android studio