Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Dear,
I am creting an android application over JNI.i am created some files in java likes
MainActivity.java

Java
package com.allwikan.runkey;


import com.allwikan.runkey.MainActivity;
import com.allwikan.runkey.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	
	 private LinearLayout linearLayout;
		private   InputMethodManager inputMethodManager;
		 private Keyboard lkeyboard;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lkeyboard=new Keyboard(MainActivity.this);
        linearLayout=(LinearLayout)findViewById(R.id.myLayout);
        Button button=(Button)findViewById(R.id.button1);

     button.setOnClickListener(new OnClickListener() {
	
	@Override
	public void onClick(View v) {
	
		// TODO Auto-generated method stub
		// inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
		lkeyboard.getSystemService(null);
		//lkeyboard.openKeyBoard();
	}
});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Keyboard.java

Java
package com.allwikan.runkey;
import android.R.bool;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
public class Keyboard {


     static {
           System.loadLibrary("com_allwikan_runkeyboard_Keyboard");
        }

    Context mContext;
     public Keyboard(Context mContext){
           this.mContext = mContext;
      }

      public  void  getSystemService(bool makevisible){
         if(makevisible != null){
            InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            m.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
         }
         else
         {
             InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            m.toggleSoftInput(0, 0);
         }
         }
      public native void  openKeyBoard();
}


It display the keyboard in emuletor.but my requirement call by c over jni then we created two file .likes

com_allwikan_runkey_Keyboard.h


C++
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_allwikan_runkey_Keyboard */

#ifndef _Included_com_allwikan_runkey_Keyboard
#define _Included_com_allwikan_runkey_Keyboard
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_allwikan_runkey_Keyboard
 * Method:    openKeyBoard
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_allwikan_runkey_Keyboard_openKeyBoard
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


com_allwikan_runkey_Keyboard.c



C++
#include <com_allwikan_runkey_keyboard.h>
#include "stdio.h"

#include "jni.h"
#include 


#define  LOG_TAG    "com_allwikan_runkeyboard_Keyboard"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
int main()
{


	JNIEnv *env;
	     JavaVM *jvm;
	     jint res;
	     jclass cls;
	     jmethodID mid;
	     jstring jstr;
	     jclass stringClass;
	     jobjectArray args;


	 #ifdef JNI_VERSION_1_2
	     JavaVMInitArgs vm_args;
	     JavaVMOption options[1];
	   //  options[0].optionString = ("-Djava.class.path=") USER_CLASSPATH;
	     vm_args.version = 0x00010002;
	     vm_args.options = options;
	     vm_args.nOptions = 1;
	     vm_args.ignoreUnrecognized = JNI_TRUE;
	     /* Create the Java VM */
	     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
	 #else
	     JDK1_1InitArgs vm_args;
	     char classpath[1024];
	     vm_args.version = 0x00010001;
	     JNI_GetDefaultJavaVMInitArgs(&vm_args);
	     /* Append USER_CLASSPATH to the default system class path */
	     sprintf(classpath, "%s%c%s",
	             vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
	     vm_args.classpath = classpath;
	     /* Create the Java VM */
	     res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
	 #endif /* JNI_VERSION_1_2 */

	     if (res < 0) {
	         fprintf(stderr, "Can't create Java VM\n");
	         exit(1);
	     }
	     cls = (*env)->FindClass(env, "Prog");
	     if (cls == NULL) {
	         goto destroy;
	     }

	     mid = (*env)->GetStaticMethodID(env, cls, "main",
	                                     "([Ljava/lang/String;)V");
	     if (mid == NULL) {
	         goto destroy;
	     }
	     jstr = (*env)->NewStringUTF(env, " from C!");
	     if (jstr == NULL) {
	         goto destroy;
	     }
	     stringClass = (*env)->FindClass(env, "java/lang/String");
	     args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
	     if (args == NULL) {
	         goto destroy;
	     }
	     (*env)->CallStaticVoidMethod(env, cls, mid, args);

	 destroy:
	     if ((*env)->ExceptionOccurred(env)) {
	         (*env)->ExceptionDescribe(env);
	     }
	     (*jvm)->DestroyJavaVM(jvm);



}


When compile this code by ndk-build commond its give error

/home/allwikan/Documents/eclipse_workspace/RunKey/jni/com_allwikan_runkey_Keyboard.c:33: undefined reference to `JNI_CreateJavaVM'
collect2: ld returned 1 exit status


Please help me.
(1)How to resolve this error
(2)how to keyboard display in emulator
(3)which function used in c for display keyboard.


Thanks ...



I have also two file like


Android.mk

LOCAL_PATH :=$(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

LOCAL_PATH := $(MY_PATH)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE := com_allwikan_runkey_Keyboard
LOCAL_SRC_FILES := com_allwikan_runkey_Keyboard.c
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)




and

application.mk

APP_ABI :=all



then where am i wrong.


please help me....
Posted
Updated 15-Sep-12 3:28am
v3
Comments
Richard MacCutchan 15-Sep-12 11:22am    
Where is JNI_CreateJavaVM() defined? Probably in some JNI system library which you have not added to your build parameters, as I explained earlier.

1 solution

You are missing a library or LIB directive in your project, so the linker cannot resolve the reference. However, I am not sure that you can directly load a C executable into an Android system.
 
Share this answer
 
Comments
Tn1111 15-Sep-12 12:37pm    
Thanks for giving reply
but i have also file android.mk where BUILD_SHARED_LIBRARY so the code is compiling but gives on error
undefined reference to `JNI_CreateJavaVM' so how to resoolve it.


thanks
Richard MacCutchan 15-Sep-12 12:51pm    
How many more times do I need to explain that you are missing a library in your build, which contains the definition for the missing reference? If you do not know how to compile and link a program then take a look at the documentation, starting with the man pages for make and ld.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900