Call shared library (.so) methods within Android S

2019-01-20 14:45发布

问题:

I'm struggling with this for several days now. At the moment i'm just testing it with a simple C++ project (1 .h & 1 .cpp file) and a minimalistic App including the ndk helloJNI sample code (which worked perfect easily):

Target Import existing C/C++ files (project) to Android Studio

Approach After trying out some of the (dozens) of different possibilities, i think/thought the following steps would be the best solution for my purpose:

  1. Create the shared library (Calculator.so) from Visual Studios 2015 "Create shared library for Android" (or something) [successful]
  2. Create jniLibs folder in src/main/ with its subfolders (x86 the relevant one in my case)
  3. Add the Android.mk file in src/main/jniLibs which has to be placed there (?)
  4. Include statement: System.loadLibrary("Calculator") without "lib" and ".so" in MainActivity

The library is listed in Android Studio in its folder jniLibs as like the Android.mk. Moreover if i build the apk, the library is successfully packed (verified by unzipping) and i dont get any errors. BUT: how can i call the methods in the library? I tried the different solutions offered in other threads, but i think i missed something in my .mk or my steps described above.

Tried

  • Different #include <myLib> statements in native-lib.cpp, like s
  • Different Android.mk settings (but i'm new to make files so not even tutorials helped me much with my specific problem ::) )
  • Other locations for the libCalculator.so like in the subfolder x86
  • and many others - simply not reminding atm (wasntme)

Your help is highly appreciated!

Android.mk

LOCAL_PATH := $(call my-dir)
APP_ABI := x86        

# library info       
include $(CLEAR_VARS)
LOCAL_MODULE := Calculator
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/Calculator.so
LOCAL_EXPORT_C_INCLUDES := ..../Visual Studio 2015/Projects/SO_Library/SO_Library
include $(BUILD_SHARED_LIBRARY)

回答1:

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.