PackageManager$NameNotFoundException fo JNI project - android

Getting PackageManager$NameNotFoundException for the signature capturing code in android studio
package com.example.u29692.helloandroidjni;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.jni_msgView)).setText(stringFromJNI(this));
}
// new code
static {
System.loadLibrary("hello-android-jni");
}
// public native String getMsgFromJni();
public native String stringFromJNI(Context context);
jni.c
#include <jni.h>
JNIEXPORT jstring JNICALL
Java_com_example_u29692_helloandroidjni_MainActivity_stringFromJNI(JNIEnv *env, jobject instance,
jobject context) {
// TODO
// For class Context
jclass native_clazz = (*env)->GetObjectClass(env, context);
// Get the getPackageManager method ID
jmethodID methodID_func = (*env)->GetMethodID(env, native_clazz,
"getPackageManager", "()Landroid/content/pm/PackageManager;");
// Access manager application package
jobject package_manager = (*env)->CallObjectMethod(env, context, methodID_func);
// For class PackageManager
jclass pm_clazz = (*env)->GetObjectClass(env, package_manager);
// Get the getPackageInfo method ID
jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz,
"getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// To obtain the application package information
jobject package_info = (*env)->CallObjectMethod(env, package_manager,
methodID_pm, (*env)->NewStringUTF(env, "com.example.u29692.helloandroidjni"), 64);
return (*env)->NewStringUTF(env, package_info);
}
Application is crashing with the logs:
10-14 12:12:55.038: A/art(17135): sart/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception 'android.content.pm.PackageManager$NameNotFoundException' thrown in unknown throw location
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] Pending exception android.content.pm.PackageManager$NameNotFoundException thrown by 'unknown throw location'
0-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] android.content.pm.PackageManager$NameNotFoundException: com.example.u29692.helloandroidjni
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] at android.content.pm.PackageInfo android.app.ApplicationPackageManager.getPackageInfo(java.lang.String, int) (ApplicationPackageManager.java:167)
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] at java.lang.String

Related

java lang class exception -Unable to cast the java vector<user defined java class> return from c++ jni object callback

I am not able to cast back the user self defined class return from C++ jni callback
code snippet as follow:
//Kotlin class
data class class_record(var id:String?,var class_name:String?,var class_type)
// com.example.MainActivity
public native Object Cls(String id);
Vector vec_ classrecord=new Vector();
vec_classrecord=(Vector)Cls("1234");
// c++ jni code
extern "C"
JNIEXPORT jobject JNICALL
Java_com_example_MainActivity_Cls(JNIEnv *env, jobject instance, jstring id,
) {
jclass java_vector_class;
jmethodID java_vector_method;
jobject java_vector_object ;
auto vec_record=// call c++ method that return vector for class record pointer
jstring jni_str;
jclass javaClassRef;
// jni for java.util.Vector
java_vector_class = env->FindClass("java/util/Vector");
java_vector_method_constructor = env->GetMethodID(java_vector_class, "<init>", "()V");
java_vector_object = env->NewObject(java_vector_class, java_vector_method_constructor, "");
for (auto record_it = vec_record.begin(); record_it < vec_record.end(); ++record_it) {
// jni for class_record
jclass java_class = env->FindClass("com/example/class_record");
javaClassRef = (jclass) env->NewGlobalRef(java_class);
jmethodID cls_constructor = env->GetMethodID(javaClassRef, "<init>", "()V");
jobject cls_object = env->NewObject(javaClassRef, cls_constructor, "");
// set id
javaMethodRef = env->GetMethodID(javaClassRef, "setId", "(Ljava/lang/String;)V");
std::string strval = record_it.id;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
// set class_name
javaMethodRef = env->GetMethodID(javaClassRef, "setClass_name","(Ljava/lang/String;)V");
std::string strval = record_it.class_name;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
//set class_type
javaMethodRef = env->GetMethodID(javaClassRef, "setClass_type","(Ljava/lang/String;)V");
std::string strval = record_it.class_type;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
jmethodID java_vector_add = env->GetMethodID(java_vector_class, "addElement","(Ljava/lang/Object;)V");
**env->CallVoidMethod(java_vector_object, java_vector_add, javaClassRef);**
}
return java_vector_object;
}
env->CallVoidMethod(java_vector_object, java_vector_add, cls_object);
Under Kotlin environment , it it much better to express jni c++ callback in ArrayList instead of vector which grow almost double the size with snippet as follow in situation where the returned arraylist is fixed size immuatable.
java_util_class = env->FindClass("java/util/ArrayList");
jmethodID java_add= env->GetMethodID(java_util_class, "add","(Ljava/lang/Object;)Z");
env->CallBooleanMethod(java_object, java_add, cls_object);
sample tutorial

