[Interest] (JNI ERROR) jbytearray -> java.lang.String[] - What am I doing wrong here?

René Hansen renehh at gmail.com
Fri Jan 25 19:13:14 CET 2019


All I want to do is send some bytes from Qt to Java, but I'm running
into an annoying problem, where my *jbyteArray* gets turned into
*java.lang.String[]* in mid-flight:

E art     : JNI ERROR (app bug): attempt to pass an instance of
java.lang.String[] as argument 1 to int Foo.bar(byte[])
F art     : art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN
APPLICATION: bad arguments passed to int Foo.bar(byte[]) (see above for
details)
F art     : art/runtime/java_vm_ext.cc:410]     from void
org.qtproject.qt5.android.QtNative.runPendingCppRunnables()
F art     : art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
...

Here's a minimal example; just a simple auto test project:

*TestJNI.pro:*

QT += testlib androidextras
QT -= gui

CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle

TEMPLATE = app

SOURCES +=  tst_test_jni.cpp

ANDROID_PACKAGE_SOURCE_DIR = $$PWD

DISTFILES += \
    src/Foo.java

*tst_test_jni.cpp:*

#include <QtTest>
#include <QAndroidJniEnvironment>
#include <QtAndroid>

class test_jni : public QObject
{
    Q_OBJECT

private slots:
    void test_case1();
};

void test_jni::test_case1()
{
  QAndroidJniEnvironment qjniEnv;
  jsize _size = static_cast<jsize>(5);
  jbyteArray jdata = qjniEnv->NewByteArray(_size);
  jint result;

  if (jdata == nullptr)
    QFAIL("Failed creating new jbyteArray");

  if (qjniEnv->GetArrayLength(jdata) != _size)
  {
    qjniEnv->DeleteLocalRef(jdata);
    QFAIL("Failed to allocate entire array through JNI");
  }

  // Lorem
  char foo[] = { 0x4c, 0x6f, 0x72 ,0x65, 0x6d };

  qjniEnv->SetByteArrayRegion(jdata, 0, _size, reinterpret_cast<jbyte
*>(foo));

  QtAndroid::runOnAndroidThread([jdata] {
    QAndroidJniObject::callStaticMethod<jint>(
          "Foo",
          "bar",
          "([B)I",
          jdata);
  });

  QCOMPARE(result, 42);
}

QTEST_APPLESS_MAIN(test_jni)

#include "tst_test_jni.moc"

*Foo.java:*

public class Foo {
  static int bar(byte baz[]) {
      return 42;
  }
}

Have I missed something very basic here?


Best regards,

René Hansen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190125/9c5f2664/attachment.html>


More information about the Interest mailing list