1.ndk环境变量
E:\AndroidToolscunzade\android-ndk-r9;
测试是否成功
C:\Users\Administrator>ndk-build
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
E:\AndroidToolscunzade\android-ndk-r9\build/core/build-local.mk:130: *** Android
NDK: Aborting . Stop.
2.新建Android工程TestJNI
3.在MainActivity中声明两个native方法
public native String helloFromC();
public native String hello_From_C();
4.新建jni文件夹
5.生成 com_example_testjni_MainActivity.h头文件
E:\AndroidToolscunzade\workspace\TestJNI\bin\classes>javah com.example.testjni.MainActivity
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_testjni_MainActivity */
#ifndef _Included_com_example_testjni_MainActivity
#define _Included_com_example_testjni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_testjni_MainActivity
* Method: helloFromC
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_helloFromC
(JNIEnv *, jobject);
/*
* Class: com_example_testjni_MainActivity
* Method: hello_From_C
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_hello_1From_1C
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
6.编写hello.c实现头文件的方法
#include <stdio.h>
#include <jni.h>
#include "com_example_testjni_MainActivity.h"
JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_helloFromC
(JNIEnv *env, jobject obj){
return (*env)->NewStringUTF(env,"helloFromC");
}
/*
* Class: com_example_testjni_MainActivity
* Method: hello_From_C
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_hello_1From_1C
(JNIEnv *env, jobject obj){
return (*env)->NewStringUTF(env,"helloFromC__");
}
7.编辑Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)
8.生成so库
E:\AndroidToolscunzade\workspace\TestJNI>ndk-build
Android NDK: WARNING: APP_PLATFORM android-18 is larger than android:minSdkVersi on 8 in ./AndroidManifest.xml
"Compile thumb : hello <= hello.c
SharedLibrary : libhello.so
Install : libhello.so => libs/armeabi/libhello.so
9.调用so库
package com.example.testjni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public native String helloFromC();
public native String hello_From_C();
static {
System.loadLibrary("hello");
}
public void click(View view) {
Toast.makeText(this, helloFromC(), 1).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId()==R.id.action_settings) {
Toast.makeText(this, hello_From_C(), 1).show();
}
return super.onMenuItemSelected(featureId, item);
}
}
10.运行
a.点击按钮 b.点击setting
11.重新生成时clean
E:\AndroidToolscunzade\workspace\TestJNI>ndk-build clean
Android NDK: WARNING: APP_PLATFORM android-18 is larger than android:minSdkVersi
on 8 in ./AndroidManifest.xml
Clean: hello [armeabi]
Clean: stdc++ [armeabi]