0

I'm writing a native app with a Java class derived from NativeActivity. I need to get a reference to that class in my C++ code, but when I try to do that in code called from ANativeActivity_onCreate the class can't be found. My code looks something like:

void ANativeActivity_onCreate(ANativeActivity *activity,
        void *savedState, size_t savedStateSize)
{
    ...
    JNIEnv *jenv = activity->env;
    dsActivityClass = jenv->FindClass("uk/co/realh/hgame/HGameActivity");
    if (!dsActivityClass)
    {
        // Log error and return to Java
    }
    ...
}

My logcat shows the error printed by the if clause followed by a report of a java.lang.NoClassDefFoundError.

I've double-checked the class name, I'm sure it's correct. And the class has to have been instantiated by the time the above code is called because it includes a static code block to load gnustl_shared, without which Android can't load my main library containing ANativeActivity_onCreate. What could cause the class to not be found? It's defined like this:

package uk.co.realh.hgame;

import android.app.NativeActivity;
...

public class HGameActivity extends NativeActivity {
...
}
realh
  • 962
  • 2
  • 7
  • 22

1 Answers1

0

According to the link fadden posted (1) creating a native thread can cause the class loader to fail to find your classes. So something to do with ANativeActivity_onCreate being called from a separate thread seems to be the most likely explanation for this problem.

There is an easy way around the problem in this case:

dsActivityClass = jenv->GetObjectClass(activity->clazz);
realh
  • 962
  • 2
  • 7
  • 22