Eclipse Android NDK 開發
下載NDK
由於Google被屏蔽,android官網也被屏蔽了,推薦去這個地址去下載,關於android開發的內容全都有
下載完之後配置NDK環境,詳細就不說了,以上網址也有相關步驟,也可以問度娘和股溝
在eclipse中新建工程
創建成功後
建立用於jni映射的java文件
HelloUtils.java
package com.example.jnitest;
public class HelloUtils {
static{
System.loadLibrary("hello");
}
public native static String toastHello();
public native static void logHello();
}
MainActivity.java
package com.example.jnitest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button btnf;
private Button btns;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView(){
btnf=(Button) findViewById(R.id.btn_first);
btns=(Button) findViewById(R.id.btn_second);
btnf.setOnClickListener(this);
btns.setOnClickListener(this);
}
private void initData(){
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_first:
toastHello();
break;
case R.id.btn_second:
logHello();
break;
default:
break;
}
}
private void toastHello(){
Toast.makeText(this, HelloUtils.toastHello(), Toast.LENGTH_SHORT).show();
}
private void logHello(){
HelloUtils.logHello();
}
}
佈局文件 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="say hello world"/>
<Button
android:id="@+id/btn_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="log hello world"/>
</LinearLayout>
創建完畢後,編譯一下項目,生成了class文件
生成相關的頭文件
打開cmd 進入到項目的classes文件夾下
使用javah工具生成頭文件
javah com.example.jnitest.HelloUtils
生成了相關的頭文件
新建jni文件夾用來存放ndk開發相關代碼
將上一步產生的com_example_jnitest_HelloUtils.h
文件移動到jni文件夾下
爲了方便將其重命名爲HelloUtils.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jnitest_HelloUtils */
#ifndef _Included_com_example_jnitest_HelloUtils
#define _Included_com_example_jnitest_HelloUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jnitest_HelloUtils
* Method: toastHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_jnitest_HelloUtils_toastHello
(JNIEnv *, jclass);
/*
* Class: com_example_jnitest_HelloUtils
* Method: logHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_jnitest_HelloUtils_logHello
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
將其copy一份重命名爲HelloUtils.cpp
文件放在jni
文件夾下
並修改如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include "android/log.h"
#include "HelloUtils.h"
#define LOG_TAG "HelloUtils"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jnidemo_HelloUtils
* Method: toastHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_jnidemo_HelloUtils_toastHello
(JNIEnv *env, jclass){
char* tmpstr = "hello world";
return env->NewStringUTF(tmpstr);
};
/*
* Class: com_example_jnidemo_HelloUtils
* Method: logHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_jnidemo_HelloUtils_logHello
(JNIEnv *, jclass){
LOGD("hello world!");
};
#ifdef __cplusplus
}
#endif
創建Android.mk文件和Application.mk文件
相關文件的意義和變量的意義,書寫方式,可以去Android官網NDK開發部分查看
具體文件如下:
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello
LOCAL_LDLIBS:=-llog
LOCAL_SRC_FILES := HelloUtils.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM :=android-14
對項目添加C++支持
右鍵項目—-》》new—-》》other
完成之後
修改編譯配置
配置完成後,運行項目OK
如果打開代碼出現代碼審查問題,參考本人以前的文章
Android OpenCV NDK 配置問題