Add C/C++ codes into AS project
- create jni folder under main, jni and java folder are the same parent location
- copy C and C++ files into jni folder
- create Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gifflen //lib name
LOCAL_SRC_FILES := gifflen.cpp //src file
LOCAL_LDLIBS += -llog // linked library
include $(BUILD_SHARED_LIBRARY) //generate dynamic library
config build.gradle
open the build.gradle under app folder
add ndk module, the moduleName is mandatory, the ldLibs must set as above, some docs mentioned config ldLibs as ldLibs += "log", my AS will report error.
create java class for you c code
- create java class
static{
System.loadLibrary("gifflen");
}
means load the library
public native void Close();
public native int AddFrame(int[] pixels);
public native means declare the functions you want to use in java
- change the jni name
find you pakage name in you java class
now open the file which include the functions you want to invoke
you'll find the JNIEXPORT name is "java_"+"package name"+"java class name"+"function name"
the picture is just the function declare, don't forget to change the real function name.
- if you files include C and C++ files, don't forget add
#ifdef __cplusplus
#endif
in you C files, or the AS will report error
Generate Libs
now, you can go to the jni folder, and generate libs
find the libs in
because I didn't config in build.gradle which platform I prefer, so ndk compiled all platforms libs
and next you could use jni gladly
how to add NDK in AS
click the top button, and choose NDK and ok button. AS will download NDK automatically.
if done, you can find the ndk in "Android/sdk/ndk-bundle", and then export ndk-build PATH "export PATH=$PATH:/usr/local/bin:/Users/liut1/Library/Android/sdk/ndk-bundle"
check out the ndk path in the local.properties of your AS project, if error, correct it, if miss, add it.