Eclipse android ndk 開發

Eclipse Android NDK 開發

下載NDK

由於Google被屏蔽,android官網也被屏蔽了,推薦去這個地址去下載,關於android開發的內容全都有

android ndk 下載地址

下載完之後配置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 配置問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章