Call shared library (.so) methods within Android Studios C files

那年仲夏 提交于 2019-11-28 14:19:33
JavadKhan

There are lots of things, you can do in Android NDK. For example, Camera hardware is one of the heaviest hardware in Android OS. Detecting faces, things, giving effects and for thousands of features NDK is the best. Some helps for your steps:

  1. You can built and prebuilt shared(.so) and static(.a) libraries in Android Studio also. Not need Visual Studio.
  2. Don't create jniLibs folder in main folder. When you build your project via gradle, it already creates this folder and put your target libraries. If you want prebuilt any libraries, put these libraries in main/jni/libs folder and prebuilt then with Android.mk.
  3. Don't add the Android.mk file in jnilibs folder. Create this file in main/jni folder. Also Application.mk file.
  4. Call your libraries, in any activity, where you need, in static method. Like this:

    static {  System.loadLibrary("my_library") }
    

    Without "lib" and ".so" extensions.

When you want to call your native methods, just use "native" keyword. For example:

private native int nGetNumberFromNativeSide();

Just call this method, where you want, and get result. But for ndk building in gradle side, look at this answer. For building library in Android.mk, these sample lines maybe help you:

include $(CLEAR_VARS)
ifneq (,$(filter $(TARGET_ARCH_ABI), armeabi-v7a x86 arm64-v8a x86_64))

LOCAL_MODULE := my_library
LOCAL_SRC_FILES := $(LOCAL_SRC_LOCATION)/native1.cpp native2.cpp
include $(BUILD_SHARED_LIBRARY)
  • You can put name anything you want, but dont add lib and .so extensions. Ndk is already doing it.
  • I have already gave Android.mk example.
  • When you build Android.mk file, it locates your libraries appropriate folder. Like main/libs/x86/libmy_library.so.

I guess this answer will help you. If you have more questions, add to comment, i'll edit my answer and add answers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!