title: ClassNotFoundException when finding a class in JNI background thread
body: <p>My Android app gives me a ClassNotFoundException when I try to create an ArrayList on a background thread in my C++ code.</p>
  
  <p>In JNI_OnLoad() I stash aside my class loader:</p>
  
  <pre><code>jclass clsMine = env-&gt;FindClass("com/mypackage/MyClass");
  jclass classClass = env-&gt;GetObjectClass(clsMine);
  jclass classLoaderClass = env-&gt;FindClass("java/lang/ClassLoader");
  jmethodID getClassLoaderMethod = env-&gt;GetMethodID(classClass, "getClassLoader", "()Ljava/lang/ClassLoader;");
  gClassLoader = env-&gt;NewGlobalRef(env-&gt;CallObjectMethod(clsMine, getClassLoaderMethod));
  gFindClassMethod = env-&gt;GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
  </code></pre>
  
  <p>I have a findClass() method which I can call from other threads:</p>
  
  <pre><code>static jclass findClass(JNIEnv* env, const char* name)
  {
      return static_cast&lt;jclass&gt;(env-&gt;CallObjectMethod(gClassLoader, gFindClassMethod, env-&gt;NewStringUTF(name)));
  }
  </code></pre>
  
  <p>I can use this to get access to MyClass and its inner classes and interfaces. But when I try to create an ArrayList, my findClass() throws a ClassNotFoundException.</p>
  
  <p>I realized that if my Java code creates an ArrayList before I load my native library, then I don't get an exception. Or if I <code>env-&gt;FindClass("java/util/ArrayList")</code> in JNI_OnLoad() then I don't get an exception. But these are work-arounds. Why can't I create Java classes using the class loader I stashed aside? Or should I be using a different class loader?</p>
  
  <p>EDIT:</p>
  
  <p>And to add a wrinkle, under ART my work-around to call <code>env-&gt;FindClass("java/util/ArrayList")</code> in JNI_OnLoad() doesn't work.</p>