android Flurry integration with NDK app
Asked Answered
U

1

8

My Android application comprises two parts: frontend written in Java and game written in C++ using NativeActivity NDK stuff. I have a problem integrating Flurry into my application. Flurry works fine from within Java part, but crashes from within C++. More specifically, call

jni_env->FindClass("com/flurry/android/FlurryAgent");

results in ClassNotFoundException.

jni_env variable is not broken because I am able to get some Intent params using it.

FlurryAgent.jar is added to libs dir and into .classpath. I've even checked 'Order and Export' checkbox for FlurryAgent.jar (though I have no idea what does it mean). Nothing helps.

One more detail: my application is divided into Library and App parts. I have added FlurryAgent.jar to both parts and checked 'Order and Export' in both parts, but it still does not help. Clean & rebuild does does not help either. Did I miss something?

Unbated answered 29/1, 2013 at 15:50 Comment(0)
U
10

The answer is here: http://archive.is/QzA8

In other words, NativeActivity cannot find a third-party class and instead of

jni_env->FindClass("com/flurry/android/FlurryAgent");

one should use

jobject nativeActivity = state->activity->clazz;
jclass acl = jni_env->GetObjectClass(nativeActivity);
jmethodID getClassLoader = jni_env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cls = jni_env->CallObjectMethod(nativeActivity, getClassLoader);
jclass classLoader = jni_env->FindClass("java/lang/ClassLoader");
jmethodID findClass = jni_env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring strClassName = jni_env->NewStringUTF("com.flurry.android.FlurryAgent");
jclass flurryClass = (jclass)(jni_env->CallObjectMethod(cls, findClass, strClassName));
jni_env->DeleteLocalRef(strClassName);
Unbated answered 30/1, 2013 at 14:2 Comment(6)
That is indeed the working method to have it load. However, using that, dalvik reports: W/dalvikvm(21811): dvmFindClassByName rejecting 'com/flurry/android/FlurryAgent'. Any ideas why?Birdsong
I'm also getting the same "dvmFindClassByName rejecting -class-". Did you found a solution?Crinkleroot
The argument to ClassLoader.loadClass() is a "binary name", e.g. java.lang.String (see the JLS or the ClassLoader javadoc). Replace occurrences of '/' with '.' in the NewStringUTF call.Avens
This answer works for me! Just a small correction: the link above is dead, here is the same post: hi.baidu.com/letsherwel/item/23577c081e3b107fbfe97e23Frenchify
the link is broken. may be it should be replaced with the one proposed by @dextor?Pleonasm
JNIEnv* jni_env = state->activity->env; if (state->activity->vm->AttachCurrentThread(&jni_env, NULL) != 0) { std::cout << "Failed to attach" << std::endl; } might be needed before jclass acl = jni_env->GetObjectClass(nativeActivity);Airily

© 2022 - 2024 — McMap. All rights reserved.