Android NDK开发的环境配置和使用

来源:互联网 发布:手机压缩图片软件 编辑:程序博客网 时间:2024/05/17 22:12

安装步骤:

1.安装ADT,其中的NDK也要安装;

2.下载对应版本的NDK,解压到对应目录下;

3.进入eclipse-->window-->preference-->Android-->NDK下,把路径设置到上面解压的目录下。

配置步骤:

1.新建Android项目,然后在项目上右键,选择Android Tools-->Add Native Support。这时,NDK工具会自动为你生成jni目录和××.cpp文件和Android.mk文件,libs/armeabi目录。


2.编写相应的Java类,然后添加native方法。(见下面代码)

3.你可以选择着控制台下进入到bin/classes目录下使用

javah -v -jni -classpath . -d ../../jni/ Java类
命令来生成对应 的头文件。但是每次这样做总感觉太麻烦,还是使用自动化工具比较好。在eclipse中选择配置外部工具,然后填入以下内容:



4.配置成功后编译下工程,然后就会自动生成对应的.so文件。

5.在需要使用native方法的类中创建native方法类的实例对象,然后对方法进行引用即可。(见下面代码)


小例子:

1.将字符串写入sd卡目录下的文件中

2.将sd卡目录下文件中的字符串读取出来

/** Author:wenix * Date:2013-5-9 * File Name:MainActivity.java */package com.wenix.webcam;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv;static{System.loadLibrary("AndroidWebCam");//加载so库,不要加lib前缀和.so后缀}private String sdcardPath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);try {sdcardPath = Environment.getExternalStorageDirectory().getCanonicalPath();} catch (IOException e) {e.printStackTrace();}tv = (TextView)findViewById(R.id.tv);NativeFileUtils nfu = new NativeFileUtils();nfu.writeString2File("我是中国人,我爱中国", sdcardPath+"/hello.txt");String info = nfu.readStringFromFile(sdcardPath+"/hello.txt");tv.setText(info);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

含有本地方法的类:

package com.wenix.webcam;public class NativeFileUtils {public native void writeString2File(String info, String filePath);public native String readStringFromFile(String filePath);}

c代码:

#include <jni.h>#include <stdio.h>#include <stdlib.h>#include <android/log.h>#include "com_wenix_webcam_NativeFileUtils.h"#define LOG_V(TAG, LOG) (__android_log_print(ANDROID_LOG_VERBOSE, TAG, LOG))JNIEXPORT void JNICALL Java_com_wenix_webcam_NativeFileUtils_writeString2File  (JNIEnv * env, jobject object, jstring info, jstring filePath){const char* chFilePath = env->GetStringUTFChars(filePath, 0);FILE* fp = fopen(chFilePath, "w+");env->ReleaseStringUTFChars(filePath, chFilePath);const char* chInfo = env->GetStringUTFChars(info, 0);fputs(chInfo,fp);env->ReleaseStringUTFChars(info, chInfo);fclose(fp);}JNIEXPORT jstring JNICALL Java_com_wenix_webcam_NativeFileUtils_readStringFromFile  (JNIEnv * env, jobject object, jstring filePath){const char* chFilePath = env->GetStringUTFChars(filePath, 0);FILE* fp = fopen(chFilePath, "r");env->ReleaseStringUTFChars(filePath, chFilePath);char chContent[128];fgets(chContent, 128, fp);char* log = (char*)malloc(13*sizeof(char));snprintf(log, sizeof(chContent), "size=%d,conent=%s", sizeof(chContent),chContent);LOG_V("1",log);jstring ret = env->NewStringUTF(chContent);return ret;}
Android.mk文件

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := AndroidWebCamLOCAL_SRC_FILES := AndroidWebCam.cppLOCAL_LDLIBS := -llog #log所需库,一定要加上,否则无法通过编译include $(BUILD_SHARED_LIBRARY)

记住要中AndroidManifest.xml文件中加上权限,否则无法读写文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

运行,效果如下图:

示例总结:

1.字符串在Linux平台上本来就是UTF-8编码,Java默认也是UTF-8编码,所以没有必要进行编码和解码的过程。
2.由于c/c++中没有String数据类型,所以jstring和char*之间的转化很有必要,JNI提供了转换的方法--env->GetStringUTFChars(jstring, jboolean*),但是记得用完后要使用
env->ReleaseStringUTFChars(jstring, const char*);释放掉(C/C++中内存的管理是很必要的,跟Java自动内存回收不同,尤其要注意)
3.c/c++中如何把char*类型转换为基本数据类型?c/c++提供了相应的方法,在stdlib.h头文件中,有atoi,atol,atoll等函数。
4.c/c++中如何把基本类型转换为char*类型?我没有找到itoa,ltoa等方法,所以使用了sprinf或者snprintf方法来进行转换,也很方便。

5.即使是c/c++编写的代码也无法绕过权限检查,必须要申请相应的权限,否则无法完成相应的操作。

原创粉丝点击