How to use NDK in android project?

前端 未结 2 1621
谎友^
谎友^ 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 <jni.h>
    #include <string.h>
    #include <stdio.h>
    #include <android/log.h>
    
    #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

    0 讨论(0)
  • 2020-12-14 06:02

    Ok, after I spent some time experimenting, I can say that the best way for a beginner to start using NDK in eclipse is first to go over this tutorial: http://marakana.com/forums/android/examples/49.html and simply create the necessary files for the jni folder (but don't do anything else). Then you should read this http://mobilepearls.com/labs/ndk-builder-in-eclipse/ and follow the steps. Then you are ready.

    0 讨论(0)
提交回复
热议问题