美文网首页
首次在AS配置JNI

首次在AS配置JNI

作者: 涛娃子 | 来源:发表于2016-07-16 14:07 被阅读0次

    Add C/C++ codes into AS project

    Paste_Image.png
    1. create jni folder under main, jni and java folder are the same parent location
    2. copy C and C++ files into jni folder
    3. create Android.mk
    Paste_Image.png
    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

    Paste_Image.png

    open the build.gradle under app folder

    Paste_Image.png

    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

    1. create java class
    Paste_Image.png
      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

    1. change the jni name

    find you pakage name in you java class

    Paste_Image.png

    now open the file which include the functions you want to invoke

    Paste_Image.png

    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.

    1. 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

    Paste_Image.png

    find the libs in

    Paste_Image.png

    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

    Paste_Image.png

    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"

    Paste_Image.png

    check out the ndk path in the local.properties of your AS project, if error, correct it, if miss, add it.

    相关文章

      网友评论

          本文标题:首次在AS配置JNI

          本文链接:https://www.haomeiwen.com/subject/oohujttx.html