在Android運行NDK
(在此不解釋如何安裝NDK)
運行平台:Archlinux 64
1.建立Android 專案檔
$android create project -p ./ -t 3 -k net.aminzai.myjni -a MyJniActivity
2.編輯MyJniActivity
package net.aminzai.myjni;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MyJniActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(getString());
setContentView(tv);
}
public native String getString();
static{
System.loadLibrary("net_aminzai_myjni_MyJniActivity");
}
}
import android.os.Bundle;
import android.widget.TextView;
public class MyJniActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(getString());
setContentView(tv);
}
public native String getString();
static{
System.loadLibrary("net_aminzai_myjni_MyJniActivity");
}
}
3.編譯java file
$ant compile
4.產生所需要的c header
$cd bin/classes
$javah -jni net.aminzai.myjni.MyJniActivity
$cd (Project-Root)
5.建立Android的jni 資料夾
$mkdir jni
6.將header file 移至 jni資料夾底下
$mv bin/classes/net_aminzai_myjni_MyJniActivity.h jni
會看到裏面的內容是
$cat ./jni/net_aminzai_myjni_MyJniActivity.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class net_aminzai_myjni_MyJniActivity */
#ifndef _Included_net_aminzai_myjni_MyJniActivity
#define _Included_net_aminzai_myjni_MyJniActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: net_aminzai_myjni_MyJniActivity
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_net_aminzai_myjni_MyJniActivity_getString
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class net_aminzai_myjni_MyJniActivity */
#ifndef _Included_net_aminzai_myjni_MyJniActivity
#define _Included_net_aminzai_myjni_MyJniActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: net_aminzai_myjni_MyJniActivity
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_net_aminzai_myjni_MyJniActivity_getString
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
7.建立 c file
$vi jni/net_aminzai_myjni_MyJniActivity.c
#include"net_aminzai_myjni_MyJniActivity.h"
/*
* Class: net_aminzai_myjni_MyJniActivity
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_net_aminzai_myjni_MyJniActivity_getString
(JNIEnv * env, jobject x){
return (*env)->NewStringUTF(env, "Complex string calculated in native code");
}
/*
* Class: net_aminzai_myjni_MyJniActivity
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_net_aminzai_myjni_MyJniActivity_getString
(JNIEnv * env, jobject x){
return (*env)->NewStringUTF(env, "Complex string calculated in native code");
}
8.產生編譯用的makefile
$vi (Project_Root)/Application.mk
APP_PROJECT_PATH:=$(call my-dir)
APP_MODULE := net_aminzai_myjni_MyJniActivity
APP_MODULE := net_aminzai_myjni_MyJniActivity
$vi (Project_Root)/jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := net_aminzai_myjni_MyJniActivity
LOCAL_SRC_FILES := net_aminzai_myjni_MyJniActivity.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := net_aminzai_myjni_MyJniActivity
LOCAL_SRC_FILES := net_aminzai_myjni_MyJniActivity.c
include $(BUILD_SHARED_LIBRARY)
9.編譯 c file
$ndk-build
Compile thumb : net_aminzai_myjni_MyJniActivity <= net_aminzai_myjni_MyJniActivity.c
SharedLibrary : libnet_aminzai_myjni_MyJniActivity.so
Install : libnet_aminzai_myjni_MyJniActivity.so => libs/armeabi/libnet_aminzai_myjni_MyJniActivity.so
Compile thumb : net_aminzai_myjni_MyJniActivity <= net_aminzai_myjni_MyJniActivity.c
SharedLibrary : libnet_aminzai_myjni_MyJniActivity.so
Install : libnet_aminzai_myjni_MyJniActivity.so => libs/armeabi/libnet_aminzai_myjni_MyJniActivity.so
10.編譯&安裝apk
$ant install
11.運行
沒有留言:
張貼留言