How to use NDK in android project?

前端 未结 2 1632
谎友^
谎友^ 2020-12-14 04:57

I need to use some native c/c++ in my project, so I need to use the NDK. Is there an easy way to set it up in eclipse?

Thanks.

2条回答
  •  清歌不尽
    2020-12-14 05:54

    There are following steps

    1 : Create a jni folder in your project directory

    2 : Create a file name Android.mk in your newly created folder jni and create a new file of C or C++, lets we consider hear we use C file and name of file is MyNativeC.c

    3: now type following code in Android.mk file

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_LDLIBS := -llog
    
    LOCAL_MODULE    := local_module_ndk // this is the name of local module by which you can call the Local source file
    LOCAL_SRC_FILES := MyNativeC.c// hear we can pass the name of your native file name hear we are use MyNativeC.c file so we pass the name of MyNativeC.c in local source file name
    
    include $(BUILD_SHARED_LIBRA
    

    4 now open MyNativeC.c file and create two method which you want to call from your android code(from your Activity) hear we create following code

        #include 
    #include 
    #include 
    #include 
    
    #define DEBUG_TAG "MY_NDK_DEMO"
    
    
    jstring Java_com_myNDKDemo_MainActivity_getString(JNIEnv * env, jobject this, jint value1, jint value2)
    {
        char *szFormat = "Addition : %i";
        char *szResult;
    
    
        jlong sum = value1+value2;
    
    
        szResult = malloc(sizeof(szFormat) + 20);
    
    
        sprintf(szResult, szFormat, sum);
    
        jstring result = (*env)->NewStringUTF(env, szResult);
    
    
        free(szResult);
    
        return result;
    }
    

    5 now edit your activity where you want to call the native code,

    first create a static block where we have to load the library of native code.

    hear we show the code of my activity name is MainActivity.java

    package com.myNDKDemo
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    
    public class MainActivity extends Activity {
        private static final String DEBUG_TAG = "MainActivity";
    
    
        private native String getStringAdd(int fist, int second);
    
        static {
            System.loadLibrary("local_module_ndk");
        }
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            button b = new Button(this);
    
            b.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
    
                                Toast.makeText(getApplicationContext(),""+getStringAdd(100,200), 2000).show();
    
            }
        });
    
               setContentView(b);
    
        }
    }
    

    6 Now first compile the c code, for compile c code first you have to NDK development kit,

    now open run. and type cmd

    now go to project path

    In my case I create application in Destop, so hear we give the path of my app

    after that typw the path of the my NDK's ndk-build file path

    enter image description here

    now we press enter the automatic it create libs directory in your project enter image description here

    7 If you see in your project, there are libs and obj created automatically.

    8.Refresh (right Click) the JNI folder (refresh it every time you build using teh ndk-build,this actually loads the newly built shared library in the libs folder.)

    9.Now run your android project, when press the button the it will show you the addition

    thanks

提交回复
热议问题