过程:
1. 写 java文件(你需要的类 或者函数)
生成. h 文件
首先写完ImageProc.java 这个保存 以后 eclipse会自动帮你生成 java 的class文件
你可以再工程目录--》 bin --》classes -》com -------。。。。。 的文件里找到
比如我的 在E:\code\TestJni\bin\classes\com\test2\test2 这个路径下
然后cd 到classes这个目录下
然后 javah com.test2.test2.ImageProc 回车 即可生成 .h文件 可以改名字
然后 将此.h文件copy到工程jni中
2. 下一步开始编写.c 或者.cpp文件(不要忘记添加头文件) 需要学习一下编写规则等等
注意:
(return
(*env)->
NewStringUTF
(env,
"Hello JTC from JNI !"
);) 这条语句貌似只能在.c中不报错
然后主程序中加载.so库文件时 不要添加
//import android.provider.Settings.System;
否则
System.loadLibrary("com_example_testhellojni_TestJni");会报错
3. 生成.so库文件
windows环境下
build command 为 ndk-build.cmd (前提是设置好了Path)
Android.mk:
其他的几个配置说明:
Tool Chain Editor 默认就可以吧 (不太懂 )
加入库文件 我的文件路径为:
然后运行程序会自动生成.so 文件 然后android程序运行
也可以通过cygwin生成.so文件:
如图:
我的一个jni工程 太烂了 :
工程目录:
详细内容:
MainActivity.java:
package com.example.testhellojni;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
//import android.provider.Settings.System;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.example.testhellojni.TestJni;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
//System.loadLibrary("com_example_testhellojni_TestJni");
Log.d("Hi", "Android call JNI: " + TestJni.outputJNI() );
}
@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 onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true ;
}
return super .onOptionsItemSelected(item);
}
static {
System. loadLibrary("com_example_testhellojni_TestJni");
}
}
TestJni.java:
package com.example.testhellojni; //自动生成
public class TestJni {
native static String outputJNI();
}
com_example_testhellojni_TestJni.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_testhellojni_TestJni */
#ifndef _Included_com_example_testhellojni_TestJni
#define _Included_com_example_testhellojni_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_testhellojni_TestJni
* Method: outputJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_testhellojni_TestJni_outputJNI
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
hello.c:
/*
* hello.c
*
* Created on: 2014-11-25
* Author: jiangtc
*/
#include <string.h>
#include <jni.h>
#include <com_example_testhellojni_TestJni.h>
JNIEXPORT jstring Java_com_example_testhellojni_TestJni_outputJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello JTC from JNI !");
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE : = com_example_testhellojni_TestJni
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)
Application:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8