<strong><span style="color:#ff0000;">MainActivity.java</span></strong>
package com.example.com.test.jni;import android.view.View;import android.view.View.OnClickListener;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText mSetValue = null;private TextView mGetValue = null;private Button mButton = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSetValue = (EditText) findViewById(R.id.setValue);mGetValue = (TextView) findViewById(R.id.getValue);mButton = (Button) findViewById(R.id.mButton);mButton.setOnClickListener(new ButtonClick());}class ButtonClick implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint val = Integer.parseInt(mSetValue.getText().toString());setValue(val);int temp = getValue();mGetValue.setText(""+temp);}}static    {        System.loadLibrary("testJNI");    }    public native static int getValue();    public native static void setValue(int i);}
  
<strong><span style="color:#ff0000;">activity_main.xml</span></strong>
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.com.test.jni.MainActivity" >        <EditText         android:id="@+id/setValue"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>        <TextView         android:id="@+id/getValue"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>        <Button         android:id="@+id/mButton"        android:text="Get"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/></LinearLayout>
以上是java层的代码,将Java层的代码编译成功之后 生成 MainActivity.class文件  
  
使用 javah命令生成 JNI 的接口文件 <span style="color:#ff0000;">注意:执行命令在原代码的目录下面,不要在 .class文件的目录线面。</span>
eg:Java项目目录是 E:/TestJNI , windows 下 cmd 进入到 <span style="font-family: Arial, Helvetica, sans-serif;">E:/TestJNI/src  执行 :javah com.example.com.test.jni.MainActivity</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="color:#ff0000;">执行前如果运行的是 Android 代码 需要在环境变量  classpath中添加 sdk的位置</span></span>
生成的<pre name="code" class="cpp">com_example_com_test_jni_MainActivity<span style="font-family: Arial, Helvetica, sans-serif;">.h文件</span>
  
  
<pre name="code" class="cpp">/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_com_test_jni_MainActivity */#ifndef _Included_com_example_com_test_jni_MainActivity#define _Included_com_example_com_test_jni_MainActivity#ifdef __cplusplusextern "C" {#endif#undef com_example_com_test_jni_MainActivity_BIND_ABOVE_CLIENT#define com_example_com_test_jni_MainActivity_BIND_ABOVE_CLIENT 8L#undef com_example_com_test_jni_MainActivity_BIND_ADJUST_WITH_ACTIVITY#define com_example_com_test_jni_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L#undef com_example_com_test_jni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT#define com_example_com_test_jni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L#undef com_example_com_test_jni_MainActivity_BIND_AUTO_CREATE#define com_example_com_test_jni_MainActivity_BIND_AUTO_CREATE 1L#undef com_example_com_test_jni_MainActivity_BIND_DEBUG_UNBIND#define com_example_com_test_jni_MainActivity_BIND_DEBUG_UNBIND 2L#undef com_example_com_test_jni_MainActivity_BIND_IMPORTANT#define com_example_com_test_jni_MainActivity_BIND_IMPORTANT 64L#undef com_example_com_test_jni_MainActivity_BIND_NOT_FOREGROUND#define com_example_com_test_jni_MainActivity_BIND_NOT_FOREGROUND 4L#undef com_example_com_test_jni_MainActivity_BIND_WAIVE_PRIORITY#define com_example_com_test_jni_MainActivity_BIND_WAIVE_PRIORITY 32L#undef com_example_com_test_jni_MainActivity_CONTEXT_IGNORE_SECURITY#define com_example_com_test_jni_MainActivity_CONTEXT_IGNORE_SECURITY 2L#undef com_example_com_test_jni_MainActivity_CONTEXT_INCLUDE_CODE#define com_example_com_test_jni_MainActivity_CONTEXT_INCLUDE_CODE 1L#undef com_example_com_test_jni_MainActivity_CONTEXT_RESTRICTED#define com_example_com_test_jni_MainActivity_CONTEXT_RESTRICTED 4L#undef com_example_com_test_jni_MainActivity_MODE_APPEND#define com_example_com_test_jni_MainActivity_MODE_APPEND 32768L#undef com_example_com_test_jni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING#define com_example_com_test_jni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L#undef com_example_com_test_jni_MainActivity_MODE_MULTI_PROCESS#define com_example_com_test_jni_MainActivity_MODE_MULTI_PROCESS 4L#undef com_example_com_test_jni_MainActivity_MODE_PRIVATE#define com_example_com_test_jni_MainActivity_MODE_PRIVATE 0L#undef com_example_com_test_jni_MainActivity_MODE_WORLD_READABLE#define com_example_com_test_jni_MainActivity_MODE_WORLD_READABLE 1L#undef com_example_com_test_jni_MainActivity_MODE_WORLD_WRITEABLE#define com_example_com_test_jni_MainActivity_MODE_WORLD_WRITEABLE 2L#undef com_example_com_test_jni_MainActivity_DEFAULT_KEYS_DIALER#define com_example_com_test_jni_MainActivity_DEFAULT_KEYS_DIALER 1L#undef com_example_com_test_jni_MainActivity_DEFAULT_KEYS_DISABLE#define com_example_com_test_jni_MainActivity_DEFAULT_KEYS_DISABLE 0L#undef com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL#define com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L#undef com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL#define com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L#undef com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SHORTCUT#define com_example_com_test_jni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L#undef com_example_com_test_jni_MainActivity_RESULT_CANCELED#define com_example_com_test_jni_MainActivity_RESULT_CANCELED 0L#undef com_example_com_test_jni_MainActivity_RESULT_FIRST_USER#define com_example_com_test_jni_MainActivity_RESULT_FIRST_USER 1L#undef com_example_com_test_jni_MainActivity_RESULT_OK#define com_example_com_test_jni_MainActivity_RESULT_OK -1L/* * Class:     com_example_com_test_jni_MainActivity * Method:    getValue * Signature: ()I */JNIEXPORT jint JNICALL Java_com_example_com_test_jni_MainActivity_getValue  (JNIEnv *, jobject);/* * Class:     com_example_com_test_jni_MainActivity * Method:    setValue * Signature: (I)V */JNIEXPORT void JNICALL Java_com_example_com_test_jni_MainActivity_setValue  (JNIEnv *, jobject, jint);#ifdef __cplusplus}#endif#endif

