title: java.lang.NoClassDefFoundError at System.loadLibrary in Android NDK
body: <p>Totally lost, spent days in NDK errors, please help</p>
  
  <p>I'm trying to call a function written in C from my java class to use it in an android application. But when I run the application, I get the following logcat</p>
  
  <blockquote>
    <p><br/>D/mytag(27455): before_lib <br/>D/dalvikvm(27455): Trying to load
    lib /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
    <br/>D/dalvikvm(27455): Added shared lib
    /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
    <br/>I/native_tag(27455): Jni_OnLoad_Called <br/>W/dalvikvm(27455):
    JNI_OnLoad returned bad version (-1) in
    /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
    <br/>W/dalvikvm(27455): Exception Ljava/lang/NoClassDefFoundError;
    thrown while initializing Lc/google/facedetect/FaceDetect;
    <br/>W/dalvikvm(27455): Class init failed in newInstance call
    (Lc/google/facedetect/FaceDetect;) <br/>W/dalvikvm(27455): threadid=1:
    thread exiting with uncaught exception (group=0x40018578)
    <br/>E/AndroidRuntime(27455): FATAL EXCEPTION: main
    <br/>E/AndroidRuntime(27455): java.lang.ExceptionInInitializerError
    <br/>E/AndroidRuntime(27455):     at
    java.lang.Class.newInstanceImpl(Native Method)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.Class.newInstance(Class.java:1409)
    <br/>E/AndroidRuntime(27455):     at
    android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    <br/>E/AndroidRuntime(27455):     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
    <br/>E/AndroidRuntime(27455):     at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    <br/>E/AndroidRuntime(27455):     at
    android.app.ActivityThread.access$1500(ActivityThread.java:117)
    <br/>E/AndroidRuntime(27455):     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    <br/>E/AndroidRuntime(27455):     at
    android.os.Handler.dispatchMessage(Handler.java:99)
    <br/>E/AndroidRuntime(27455):     at
    android.os.Looper.loop(Looper.java:130) <br/>E/AndroidRuntime(27455):
      at android.app.ActivityThread.main(ActivityThread.java:3687)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.reflect.Method.invokeNative(Native Method)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.reflect.Method.invoke(Method.java:507)
    <br/>E/AndroidRuntime(27455):     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    <br/>E/AndroidRuntime(27455):     at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    <br/>E/AndroidRuntime(27455):     at
    dalvik.system.NativeStart.main(Native Method)
    <br/>E/AndroidRuntime(27455): Caused by:
    java.lang.NoClassDefFoundError: C <br/>E/AndroidRuntime(27455):   at
    java.lang.Runtime.nativeLoad(Native Method)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.Runtime.loadLibrary(Runtime.java:432)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.System.loadLibrary(System.java:554)
    <br/>E/AndroidRuntime(27455):     at
    c.google.facedetect.FaceDetect.(FaceDetect.java:62)
    <br/>E/AndroidRuntime(27455):     ... 15 more
    <br/>E/AndroidRuntime(27455): Caused by:
    java.lang.ClassNotFoundException: C in loader
    dalvik.system.PathClassLoader[/data/app/c.google.facedetect-1.apk]
    <br/>E/AndroidRuntime(27455):     at
    dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    <br/>E/AndroidRuntime(27455):     at
    java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    <br/>E/AndroidRuntime(27455):     ... 19 more</p>
  </blockquote>
  
  <p>The pacakage name is c.google.facedetect</p>
  
  <p>My java class is FaceDetect.java and is as follows:</p>
  
  <pre><code>package c.google.facedetect;
  
  import android.app.Activity;
  .
  .
  
  public class FaceDetect extends Activity implements SurfaceHolder.Callback, Camera.PreviewCallback
  {
  .
  .
      int[] argb8888 = new int[CAMERA_WIDTH * CAMERA_HEIGHT];
  
      static
      {
          Log.d("mytag", "before_lib");
          System.loadLibrary("face-detect");
          Log.d("mytag", "after_lib");
      }
      .
      .
      public void onCreate(Bundle savedInstanceState)
      {
      .
      .
          decodeYUV(argb8888, data, CAMERA_WIDTH, CAMERA_HEIGHT);
          .
          .
      }
  
      public static native void decodeYUV(int[] out, byte[] fg, int width, int height);
  }
  </code></pre>
  
  <p>my jni generated header is c_google_facedetect_FaceDetect.h and is placed in the jni folder and is as follows:</p>
  
  <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */
  #include &lt;jni.h&gt;
  /* Header for class c_google_facedetect_FaceDetect */
  
  #ifndef _Included_c_google_facedetect_FaceDetect
  #define _Included_c_google_facedetect_FaceDetect
  #ifdef __cplusplus
  extern "C" {
  #endif
  #undef c_google_facedetect_FaceDetect_CAMERA_WIDTH
  #define c_google_facedetect_FaceDetect_CAMERA_WIDTH 480L
  #undef c_google_facedetect_FaceDetect_CAMERA_HEIGHT
  #define c_google_facedetect_FaceDetect_CAMERA_HEIGHT 320L
  /*
   * Class:     c_google_facedetect_FaceDetect
   * Method:    decodeYUV
   * Signature: ([I[BII)V
   */
  JNIEXPORT void JNICALL Java_c_google_facedetect_FaceDetect_decodeYUV(JNIEnv *, jclass, jintArray, jbyteArray, jint, jint);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p>My C file is called face-detect.c and is placed in the jni folder and is as follows:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;android/log.h&gt;
  
  int* rgbData;
  int rgbDataSize = 0;
  
  JavaVM *cached_jvm;
  jclass Class_C;
  jmethodID MID_C_g;
  
  JNIEXPORT void JNICALL Java_c_google_facedetect_FaceDetect_decodeYUV(
          JNIEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp,
          jint width, jint height)
  {
      int sz;
      int i;
      int j;
      int Y;
      int Cr = 0;
      int Cb = 0;
      int pixPtr = 0;
      int jDiv2 = 0;
      int R = 0;
      int G = 0;
      int B = 0;
      int cOff;
      int w = width;
      int h = height;
      sz = w * h;
  
      jbyte* yuv = yuv420sp;
      if (rgbDataSize &lt; sz)
      {
          int tmp[sz];
          rgbData = &amp;tmp[0];
          rgbDataSize = sz;
          //__android_log_write(ANDROID_LOG_INFO, "JNI", "alloc");
      }
  
      for (j = 0; j &lt; h; j++)
      {
          pixPtr = j * w;
          jDiv2 = j &gt;&gt; 1;
          for (i = 0; i &lt; w; i++)
          {
              Y = yuv[pixPtr];
              if (Y &lt; 0)
                  Y += 255;
              if ((i &amp; 0x1) != 1)
              {
                  cOff = sz + jDiv2 * w + (i &gt;&gt; 1) * 2;
                  Cb = yuv[cOff];
                  if (Cb &lt; 0)
                      Cb += 127;
                  else
                      Cb -= 128;
                  Cr = yuv[cOff + 1];
                  if (Cr &lt; 0)
                      Cr += 127;
                  else
                      Cr -= 128;
              }
              R = Y + Cr + (Cr &gt;&gt; 2) + (Cr &gt;&gt; 3) + (Cr &gt;&gt; 5);
              if (R &lt; 0)
                  R = 0;
              else
                  if (R &gt; 255)
                      R = 255;
              G = Y - (Cb &gt;&gt; 2) + (Cb &gt;&gt; 4) + (Cb &gt;&gt; 5) - (Cr &gt;&gt; 1) + (Cr &gt;&gt; 3)
                      + (Cr &gt;&gt; 4) + (Cr &gt;&gt; 5);
              if (G &lt; 0)
                  G = 0;
              else
                  if (G &gt; 255)
                      G = 255;
              B = Y + Cb + (Cb &gt;&gt; 1) + (Cb &gt;&gt; 2) + (Cb &gt;&gt; 6);
              if (B &lt; 0)
                  B = 0;
              else
                  if (B &gt; 255)
                      B = 255;
              rgbData[pixPtr++] = 0xff000000 + (B &lt;&lt; 16) + (G &lt;&lt; 8) + R;
          }
      }
      (*env)-&gt;SetIntArrayRegion(env, rgb, 0, sz, (jint *) &amp;rgbData[0]);
  }
  
  JNIEXPORT jint JNICALL
  JNI_OnLoad(JavaVM *jvm, void *reserved)
  {
      JNIEnv *env;
      jclass cls;
      cached_jvm = jvm; /* cache the JavaVM pointer */
  
      __android_log_write(ANDROID_LOG_INFO, "native_tag", "Jni_OnLoad_Called");
  
      if ((*jvm)-&gt;GetEnv(jvm, (void **) &amp;env, JNI_VERSION_1_2))
      {
          return JNI_ERR; /* JNI version not supported */
      }
      cls = (*env)-&gt;FindClass(env, "C");
      if (cls == NULL)
      {
          return JNI_ERR;
      }
      /* Use weak global ref to allow C class to be unloaded */
      Class_C = (*env)-&gt;NewWeakGlobalRef(env, cls);
      if (Class_C == NULL)
      {
          return JNI_ERR;
      }
      /* Compute and cache the method ID */
      MID_C_g = (*env)-&gt;GetMethodID(env, cls, "g", "()V");
      if (MID_C_g == NULL)
      {
          return JNI_ERR;
      }
  
      return JNI_VERSION_1_2;
  }
  </code></pre>
  
  <p>My Android.mk file is placed in the jni folder and is as follows:</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  
  include $(CLEAR_VARS)
  
  LOCAL_MODULE    := face-detect
  LOCAL_SRC_FILES := face-detect.c
  
  LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>The libs/armeabi folder in my project contains the libface-detect.so, which you can find here <a href="http://uploading.com/files/get/28f3c9bd/libface-detect.so" rel="nofollow">http://uploading.com/files/get/28f3c9bd/libface-detect.so</a></p>
  
  <p>I still don't know what's wrong, please help</p>