Android JNI call Java method from native thread

I am trying to call a method in my java class from within a thread in the native code but not having any success. These are the global vars:
JavaVM* javaVM = NULL;
jclass activityClass;
jobject activityObj;
Code called on initialisation of the native code:
extern "C" {
JNIEXPORT jint JNICALL
naInit(JNIEnv *pEnv, jobject pObj, jstring pFileName, jstring, defaultStorageDirectory) {
pEnv->GetJavaVM(&javaVM);
jclass cls = pEnv->GetObjectClass(pObj);
activityClass = reinterpret_cast<jclass>((jclass) pEnv->NewGlobalRef(cls));
activityObj = pEnv->NewGlobalRef(pObj);
}
}
Code used within the thread function:
void *decodeAndRender(void * args) {
JNIEnv *env;
javaVM->AttachCurrentThread(&env, NULL);
jmethodID retryStartVideoMethodID = env->GetMethodID(activityClass, "retryStartVideo", "()V");
env->CallVoidMethod(activityObj, retryStartVideoMethodID);
javaVM->DetachCurrentThread();
return 0;
}
Java code :
public void retryStartVideo() {
Log.d(TAG, "METHOD CALLED FROM CPP ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
Error in logcat:
JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.NoSuchMethodError: no non-static method "Ljava/lang/Class;.retryStartVideo()V"
I have set the instances of the calling class to be a global ref and used that when calling from within the thread function but it is still failing to find the method. I can use similar code to call a static method no problem but I need to be able to call a non static one.

Get app signature hash code in jni

i want to get app signature in native with these code: (source)
// For class Context
jclass native_clazz = (*env)->GetObjectClass(env, context);
// Get the getPackageManager method ID
jmethodID methodID_func = (*env)->GetMethodID(env, native_clazz,
"getPackageManager", "()Landroid/content/pm/PackageManager;");
// Access manager application package
jobject package_manager = (*env)->CallObjectMethod(env, thiz, methodID_func);
// For class PackageManager
jclass pm_clazz = (*env)->GetObjectClass(env, package_manager);
// Get the getPackageInfo method ID
jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz,
"getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// To obtain the application package information
jobject package_info = (*env)->CallObjectMethod(env, package_manager,
methodID_pm, (*env)->NewStringUTF(env, "com.example.hellojni"), 64);
but i got this:
error: 'context' undeclared (first use in this function)
how do i get context in native?
how do i return signature hashcode as string?
You can send the context to JNI in a parameter for example
jstring Java_packagename_stringFromJNI( JNIEnv* env, jobject thiz , jobject context)
And when you call the method in your activity:
public native String stringFromJNI(Context context);
...
Log.d(TAG, "Receive: " + stringFromJNI(this));

JNI: Segfaults when calling Java callbacks from C

I a Library in C that I'm leveraging for an Android application. This library has an audio stream that it occasionally flushes. When this happens it calls a write callback function of my design.
My intent is to have that C callback call a method on a specific Java Object which will handle stuff with the strem.
Currently I have code like so:
methodID compressionHandler=0;
jobject compressionHandlerClass;
int audioBufferChunkSize;
static JavaVM *gJavaVM;
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
gJavaVM = vm;
return JNI_VERSION_1_6;
}
JNIEXPORT void JNICALL
Java_com_my_code_init(JNIEnv* env, jobject obj, /*classpath of the class we want to call against*/jstring compressedAudioHandlerPath, /*class instance we want to call against*/jobject callbackClass) {
......
// this is a global ref as per:
//http://stackoverflow.com/questions/14765776/jni-error-app-bug-accessed-stale-local-reference-0xbc00021-index-8-in-a-tabl
compressionHandlerClass = (*env)->NewGlobalRef(env,callbackClass);
// name of the class
const char *classLocation;
// convert jString to c String
classLocation = (*env)->GetStringUTFChars( env, compressedAudioHandlerPath , NULL ) ;
// tmp variable for holding the class location, relates to the above issue with garbage collection
jclass clazz = (*env)->FindClass(env, classLocation);
// the actual method that we want to call, this gets used in the writeCallback
compressionHandler = (*env)->GetMethodID(env, clazz, "handleCompressedAudio", "([B)V");
......
}
The callback method looks like so:
void writeCallback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data) {
JNIEnv *env;
int isAttached = 0;
if ((status = (*gJavaVM)->GetEnv(gJavaVM, (void**)&env, JNI_VERSION_1_6)) < 0) {
if ((status = (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL)) < 0) {
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
isAttached = 1;
}
if(*env!=0 && compressionHandler!=0){
jbyteArray arr = (*env)->NewByteArray(env,bytes);
(*env)->SetByteArrayRegion(env,arr, 0, bytes, (jbyte*)buffer);
(*env)->CallVoidMethod(env,compressionHandlerClass, compressionHandler,arr);
free(arr);
free(env);
free(isAttached);
}
}
I'm getting crashes at the CallVoidMethod, that signature of which is an interface implemented by whatever object I pass in:
public interface CompressedAudioHandler {
void handleCompressedAudio(byte[] buff);
}
I suspect that I am improperly attaining/keep references to these objects, but I haven't found a great way to handle that. Any advice on how I can more correctly handle this?

How to fix JNI crash on env->NewObject()?

Here is my program:
extern "C" {
JNIEXPORT jint Java_android_app_integrity_VerifyIntegrity_checkCrc(JNIEnv *jniEnv,jobject thiz,jstring crcStr) {
jclass clsZipFile = jniEnv->FindClass("java/util/zip/ZipFile");
jmethodID mtdConstruct = jniEnv->GetMethodID(clsZipFile, "<init>", "(Ljava/lang/String;)V");
jmethodID mtdGetEntry = jniEnv->GetMethodID(clsZipFile,"getEntry","(Ljava/lang/String;)Ljava/util/zip/ZipEntry;");
jclass clsZipEntry = jniEnv->FindClass("java/util/zip/ZipEntry");
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()L");
LOGD("pos2");
jobject objZipFile = jniEnv->NewObject(clsZipFile,mtdConstruct,crcStr);
if (NULL == objZipFile){
LOGD("NULL == objZipFile");
}
LOGD("pos3");
jobject objZipEntry = jniEnv->CallObjectMethod(objZipFile, mtdGetEntry,"classes.dex");
LOGD("pos4");
jlong ret = jniEnv->CallLongMethod(objZipEntry, mtdGetCrc);
LOGD("%ld",(long int)ret);
return 0;
}
};
It only print "pos2". The line below the "LOGD("pos2");" will cause crash!
I can't find the reason. Who can help me? Thx!
Try fixing the following line. It has an invalid signature and will cause an implicit exception to be thrown for MethodNotFound exception and is likely the culprit.
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()L");
Should be:
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()J");
However I would second other suggestions to check return values from all FindClass and FindMethod calls as they not only return NULL they also each throw an exception upon failure. Also OutOfMemoryException is thrown when JNI can not allocate a local reference object to return for your jclass lookups.

Categories

Resources