NDK - How to use a generated .so library in another project
I figured out a way to do this, so posting it here in case someone else runs into it.
The code to be shared (including the Java JNI wrapper, native code, .so library), should be in a separate project. Convert this to a Library project by right-click on project name --> properties --> Android properties --> check mark "Is Library". This project cannot be executed now but can be referenced by other projects.
In the project which will use the shared object, add a reference to the Libarray project by right-click on project name --> properties --> Android properties --> Library/"Add". This should show up the Library project created in the previous step. Select it.
Now the .so can be referred to easily between different projects.
How to use the generated .so library in another Android Project?
You should import MyNDK and use its instance. JNI library is related to Java class. Change the code:
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MyTag", new MyNDK().getMyString());
}
}
You can either export the MyNDK class as a jar package or create an aar bundle. Then import the Java class into your new project with JNI library.
The post How to Build *.so Library Files into AAR Bundle in Android Studio may help you.
How to use extra *.so libraries on Android Studio and NDK
Ok, there could be two different issues.
First, you have to be sure that the library is compiled for the correct architecture. If you are using an armeabi-v7a library, but the compiler is trying to load an armeabi library the compilation will fail.
Second, and following also with the first issue, you have to include the libraries depending the used architecture.
Use the 'flavours' configuration in your module build.gradle script.
In example, you can try to do something like this:
android.productFlavors {
create("arm") {
ndk.with{
abiFilters.add("armeabi")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"
ldLibs.add(libsDir + "libinterface.so")
}
}
create("armv7") {
ndk.with {
abiFilters.add("armeabi-v7a")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi-v7a/"
ldLibs.add(libsDir + "libinterface.so")
}
}
}
Furthermore, I suggest you to use 'jniLibs' to store the libraries, because it is the default path for them, but use different folder for each arch.
You can check other examples like this.
Hope this helps. Greetings.
How to use .so file from one project to another
Using something like this in your Android.mk
should do it:
# Use the sub library as a prebuilt shared library
include $(CLEAR_VARS)
LOCAL_MODULE := MySubLibrary
LOCAL_SRC_FILES = /path/to/sub/library.so
LOCAL_EXPORT_C_INCLUDES := /path/to/sub/library/includes
include $(PREBUILT_SHARED_LIBRARY)
# Build your own library referencing your sub library
include $(CLEAR_VARS)
LOCAL_MODULE := MyFinalLibrary
LOCAL_SHARED_LIBRARIES := MySubLibrary
# Add your source files etc here...
include $(BUILD_SHARED_LIBRARY)
Hope this helps!
NDK: how include *.so files in AndroidStudio
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)
}
Using my own prebuilt shared library in an Android NDK project
In the documentation of Android.mk, check the PREBUILT_SHARED_LIBRARY
script description. Put the .so file in lib
(not libs
) directory and write an Android.mk file next to it that looks something like:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Myaccessories
LOCAL_SRC_FILES := libMyaccessories.so
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../jni/include
include $(PREBUILT_SHARED_LIBRARY)
Related Topics
Android Navigation Drawer on Top Actionbar
How to Provide Different Android App Icons for Different Gradle Buildtypes
Okhttp Response Callbacks on the Main Thread
How to Implement Google Maps Search by Address in Android
Download and Extract Zip File in Android
Android Studio Image Asset Launcher Icon Background Color
Why a Viewmodel Factory Is Needed in Android
Adding Custom Font to Theme in Android
How to Handle Oncontextitemselected in a Multi Fragment Activity
Android: Why Must Use Getbasecontext() Instead of This
Otp (Token) Should Be Automatically Read from the Message
Don't Collapse Toolbar When Recyclerview Fits the Screen
Writing/Reading Files To/From Android Phone's Internal Memory
Nexus 5X Reverse Landscape Sensor Fix in a Android Camera Preview App