0

Totally lost, spent days in NDK errors, please help

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


D/mytag(27455): before_lib
D/dalvikvm(27455): Trying to load lib /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
D/dalvikvm(27455): Added shared lib /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
I/native_tag(27455): Jni_OnLoad_Called
W/dalvikvm(27455): JNI_OnLoad returned bad version (-1) in /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
W/dalvikvm(27455): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lc/google/facedetect/FaceDetect;
W/dalvikvm(27455): Class init failed in newInstance call (Lc/google/facedetect/FaceDetect;)
W/dalvikvm(27455): threadid=1: thread exiting with uncaught exception (group=0x40018578)
E/AndroidRuntime(27455): FATAL EXCEPTION: main
E/AndroidRuntime(27455): java.lang.ExceptionInInitializerError
E/AndroidRuntime(27455): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(27455): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(27455): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(27455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
E/AndroidRuntime(27455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
E/AndroidRuntime(27455): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(27455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
E/AndroidRuntime(27455): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27455): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(27455): at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(27455): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27455): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(27455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
E/AndroidRuntime(27455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E/AndroidRuntime(27455): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(27455): Caused by: java.lang.NoClassDefFoundError: C
E/AndroidRuntime(27455): at java.lang.Runtime.nativeLoad(Native Method)
E/AndroidRuntime(27455): at java.lang.Runtime.loadLibrary(Runtime.java:432)
E/AndroidRuntime(27455): at java.lang.System.loadLibrary(System.java:554)
E/AndroidRuntime(27455): at c.google.facedetect.FaceDetect.(FaceDetect.java:62)
E/AndroidRuntime(27455): ... 15 more
E/AndroidRuntime(27455): Caused by: java.lang.ClassNotFoundException: C in loader dalvik.system.PathClassLoader[/data/app/c.google.facedetect-1.apk]
E/AndroidRuntime(27455): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
E/AndroidRuntime(27455): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
E/AndroidRuntime(27455): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(27455): ... 19 more

The pacakage name is c.google.facedetect

My java class is FaceDetect.java and is as follows:

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

my jni generated header is c_google_facedetect_FaceDetect.h and is placed in the jni folder and is as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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

My C file is called face-detect.c and is placed in the jni folder and is as follows:

#include <jni.h>
#include <stdio.h>
#include <android/log.h>

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 < sz)
    {
        int tmp[sz];
        rgbData = &tmp[0];
        rgbDataSize = sz;
        //__android_log_write(ANDROID_LOG_INFO, "JNI", "alloc");
    }

    for (j = 0; j < h; j++)
    {
        pixPtr = j * w;
        jDiv2 = j >> 1;
        for (i = 0; i < w; i++)
        {
            Y = yuv[pixPtr];
            if (Y < 0)
                Y += 255;
            if ((i & 0x1) != 1)
            {
                cOff = sz + jDiv2 * w + (i >> 1) * 2;
                Cb = yuv[cOff];
                if (Cb < 0)
                    Cb += 127;
                else
                    Cb -= 128;
                Cr = yuv[cOff + 1];
                if (Cr < 0)
                    Cr += 127;
                else
                    Cr -= 128;
            }
            R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
            if (R < 0)
                R = 0;
            else
                if (R > 255)
                    R = 255;
            G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3)
                    + (Cr >> 4) + (Cr >> 5);
            if (G < 0)
                G = 0;
            else
                if (G > 255)
                    G = 255;
            B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
            if (B < 0)
                B = 0;
            else
                if (B > 255)
                    B = 255;
            rgbData[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
        }
    }
    (*env)->SetIntArrayRegion(env, rgb, 0, sz, (jint *) &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)->GetEnv(jvm, (void **) &env, JNI_VERSION_1_2))
    {
        return JNI_ERR; /* JNI version not supported */
    }
    cls = (*env)->FindClass(env, "C");
    if (cls == NULL)
    {
        return JNI_ERR;
    }
    /* Use weak global ref to allow C class to be unloaded */
    Class_C = (*env)->NewWeakGlobalRef(env, cls);
    if (Class_C == NULL)
    {
        return JNI_ERR;
    }
    /* Compute and cache the method ID */
    MID_C_g = (*env)->GetMethodID(env, cls, "g", "()V");
    if (MID_C_g == NULL)
    {
        return JNI_ERR;
    }

    return JNI_VERSION_1_2;
}

My Android.mk file is placed in the jni folder and is as follows:

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)

The libs/armeabi folder in my project contains the libface-detect.so, which you can find here http://uploading.com/files/get/28f3c9bd/libface-detect.so

I still don't know what's wrong, please help

Mohamed Heiba
  • 1,813
  • 4
  • 38
  • 67

3 Answers3

1

Well, it looks like my fault. In the comment on your very first question i have given you a link to JNI book about how to write JNI_OnLoad as a proof that your library is indeed being correctly loaded. So now you see your logcat statement, so yes, your library loads correctly. I really didn't expect that you copy&paste the whole JNI_OnLoad example from JNI book. Complete with finding classes which you don't have in your Java code :) I told you that you only need the return statement.

Community
  • 1
  • 1
Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
0

It looks like it can not find class 'C' or method 'g', do you indeed have such classes in your Java program?

Usually they have something like FindClass("myorg/mypackage/myclass");

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

I rebuilt ndk and rebuilt the java jni header file and the program worked, no idea how

Mohamed Heiba
  • 1,813
  • 4
  • 38
  • 67