Android NDK Native Function Call Issue

后端 未结 2 1040
盖世英雄少女心
盖世英雄少女心 2020-12-21 22:38

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 impleme

相关标签:
2条回答
  • 2020-12-21 23:29

    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);
    

    On the Java side, that would be a method of class MyClass in package com.mypackage:

    native void MyMethod(int arg1);
    

    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.

    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.

    EDIT: 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.

    0 讨论(0)
  • 2020-12-21 23:30

    make sure the library you are trying to use (and its dependencies) are built for target platform (Android Native platform).

    0 讨论(0)
提交回复
热议问题