解决jni调用在Android5.x系统闪退问题

时间:2015-08-14 19:17:42   收藏:0   阅读:9684

日志信息如下:
08-14 15:48:41.127: A/art(5526): art/runtime/check_jni.cc:70] JNI DETECTED ERROR IN APPLICATION: illegal class name ‘xxx.xxx.xxx’
08-14 15:48:41.127: A/art(5526): art/runtime/check_jni.cc:70] (should be of the form ‘package/Class’, [Lpackage/Class;’ or ‘[[B’)
08-14 15:48:41.127: A/art(5526): art/runtime/check_jni.cc:70] in call to FindClass

check_jni.cc源码地址:https://android.googlesource.com/platform/art/+/kitkat-dev/runtime/check_jni.cc

看日志是在调用FindClass是出错的。在FindClass的时候会调用CheckClassName方法检查类名是否合法。CheckClasName方法源码如下:

  // Checks that ‘class_name‘ is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
  // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
  // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
  // circumstances, but this is incorrect.
  void CheckClassName(const char* class_name) {
    if (!IsValidJniClassName(class_name)) {
      JniAbortF(function_name_,
                "illegal class name ‘%s‘\n"
                "    (should be of the form ‘package/Class‘, [Lpackage/Class;‘ or ‘[[B‘)",
                class_name);
    }
  }



在CheckClassName方法中调用了IsValidJniClassName方法,通过google搜索”IsValidJniClassName site:android.googlesource.com”,发现IsValidJniClassName方法的声明在utils.h中,实现在utils.cc中。
技术分享



下面是utils.h和utils.cc的链接地址:
https://android.googlesource.com/platform/art/+/master/runtime/utils.h
https://android.googlesource.com/platform/art/+/kitkat-release/runtime/utils.cc

IsValidJniClassName方法实现如下:
技术分享
他调用了IsValidClassName方法,注意第三个参数是‘/‘,这是类名的分隔符,而我这里调用FindClass时类名是使用了‘.‘分隔,改成‘/‘,问题就解决了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!