我試圖按照本教程 http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html 但我在日食使得Android應用的Android通話功能
所以我創建空白項目,並添加一個新的類
package com.example.jpgtest;
import android.util.Log;
public class TestJNIPrimitive {
//static {
//System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes)
// System.load("d:/libjpeg.so");
// Log.d("TestJNIPrimitive", "load ok");
// }
// Declare a native method average() that receives two ints and return a double containing the average
private native double average(int n1, int n2);
// Test Driver
public static void main() {
System.load("d:/libjpeg.so");
Log.d("TestJNIPrimitive", "load ok");
double d = new TestJNIPrimitive().average(3, 2);
//double d = new TestJNIPrimitive().average(3, 2);
Log.d("TestJNIPrimitive", "d="+d);
}
}
和onCreate()
TestJNIPrimitive t = new TestJNIPrimitive();
t.main();
我做了C和H文件中像這樣叫它TestJNIPrimitive.h /
* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestJNIPrimitive */
#ifndef _Included_TestJNIPrimitive
#define _Included_TestJNIPrimitive
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestJNIPrimitive
* Method: average
* Signature: (II)D
*/
JNIEXPORT jdouble JNICALL Java_TestJNIPrimitive_average
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
TestJNIPrimitive.c
#include <jni.h>
#include "TestJNIPrimitive.h"
JNIEXPORT jdouble JNICALL Java_TestJNIPrimitive_average(JNIEnv *env, jobject thisObj, jint n1, jint n2)
{
jdouble result;
//printf("In C, the numbers are %d and %d\n", n1, n2);
result = ((jdouble)n1 + n2)/2.0;
// jint is mapped to int, jdouble is mapped to double
return result;
}
我建立並且作出libjpg.so在我的d盤的根 我檢查符號
$ readelf -Ws /cygdrive/d/libjpeg.so | grep average
272: 00003f5c 72 FUNC GLOBAL DEFAULT 7 Java_TestJNIPrimitive_average
1896: 00003f5c 72 FUNC GLOBAL DEFAULT 7 Java_TestJNIPrimitive_average
當我運行它,負載工作正常,但平均調用失敗, 沒有發現原生Lcom/example/jpegtest/TestJNIPrimative ;。平均(II)d
任何想法請,我真的卡在這個
感謝