During the preliminary download, The ndk environment configuration is ignored here. It was previously written.
1: Create an android project:
For example, the project name is testndk, the program name is testndk, the package name is com. Cheng, and the activity is testndk.
2: Create a Java class named "JNI" under the package. The content is as follows:
package com.cheng;public class jni{ public native int GetCint(); public native String getCstring(); }
3. Copy the JNI. Java file under the directory "../src/COM/Cheng" to the directory "../bin" (this step is not required for personal testing)
4. Open the terminal, locate "../bin" in the directory, and execute "javac JNI. Java" to generate JNI. Class (do not close the terminal at this time, but use it again)(This step is not required after personal testing)
5. Copy JNI. class to the "../bin/COM/Cheng" directory to overwrite files with the same name.(This step is not required after personal testing)
6. Enter "javah-JNI com. Cheng. JNI" on the terminal. A header file "com_cheng_jni.h" is generated in the current directory"
7. Create a folder named "JNI" under the Code root directory and copy "com_cheng_jni.h" to this folder.
8. Create a c file:
A can create a C project or a *. c file.
I created a C project with an arbitrary project name (hellondk). The directory should be set to "../JNI ".
B. Create the c file. Because the header file is "com_cheng_jni.h", the C file name must correspond to "com_cheng_jni.c"
Content:
#include <stdio.h>#include <stdlib.h>#include "com_cheng_jni.h"int add2(){int x=10,y=10;return x+y;}JNIEXPORT jint JNICALL Java_com_cheng_jni_GetCint (JNIEnv *env, jobject thiz){return add2();}JNIEXPORT jstring JNICALL Java_com_cheng_jni_getCstring (JNIEnv *env, jobject thiz){(*env)->NewStringUTF(env,"hello cheng!!!");}
Code Writing: the header file must be input by hand, and the Add2 method can be written by yourself.
The next two methods directly go to "com_cheng_jni.h", copy the two function declarations, and add the parameters "env" and "thiz"
Content of the completion method.
9. Create the Android. mk file in the JNI directory
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = hellondk
Local_src_files: = com_cheng_jni.c
Include $ (build_shared_library)
10. Create an application. MK file (this step does not seem necessary, and the compilation commands provided by the author seem to be out of date due to version upgrades; the compilation commands executed later are my own research and the results are correct)
11 compile and generate the *. So file:
Terminal, locate in the Code root directory, and execute ndk_build (view the options ndk-Build-H for this command)
12 at this time in eclipse right-click the project, refresh the project file, you can see the current project directory and file structure, libhello-jni.so in OBJ directory
13. Edit testndk. Java and call this so file.
package com.cheng;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class TestNDKActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); JNI j=new JNI(); TextView t=new TextView(this); t.setText(j.getCstring()+Integer.toString(j.GetCint())); setContentView(t); } static { System.loadLibrary("helloNDK"); }}
14. Compile and execute the program. You can see the effect on the simulator.
---------------------------------------------------------------------------
Note: The module and file name are case-insensitive and error-prone.
Andk is probably in this process. Do it again and try it yourself.
B And then modify the c file. You need to execute ndk-Build-B and re-compile the so file. Then eclipse refresh the file directory and the execution will see the changes.
Here is a similar article. You can refer to it as follows:
Http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html