I'm aware that there are answers related to this, but they're old and outdated. I got Android Studio 1.3 and already downloaded Android NDK. When I run an app, it crashes and gives findLibrary returned null
in Logcat
. I figured out this was due to no .so
library file (Correct me if I'm wrong). My question is - How do I generate a .so file in Android Studio?
What I have -
Notice - If the library .so
file has to be built from Cygwin
or CMD
, please tell me how to do it.
The SO file stands for Shared Library. You compile all C++ code into the.SO file when you write it in C or C++. The SO file is a shared object library that may be dynamically loaded during Android runtime. Library files are larger, often ranging from 2MB to 10MB in size.
These files are normally stored in /lib/ or /usr/lib/. On an Android device, SO files are stored within the APK under /lib//.
Actually inside your JNI folder, android NDK which convert your native code like c or c++ into binary compiled code that is called "filename.so". You cannot read the binary code . so it wil create lib folder inside your libs/armeabi/ filename.so file. Show activity on this post.
SO files are not meant to be opened. Also, in most cases, you should not move or rename an SO file, as programs require SO files to have specific names and reside in specific locations to call them. Advanced Linux users can use the command nm -D path/to/filename.so.
There are a few steps needed to get the NDK hooked up into Android Studio. Currently, support is marked as experimental and AS is starting to bundle the ability to download the NDK within the IDE. By default, AS uses a generic Android.mk
and Application.mk
when source and/or libs are placed in the jni
or jniLibs
folder. The instructions below override those defaults in order to provide more customization ability.
In short, you will need to:
jni
and jniLibs
directories for your source and libs.Android.mk
file to specify building and linking orderCreate directories
Inside /app/src/main
create a jni
and jniLibs
directory.
Update local.properties
Inside your local.properties
file, add a line similar to:
ndk.dir=/home/nathan/development/bin/android-ndk-r10e
Update build.gradle
This refers to the module level, not the application level. This ensures that we have defined the build path in the step above and removes the ability for Android Studio to automatically invoke ndk-build. Use the following example as a guide.
apply plugin: 'com.android.model.application' model { android { compileSdkVersion = 23 buildToolsVersion = "23.0.0" defaultConfig.with { applicationId = "com.example.hellojni" minSdkVersion.apiLevel = 4 targetSdkVersion.apiLevel = 23 } } compileOptions.with { sourceCompatibility=JavaVersion.VERSION_1_7 targetCompatibility=JavaVersion.VERSION_1_7 } /* * native build settings */ android.ndk { moduleName = "hello-jni" /* * Other ndk flags configurable here are * cppFlags += "-fno-rtti" * cppFlags += "-fno-exceptions" * ldLibs = ["android", "log"] * stl = "system" */ } android.buildTypes { release { minifyEnabled = false proguardFiles += file('proguard-rules.txt') } } android.productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa create("arm") { ndk.abiFilters += "armeabi" } create("arm7") { ndk.abiFilters += "armeabi-v7a" } create("arm8") { ndk.abiFilters += "arm64-v8a" } create("x86") { ndk.abiFilters += "x86" } create("x86-64") { ndk.abiFilters += "x86_64" } create("mips") { ndk.abiFilters += "mips" } create("mips-64") { ndk.abiFilters += "mips64" } // To include all cpu architectures, leaves abiFilters empty create("all") } }
Android.mk
You will need an Android.mk
file inside the /app/src/main/jni
directory
LOCAL_PATH := $(call my-dir) # Builds a dylib out of test.cpp include $(CLEAR_VARS) LOCAL_MODULE := test LOCAL_SRC_FILES := test.cpp LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)
test.cpp
Add some awesome C/C++ source code for your lib. These files will start in /app/src/main/jni
and will be compiled and linked as specified in your Android.mk
Example file
#include <jni.h> #include <android/log.h> static const char *SOME_TAG = "MyAwesomeTag"; extern "C" { void Java_com_something_something_1android_ClassName_some_fn(JNIEnv *env, jobject obj) { __android_log_print(ANDROID_LOG_VERBOSE, SOME_TAG, "Hello from NDK :)"); } } // End extern
Compile and run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With