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:
- Create the shared library (Calculator.so) from Visual Studios 2015 "Create shared library for Android" (or something) [successful]
- Create jniLibs folder in src/main/ with its subfolders (x86 the relevant one in my case)
- Add the Android.mk file in src/main/jniLibs which has to be placed there (?)
- 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)
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:
Call your libraries, in any activity, where you need, in static method. Like this:
Without "lib" and ".so" extensions.
When you want to call your native methods, just use "native" keyword. For example:
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:
I guess this answer will help you. If you have more questions, add to comment, i'll edit my answer and add answers.