标签:byte 分享 generated nis android jniexport eclipse ini 文件配置
本文记录一个Java层与JNI层參数与数据交互的应用程序开发过程。为实现一个功能完整的带Java与JNI的应用程序打下基础。
本文如果读者已搭建好Android的Eclipse与NDK开发环境,包含通过ADB连接手机的配置。
点击上图右下角的activity_main.xml,当中有例如以下定义:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> 当中的字符串就定义在res目录下values目录中的strings.xml中。我们对hello_world的值做改动,例如以下:<?依据须要,能够将上图中模拟应用界面的左边栏中的控件拖入模拟应用界面上放置。对应地,XML文件里就会有该控件的布局描写叙述。xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">app</string> <string name="action_settings">Settings</string> <string name="hello_world">Napolean: Hello Android Application!</string> </resources>
package com.napolean.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@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;
}
} 插入手机连接上ADB,或者配置一个Android模拟器,点击执行程序,就可以看到应用程序执行并显示我们所改动的字符串。关于手机怎样通过ADB连接以及Android模拟器的配置。在此不作展开。package com.napolean.app;
public class NativeInterface {
private native void native_init();
private native void native_exit();
private native byte[] native_process(byte[] in_buffer, int width, int height);
public void NativeInit() {
native_init();
}
public void NativeExit() {
native_exit();
}
public void NativeProcess(byte[] in_buffer, int width, int height)
{
native_process(in_buffer, width, height);
}
static {
System.loadLibrary("NativeInterface");
}
}当中处理函数包括输入字节流、Buffer宽度、Buffer高度,返回处理后的字节流。
接下来三个函数各自是三个Native函数的公共接口。javah -classpath bin/classes -d jni com.napolean.app.NativeInterface执行命令后。在jni目录下就会生成一个名为com_napolean_app_NativeInterface.h的头文件。里面包括有的接口函数例如以下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_napolean_app_NativeInterface */
#ifndef _Included_com_napolean_app_NativeInterface
#define _Included_com_napolean_app_NativeInterface
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_napolean_app_NativeInterface
* Method: native_init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_napolean_app_NativeInterface_native_1init
(JNIEnv *, jobject);
/*
* Class: com_napolean_app_NativeInterface
* Method: native_exit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_napolean_app_NativeInterface_native_1exit
(JNIEnv *, jobject);
/*
* Class: com_napolean_app_NativeInterface
* Method: native_process
* Signature: ([BII)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_napolean_app_NativeInterface_native_1process
(JNIEnv *, jobject, jbyteArray, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
生成这个头文件的目的是免去手动编写JNI层的接口函数,并无其他用途,使用后可删除。在jni目录下创建NativeInterface.cpp,将上述头文件里全部内容拷入,经过改造,例如以下:#include <jni.h>
#include <android/log.h>
#define LOG_TAG "APP"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#ifndef _Included_com_napolean_app_NativeInterface
#define _Included_com_napolean_app_NativeInterface
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_napolean_app_NativeInterface
* Method: native_init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_napolean_app_NativeInterface_native_1init
(JNIEnv *, jobject)
{
LOGI("APP native init.");
}
/*
* Class: com_napolean_app_NativeInterface
* Method: native_exit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_napolean_app_NativeInterface_native_1exit
(JNIEnv *, jobject)
{
LOGI("APP native exit.");
}
/*
* Class: com_napolean_app_NativeInterface
* Method: native_process
* Signature: ([BII)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_napolean_app_NativeInterface_native_1process
(JNIEnv *, jobject, jbyteArray, jint, jint)
{
LOGI("APP native process.");
}
#ifdef __cplusplus
}
#endif
#endif
上述改造的代码主要是将函数声明改为函数定义,而且加入了信息打印的支持。点击右边的"Add...",加入四个路径。然后点击"Apply"应用。
/home/work/android/android-ndk-r9/platforms/android-18/arch-arm/usr/include/ /home/work/android/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.6/include/ /home/work/android/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include/ /home/work/android/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.6/include/
加入例如以下代码:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := NativeInterface LOCAL_SRC_FILES := NativeInterface.cpp LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)
package com.napolean.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private NativeInterface myJNI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
byte[] in_buffer = new byte[1920*1080*3/2];
int width = 1920;
int height = 1080;
myJNI = new NativeInterface();
myJNI.NativeInit();
myJNI.NativeProcess(in_buffer, width, height);
}
@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;
}
protected void onDestroy() {
myJNI.NativeExit();
super.onDestroy();
}
}
基于Eclipse的Android JNI层測试应用开发过程记录
标签:byte 分享 generated nis android jniexport eclipse ini 文件配置
原文地址:http://www.cnblogs.com/yutingliuyl/p/6994739.html