如何从.c文件中调用方法到NDK?

时间:2012-02-07 10:01:47

标签: android android-ndk

我的gnutls .c文件中有一个方法如下

 int gnutls_error_is_fatal (int error)
        {
   int ret = 1;
   const gnutls_error_entry *p;

  /* Input sanitzation.  Positive values are not errors at all, and
   definitely not fatal. */
  if (error > 0)
  return 0;

   for (p = error_algorithms; p->desc != NULL; p++)
    {
    if (p->number == error)
      {
        ret = p->fatal;
       break;
       }
      }

        return ret;
        }

我如何在我的活动类中定义它,以便我能够使用它并获得返回值?

我试过

     private native int gnutls_error_is_fatal(int i);


     @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // this is where we call the native code
      int hello = gnutls_error_is_fatal(1);

但无法正常工作

给出以下错误

    E/AndroidRuntime(  756): FATAL EXCEPTION: main
E/AndroidRuntime(  756): java.lang.ExceptionInInitializerError
E/AndroidRuntime(  756):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  756):    at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(  756):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(  756):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime(  756):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(  756):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(  756):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(  756):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  756):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  756):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  756):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  756):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  756):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  756):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  756):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  756): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load REALGnuTLS: findLibrary returned null
E/AndroidRuntime(  756):    at java.lang.Runtime.loadLibrary(Runtime.java:429)
E/AndroidRuntime(  756):    at java.lang.System.loadLibrary(System.java:554)
E/AndroidRuntime(  756):    at com.test.example.REALGnuTLSActivity.<clinit>(REALGnuTLSActivity.java:12)

1 个答案:

答案 0 :(得分:1)

您是否正确构建了本机库并生成了jni标头?

查看本教程:

http://marakana.com/forums/android/examples/49.html

它有点过时,但一般方法应该是相同的。