的Android NDK本地函数调用问题 [英] Android NDK Native Function Call Issue
问题描述
我试图写一个Android应用程序,它使用了GNU科学图书馆(GSL)的。特别是,我为它的BLAS执行兴趣libgslcblas.so。我决定了Android NDK的优势,并写加载库中的Java程序,并进行适当的函数调用。
I am attempting to write an Android application that makes use of the GNU Scientific Library (GSL). In particular, I am interested in 'libgslcblas.so' for its BLAS implementation. I decided to take advantage of the Android NDK and write a Java program that loads the library and makes the appropriate function calls.
要测试这是如何工作的,我试图写一个简单的程序,将加载libm.so,并进行任意函数调用。这似乎是关于一个简单的用例,因为我能想到的NDK,但我遇到了以下问题:
To test how this would work, I attempted to write a simple program that would load 'libm.so' and make an arbitrary function call. This seemed about a trivial a use-case of the NDK as I could think of, yet I ran into the following issue:
07-05 18:11:07.264: I/System.out(1298): debugger has settled (1464)
07-05 18:11:07.583: D/dalvikvm(1298): No JNI_OnLoad found in /system/lib/libm.so 0x41345988, skipping init
07-05 18:11:07.903: W/dalvikvm(1298): No implementation found for native Lissm/libctest/LibCtest;.sqrt (D)D
我的code是如下:
My code is as follows:
package issm.libctest;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class LibCtest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(R.layout.activity_lib_ctest);
double x = sqrt(4);
tv.setText( "Sine of: " + x);
setContentView(tv);
}
public native double sqrt(double x);
static
{
System.load("/system/lib/libm.so");
}
}
我真的不明白的问题。正如我所说的,这是最简单的使用NDK的我能想到的。有人可以帮我解决这个问题?
I really do not understand the problem. As I said, this is the simplest use of the NDK I can think of. Can someone help me resolve this issue?
谢谢!
推荐答案
阅读上JNI。总之,任意全局C函数不是从Java调用通过NDK。为了能够调用一个函数需要一个非常特殊的签名,大致为:
Read up on JNI. In short, arbitrary global C functions are NOT callable from Java via NDK. In order to be callable, a function needs a very specific signature, along the lines of:
void Java_com_mypackage_MyClass_MyMethod(JNIEnv *jni, jobject thiz, jint arg1);
在Java方面,这将是在包中的类 MyClass的
com.mypackage
的方法:
On the Java side, that would be a method of class MyClass
in package com.mypackage
:
native void MyMethod(int arg1);
Obviosly,香草开方
将不符合要求。所以,你需要提供一个Java友好的包装,每一个你打算从Java调用函数。考虑到这一点,你可能会想提供封装更高层次的概念,仅仅数学元。在Java / C边境混乱不堪;越少,你穿过它,就更好了。
Obviosly, vanilla sqrt
won't fit the bill. So you need to provide a Java-friendly wrapper for every function you intend to call from Java. With that in mind, you'll probably want to provide wrappers for higher-level concepts that mere math primitives. The Java/C border is messy; the less you cross it, the better.
此外,库需要从源专为Android的待建。 NDK具有用于该工具。一个现成的二进制为另一个平台(例如,Linux或Windows)将无法使用。
Also, the library needs to be built from sources specifically for Android. NDK has the tools for that. A ready-made binary for another platform (say, Linux or Windows) won't be usable.
编辑:从JDK的 javah的
工具,可以采取一个Java类和一帮本地的
申报,骨架H / C文件虚拟实现。从本质上讲,翻译从Java方法的原型沿JNI的规则下进行。
The javah
tool from JDK can take a Java class with a bunch of native
declarations and make skeleton H/C files with dummy implementations. Essentially, translate the method prototypes from Java to C along the JNI rules.
这篇关于的Android NDK本地函数调用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!