再实现对应的
com_example_com_test_jni_MainActivity.<span style="font-family: Arial, Helvetica, sans-serif;">cpp文件</span>
  
  
<pre name="code" class="cpp">#include "com_example_com_test_jni_MainActivity.h"int i = 0;jint  Java_com_example_com_test_jni_MainActivity_getValue(JNIEnv *env, jobject jc){return i;}void  Java_com_example_com_test_jni_MainActivity_setValue(JNIEnv *env, jobject jc, jint j){i = j;}

两个文件生成之后就可以编译了
  
gcc -fPIC -D_REENTRANT -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux/ -c com_example_com_test_jni_MainActivity.cpp  编译目标文件 <pre name="code" class="java">com_example_com_test_jni_MainActivity<span style="font-family: Arial, Helvetica, sans-serif;">.o</span>
  
  
gcc -shared com_example_com_test_jni_MainActivity.o -o libtestJNI.so 生成动态库
这样生成的动态库 会有32  64 位不一致问题,具体怎么搞好没有研究
  
如果有Android 工程的话可以编写个 Android.mk直接编译即可
Android.mk
  
<pre name="code" class="cpp">LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_MODULE := libtestJNILOCAL_32_BIT_ONLY := trueLOCAL_LDFLAGS += -Wl,--no-warn-shared-textrelLOCAL_LDFLAGS += -Wl,--no-fatal-warningsLOCAL_SRC_FILES += com_example_com_test_jni_MainActivity.cpp \                                include $(BUILD_SHARED_LIBRARY)
  
将生成的 .so文件放到app工程的 libs/armeabi 目录下面编译即可


  
  
  

更多相关文章

  1. android 读写文件
  2. Android情景模式、文件管理器 完整示例编程详解、Android程序优
  3. android WebView载入本地html及css文件
  4. [Android Pro] proguard.cfg 配置文件
  5. Android 将文件保存到SD卡中
  6. 编译NotificationManagerService.java文件的方法
  7. Android studio gradle 无法获取 pom文件
  8. Android FTP客户端使用,快速上传文件
  9. Android N共享文件报错:android.os.FileUriExposedException

随机推荐

  1. 第二十一课 Bootstrap学习
  2. file本地文本传输协议为什么无法设置cook
  3. 学习笔记:表格小知识
  4. 匿名函数、箭头函数和立即执行函数
  5. gitignore的一些语法
  6. 表单事件、键盘事件、get
  7. 07-14作业 JSON 对象转换字符串 选项卡
  8. 匿名函数、箭头函数、立即执行函数和字面
  9. 进入bootstrap的大门之表格
  10. bootstrap常用组件样式使用之,导航,列表,按