简体   繁体   中英

provide help for using Android NDK

I want to use android NDK in my project. So my question is, how to integrate and execute default sample project of hello-jni from android NDK? Please provide me step by step solution on it... I don't know anything about it...

1.    First Create Android Project 
2.    Then Create Folder  in the Project with name jni (Dont use other name)

3.       Create File With the Android.mk ( Rightclick on the jni folder ->New -> File -                                                                                 >Android.mk)
// Coding To Build Path and Access
LOCAL_PATH :=$(call my-dir)         // This is Common
include $(CLEAR_VARS)               // This is common`enter code here`
LOCAL_MODULE    := myCFile  // myCFile is ur own  Name of the module
                that will be    called in Activity)
LOCAL_SRC_FILES := my.c   // Our C File What should be given in C
                                                                             file creation

include $(BUILD_SHARED_LIBRARY)


4.    Create C File like above With ur own name with the extension .c (This file will be
                                                           used  in LOCAL_SRC_FILES )
                    C File Will Look Like this:
Note :
// Your Package name com.JniMyDemo Then Your Activity File and then the Function name for c file
So that I used jint(return value) 
(Java) its must
(com_JniDemo) is  Specified in Package name(com.JniDemo) we couldnt use . for package so 
we put underscore for c file )
(JniMyDemoActivity) Is my class

First two arguement is Important one others are our own variable for function //

Include two header file string.h,jni.h


#include "string.h"
#include "jni.h"

jint Java_com_JniMyDemo_JniMyDemoActivity_add(JNIEnv *env,jobject jobj,jint n1,jint n2)
{
jint a,b,c;
a=n1;
b=n2;
return (a+b);
}

5.  Then Call the The C File Like this 

// Activity Look like this 

public class JniMyDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView txt    =new TextView(this);
        txt.setText(" "+add(100,100));
        setContentView(txt);
    }

    private native int add(int i,int j,int k);
    static
    {
        System.loadLibrary("myCFile");
    }
}  

6.      Then Compile the C File by 
    Starting Terminal (cmd prompt) Type the Project Path like ( cd  Project Path) and Enter
    Then type the          ( Ndk Location/ndk-build) it will automatically compile & ur jni             folder
7.   If it success means Start the Actvity  it will show the Result of Addition

Here is a rather good tutorial for NDK beginners: Getting Started with the NDK .

Learn it the tough way to remember things. Inside Andaroid NDK you downloaded from developers.android.com, you will have docs folder in which you shall be having different documents which will clearly tell you how and when NDK should be used.Especially go through OVERVIEW.HTML Completely. http://developer.android.com/sdk/ndk/index.html

I did it this way when i used NDK first time. And i strongly recommend only this way..

If you are confused what is Native Code -> It is the C or C++ or Assembly code that you want to use with Android application. This is being called as native code as it is not in java language.

All the best.

You have a great tutorial in here, explain all you need to use the NDK, how to create a functions and how to use them and it's easy to follow. http://marakana.com/s/introduction_to_ndk,1153/index.html , sorry about my English, not my mother language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM