2014-07-03 93 views
2

我試圖按照本教程 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

任何想法請,我真的卡在這個

感謝

回答

0

我無法找出問題,而是我讀到這 https://developer.android.com/tools/sdk/ndk/index.html

我構建並運行hello-jni示例,然後構建一個新項目,然後從測試中添加到jni文件夾中並更改包名稱和類名以匹配新項目

然後我開始編輯修改功能,以我的需求和其確定現在

不是簡單的東西這個

3

你缺少packace到JNI。這就是找不到該功能的原因。

你的Java包是:com.example.jpgtest

所以,你的本地函數必須是:

JNIEXPORT jdouble JNICALL Java_com_example_jpgtest_TestJNIPrimitive_average(JNIEnv *env, jobject thisObj, jint n1, jint n2) 
{ 
    ... 
} 

編譯NDK-構建之後,並使用新libjpeg.so必須工作。

我不得不補充說,你的java看起來很奇怪(對我而言)。我將實現爲:

package com.example.jpgtest; 

import android.util.Log; 

public class TestJNIPrimitive 
{ 
    // Test Driver 
    public static void main() 
    { 
      Log.d("TestJNIPrimitive", "load ok"); 

      double d = new TestJNIPrimitive().average(3, 2); 
      Log.d("TestJNIPrimitive", "d="+d); 
    } 
    // Native functions (Callbacks from Java to C++) 
    // ========================================================================= 
    // Declare a native method average() that receives two ints and return a double containing the average 
     private native double average(int n1, int n2); 

    // Load Native Libraries 
    // ========================================================================= 
    static 
    { 
     System.load("libjpeg.so"); 
    } 
} 

而且因此必須是文件夾裏面的庫/ armeabi-V7A

我你想有一個基本的理論支票thisspecifications