0

I'm building an android app which uses NDK and Java. The app is working fine in debug mode. But when I try to build release apk. It starts crashing. After debugging the release APK I found that

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "example.motion.MotionDetectionReturnValue"

Here's the problem from my .cpp class

jobject object;
    jmethodID constructor;
    jclass cls;

    cls = env->FindClass("example/motion/MotionDetectionReturnValue");
    constructor = env->GetMethodID(cls, "<init>", "(DDDDDDD)V");
    object = env->NewObject(cls, constructor, avg.x, avg.y, totalFraction, bottomRightFraction, bottomLeftFraction, topRightFraction, topLeftFraction);

should I attach it to java thread or something like that. I tried but I didn't get it

What am I missing ? ..Thanks

Mohamed Saber
  • 832
  • 1
  • 10
  • 16
  • _"should I attach it to java thread"_ Impossible to say without more context. If the call to the native code originated in Java and you got a `JNIEnv*` that way, then you shouldn't attach the thread yourself. If this is on a purely native thread, then you need to attach it. – Michael Sep 13 '19 at 13:13
  • Instead of making wild guesses about attaching to threads, you should read the error message. You have a `ClassNotFoundExfeotuon`. That has a predictable cause and has nothing to do with threads. Obviously `FindClass()` is failing, and your code omitted to detect that and instead proceeded as though there was no error. You must error-check every JNI call you make and react accordingly. – user207421 Sep 13 '19 at 22:13
  • @user207421 I found this answer for similar error : https://stackoverflow.com/a/44440800/7555345 .. That's why I was confused about attaching it to Java thread.. anyway I figured that I shall not – Mohamed Saber Sep 13 '19 at 22:41

1 Answers1

3

The typical difference between debug and release builds is that the latter turns on ProGuard obfuscation. This means that names of many classes and methods are automatically changed. Probably, this is what happened to the class example.motion.MotionDetectionReturnValue. The easy fix is to keep the names of classes and methods that are involved in JNI, without obfuscation. See https://stackoverflow.com/a/24923279/192373.

More advanced techniques allow to protect these classes, too

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • That seems to be the problem. Is there any link to how dontobfuscate the native method? – Mohamed Saber Sep 13 '19 at 13:50
  • You should turn off obfuscation of Java classes and methods used from c++ code. See https://stackoverflow.com/a/24923279/192373. Actually, names of native methods should not be obfuscated, too. – Alex Cohn Sep 13 '19 at 21:09