android java 调用 .so 动态库实例
16lz
2021-12-04
<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 目录下面编译即可
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android 读写文件
- Android(安卓)删除指定文件代码
- Android中带分割线的九宫格
- Android使用ccache减少编译时间
- Android(安卓)JNI编程
- Android(安卓)背光流程小结
- [Android(安卓)Pro] proguard.cfg 配置文件