Android ndk windows下使用环境设置与编程实例:由.java自动生成xxx.h
2013-07-16 18:13
585 查看
思路:
本文从java到c 自动生成jni的xx.h文件
在eclipse中创建好xx.java文件,然后编译在相应的bin目录下生成xx.class文件,要生成xx.h的jni头文件就需要这个.class文件,
使用javah command 格式如下:javah -jni -classpath xxx.class
注意路径指定到.class文件的包文件夹的上一层,例如要编译bin\classes\com\example\hellojni\HelloJni.class 文件,
需要cd 到classes\下就可
然后执行命令:
javah -jni -classpath . com.example.hellojni.HelloJni
会在classes\ 生成com_example_hellojni_HelloJni.h 文件
Ps: 有个‘.’在 -classpath 命令后。
code 如下:HelloJni.java
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
TextView tv ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText( "get string from c: " + printHexString( getByteArray("2341")) );
setContentView(tv);
}
public native String stringFromJNI();
public native String[] getStringArray();
public native byte[] getByteArray(String userPin);
static {
System.loadLibrary("hello-jni");
}
}
编译后自动生成HelloJni.h,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellojni_HelloJni */
#ifndef _Included_com_example_hellojni_HelloJni
#define _Included_com_example_hellojni_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_hellojni_HelloJni
* Method: stringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *, jobject);
/*
* Class: com_example_hellojni_HelloJni
* Method: getStringArray
* Signature: ()[Ljava/lang/String;
*/
JNIEXPORT jobjectArray JNICALL Java_com_example_hellojni_HelloJni_getStringArray
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
然后在.cpp中实现具体的函数功能:
#include <com_example_hellojni_HelloJni.h>
#include <stdio.h>
#include <string.h>
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *env, jobject thiz)
{
return (*env)->NewStringUTF(env,"the log of android is mad");
}
JNIEXPORT jbyteArray JNICALL Java_com_example_hellojni_HelloJni_getByteArray
(JNIEnv *env, jobject thiz, jstring jstr)
{
char const *bStr = NULL;
// jstring 2 char*
bStr = (*env)->GetStringUTFChars(env,jstr, 0);
jbyteArray RtnArr = NULL;
RtnArr = (*env)->NewByteArray(env,strlen(bStr));
(*env)->SetByteArrayRegion(env,RtnArr, 0, strlen(bStr), (jbyte*)bStr);
// if(bStr)
// {
// free(bStr);
// }
return RtnArr;
}
完成之后就可以编译了。
本文从java到c 自动生成jni的xx.h文件
在eclipse中创建好xx.java文件,然后编译在相应的bin目录下生成xx.class文件,要生成xx.h的jni头文件就需要这个.class文件,
使用javah command 格式如下:javah -jni -classpath xxx.class
注意路径指定到.class文件的包文件夹的上一层,例如要编译bin\classes\com\example\hellojni\HelloJni.class 文件,
需要cd 到classes\下就可
然后执行命令:
javah -jni -classpath . com.example.hellojni.HelloJni
会在classes\ 生成com_example_hellojni_HelloJni.h 文件
Ps: 有个‘.’在 -classpath 命令后。
code 如下:HelloJni.java
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
TextView tv ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText( "get string from c: " + printHexString( getByteArray("2341")) );
setContentView(tv);
}
public native String stringFromJNI();
public native String[] getStringArray();
public native byte[] getByteArray(String userPin);
static {
System.loadLibrary("hello-jni");
}
}
编译后自动生成HelloJni.h,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellojni_HelloJni */
#ifndef _Included_com_example_hellojni_HelloJni
#define _Included_com_example_hellojni_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_hellojni_HelloJni
* Method: stringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *, jobject);
/*
* Class: com_example_hellojni_HelloJni
* Method: getStringArray
* Signature: ()[Ljava/lang/String;
*/
JNIEXPORT jobjectArray JNICALL Java_com_example_hellojni_HelloJni_getStringArray
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
然后在.cpp中实现具体的函数功能:
#include <com_example_hellojni_HelloJni.h>
#include <stdio.h>
#include <string.h>
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *env, jobject thiz)
{
return (*env)->NewStringUTF(env,"the log of android is mad");
}
JNIEXPORT jbyteArray JNICALL Java_com_example_hellojni_HelloJni_getByteArray
(JNIEnv *env, jobject thiz, jstring jstr)
{
char const *bStr = NULL;
// jstring 2 char*
bStr = (*env)->GetStringUTFChars(env,jstr, 0);
jbyteArray RtnArr = NULL;
RtnArr = (*env)->NewByteArray(env,strlen(bStr));
(*env)->SetByteArrayRegion(env,RtnArr, 0, strlen(bStr), (jbyte*)bStr);
// if(bStr)
// {
// free(bStr);
// }
return RtnArr;
}
完成之后就可以编译了。
相关文章推荐
- Android ndk windows下使用环境设置与编程实例:由.java自动生成xxx.h
- android使用ant自动打包(更改java文件中的属性值,批量自动生成)
- Windows下使用批处理自动设置Java环境
- wsdl2java 生成客户端: 具有相同名称 "xxx" 的类/接口已在使用。请使用类定制设置来解决此冲突。
- Windows下使用批处理自动设置Java环境
- android环境不能自动生成R.java文件的问题
- 谨记:android编程R.java未自动生成的重要原因之一
- 【CXF】wsdl2java 生成客户端: 具有相同名称 "xxx" 的类/接口已在使用。请使用类定制设置来解决此冲突。
- android使用ant自动打包(更改java文件中的属性值,批量自动生成)
- Android 学习使用annotationprocessor自动生成java文件
- Hessian远程调用框架进阶教程(一) Hessian介绍JAVA使用Hessian进行编程实例
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格?[转]
- 实例介绍 Java(android) 回调函数使用方法
- Android及Java使用google.zxing生成和解码(解析)二维码(QRCode)
- Android Studio开发环境建立aidl文件,怎么生成相应的java文件? Android Studio开发环境建立aidl文件,不自动生成java文件?在Eclipse中建立aidl文件后在
- Android中的基础-----如何使用JAVA动态的添加并设置组件属性
- 安卓Android ROM定制、移植,安卓软件反编译、汉化实战教程第一篇:JAVA环境设置。
- Java多线程编程之使用Exchanger数据交换实例
- Android 自动编译、打包生成apk文件 2 - 使用原生Ant方式
- 使用jsonschema2pojo根据JSON文件自动生成Java POJO类(Java Bean)源文件