Android 4.4及更早版本的GetFieldID失败

时间:2016-10-07 11:18:31

标签: android android-ndk java-native-interface

我想使用dlib库进行人脸检测,但它不适用于旧手机。

我崩溃了以下堆栈跟踪:

E/AndroidRuntime( 4288): FATAL EXCEPTION: pool-1-thread-1
E/AndroidRuntime( 4288): java.lang.ExceptionInInitializerError
E/AndroidRuntime( 4288):    at com.tzutalin.dlibtest.MainActivity.runDetectAsync(MainActivity.java:235)
E/AndroidRuntime( 4288):    at com.tzutalin.dlibtest.MainActivity_.access$301(MainActivity_.java:34)
E/AndroidRuntime( 4288):    at com.tzutalin.dlibtest.MainActivity_$6.execute(MainActivity_.java:158)
E/AndroidRuntime( 4288):    at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:405)
E/AndroidRuntime( 4288):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
E/AndroidRuntime( 4288):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime( 4288):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime( 4288):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:150)
E/AndroidRuntime( 4288):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:264)
E/AndroidRuntime( 4288):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime( 4288):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime( 4288):    at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime( 4288): Caused by: java.lang.NoSuchFieldError: no field with name='mLabel' signature='java/lang/String' in class Lcom/tzutalin/dlib/VisionDetRet;
E/AndroidRuntime( 4288):    at com.tzutalin.dlib.PeopleDet.jniNativeClassInit(Native Method)
E/AndroidRuntime( 4288):    at com.tzutalin.dlib.PeopleDet.<clinit>(PeopleDet.java:42)
E/AndroidRuntime( 4288):    ... 12 more

所以,C代码看起来像这样:

void JNIEXPORT DLIB_JNI_METHOD(jniNativeClassInit)(JNIEnv* env, jclass _this) {
  jclass detRetClass = env->FindClass("com/tzutalin/dlib/VisionDetRet");
  CHECK_NOTNULL(detRetClass);
  gVisionDetRetOffsets.label =
      env->GetFieldID(detRetClass, "mLabel", "java/lang/String");
  gVisionDetRetOffsets.confidence =
      env->GetFieldID(detRetClass, "mConfidence", "F");
  gVisionDetRetOffsets.left = env->GetFieldID(detRetClass, "mLeft", "I");
  gVisionDetRetOffsets.top = env->GetFieldID(detRetClass, "mTop", "I");
  gVisionDetRetOffsets.right = env->GetFieldID(detRetClass, "mRight", "I");
  gVisionDetRetOffsets.bottom = env->GetFieldID(detRetClass, "mBottom", "I");
  gVisionDetRetOffsets.addLandmark =
      env->GetMethodID(detRetClass, "addLandmark", "(II)Z");
  if (gVisionDetRetOffsets.addLandmark == NULL) {
    LOG(FATAL) << "Can't Find Method addLandmark(int,int)";
  }
  LOG(INFO) << "JniNativeClassIni Success";
}

相关的Java类:

public final class VisionDetRet {
    private String mLabel;
    private float mConfidence;
    private int mLeft;
    private int mTop;
    private int mRight;
    private int mBottom;
    private ArrayList<Point> mLandmarkPoints = new ArrayList<>();

    VisionDetRet() {
    }

    // Other code
}

因此,字段显然在这里,它获得了类(CHECK_NOTNULL(detRetClass)成功)。那为什么它会在旧手机上崩溃? Google没有多大帮助,而且我对NDK / JNI的新东西也很陌生。

P上。 S. Java文件是here,C文件是here(是的,它们在2个不同的存储库中)

1 个答案:

答案 0 :(得分:3)

我不确定这对任何Android版本的效果如何,因为java/lang/StringGetFieldID的错误签名。

对于课程,签名应为L fully-qualified-class ;,因此在您的情况下,您需要Ljava/lang/String;