Hi,

I am porting one multi-threaded native code to android platform,
created shared library,

I am using some of the Android java applications by using JNI, when i
call Java code from C, i am getting  NoClassDefFoundError Exception on
runtime,

E/MY-ALSA ( 3508): JNI_OnLoad called
E/MY-ALSA ( 3508): initClassHelper: failed to get  class reference
W/dalvikvm( 3508): Exception Ljava/lang/NoClassDefFoundError; thrown
while initializing Lcom/example/hellojni/HelloJni;
W/dalvikvm( 3508): Class init failed in newInstance call (Lcom/example/
hellojni/HelloJni;)
D/AndroidRuntime( 3508): Shutting down VM
W/dalvikvm( 3508): threadid=1: thread exiting with uncaught exception
(group=0xb586a4f0)

Below is the Java and native code, and please help me to resolve it, i
am new to java.






/* ----------------------------------------------------  Java code
--------------------------------------------------------------------
*/

public class HelloJni extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
                /* Create a TextView and set its content.
             * the text is retrieved by calling a native
         * function.
         */
            TextView  tv = new TextView(this);

                tv.setText( stringFromJNI() );
                setContentView(tv);

  }

  public boolean javaFunc(String s)
  {
                /* Do some application work */
     return true;

  }


    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();


    static {
        System.loadLibrary("hello-jni");
    }

}


/* ----------------------------------------------------  Jni code
--------------------------------------------------------------------
*/

/* Global data */
JavaVM* javaVM;
jobject obj;

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env_var,
                                                  jobject thiz)
{
        /* here i am initializing few  native threads */
        return (*env)->NewStringUTF(env, "Hello from JNI!");
}


oneThreadFun()
{
    int status;
    JNIEnv *env;
    bool isAttached = false;

    status = (*javaVM)->GetEnv(javaVM, (void **) &env,
JNI_VERSION_1_4);
    if(status < 0) {
        __android_log_print(ANDROID_LOG_ERROR, "NativeCode",
"callback_handler: failed to get JNI environment, "
             "assuming native thread");
        status = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
        if(status < 0) {
            __android_log_print(ANDROID_LOG_ERROR, "NativeCode",
"callback_handler: failed to attach "
                 "current thread");
            return;
        }
        isAttached = true;
    }
    /* Construct a Java string */
    //jstring js = env->NewStringUTF(s);
    jclass interfaceClass = (*env)->GetObjectClass(env, obj);
    if(!interfaceClass) {
        __android_log_print(ANDROID_LOG_ERROR, "NativeCode",
"callback_handler: failed to get class reference");
        if(isAttached) (*javaVM)->DetachCurrentThread(javaVM);
        return;
    }
    /* Find the callBack method ID */
    jmethodID method = (*env)->GetStaticMethodID(env, interfaceClass,
"javaFunc", "(Ljava/lang/String;)V");
    if(!method) {
        __android_log_print(ANDROID_LOG_ERROR, "NativeCode",
"callback_handler: failed to get method ID");
        if(isAttached) (*javaVM)->DetachCurrentThread(javaVM);
        return;
    }
    jstring js;
    (*env)->CallStaticVoidMethod(env, interfaceClass, method, js);
    if(isAttached) (*javaVM)->DetachCurrentThread(javaVM);
        return;
}


static jobject  io;
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
        JNIEnv *env;
        javaVM = vm;
        jclass cls;
   // LOGI("JNI_OnLoad called");
        __android_log_write(ANDROID_LOG_ERROR, "JNI_OnLoad", "JNI_OnLoad
called");
    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        //LOGE("Failed to get the environment using GetEnv()");
                __android_log_write(ANDROID_LOG_ERROR, "JNI_OnLoad", "Failed to 
get
the environment using GetEnv()");
        return JNI_VERSION_1_4;
    }


     cls = (*env)->FindClass(env, "com/examples/hellojni/HelloJni");
        if(!cls) {
        __android_log_write(ANDROID_LOG_ERROR, "JNI_OnLoad",
"initClassHelper: failed to get  class reference");
        return JNI_VERSION_1_4;
    }


        __android_log_print(ANDROID_LOG_ERROR, "JNI_OnLoad", "FindClass->
%p", cls);

        jmethodID constr = (*env)->GetMethodID(env, cls, "<init>", "()V");
        if(!constr) {
         __android_log_write(ANDROID_LOG_ERROR, "JNI_OnLoad",
"initClassHelper: failed to get constructor");
        return JNI_VERSION_1_4;
    }

        __android_log_print(ANDROID_LOG_ERROR, "JNI_OnLoad", "GetMethodID ->
%p", constr);

        obj = (*env)->NewObject(env, cls, constr);
    if(!obj) {
        __android_log_print(ANDROID_LOG_ERROR, "MY-ALSA",
"initClassHelper: failed to create a  object");
        return JNI_VERSION_1_4;
    }
    obj = (*env)->NewGlobalRef(env, obj);
    return JNI_VERSION_1_4;
}

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: http://groups.google.com/group/android-porting

Reply via email to