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

René Hansen renehh at gmail.com
Sat Jan 26 18:18:35 CET 2019


I think I've found the problem. It seems wrapping the call to
*callStaticMethod* like this:

QtAndroid::runOnAndroidThread([jdata] {
  ...
});

Had the effect of changing something about the type of jdata. If I remove
it out, that makes call pass just fine. I'm not sure how to run on the
android ui thread then, but I guess that's a different problem then.


/René

On Sat, 26 Jan 2019 at 12:35 René Hansen <renehh at gmail.com> wrote:

> I don't know if there's a difference between *QAndroidJniEnvironment* and
> *QtAndroidExtras/QAndroidJniEnvironment*.
>
> I tried a few different things, also e.g. declaring:
>
> const jbyte foo[] = { 0x4c, 0x6f, 0x72 ,0x65, 0x6d };
>
> Directly to avoid the cast, but the same thing is happening. I still see:
>
> JNI ERROR (app bug): attempt to pass an instance of java.lang.String[] as
> argument 1 to int Foo.bar(byte[])
>
> So either *jbyteArray* is simply not the right type to pass to
> *callStaticMethod*, for a *byte[]* parameter type, or some magic
> conversion is happening in the Qt wrapper classes.
>
>
> /René
>
> On Sat, 26 Jan 2019 at 01:01 Jason H <jhihn at gmx.com> wrote:
>
>> So I think i got that completely wrong.  I'm more used to using
>> androidextras. They have a nice API that cleans up for you. I gotta that's
>> not an option?  Maybe they JNI stuff should get it's own module separate
>> from Android?
>>
>> > Sent: Friday, January 25, 2019 at 9:00 PM
>> > From: "Jason H" <jhihn at gmx.com>
>> > To: "René Hansen" <renehh at gmail.com>
>> > Cc: interest <interest at qt-project.org>
>> > Subject: Re: [Interest] (JNI ERROR) jbytearray -> java.lang.String[] -
>> What am I doing wrong here?
>> >
>> >
>> >
>> > I'm not familiar with that, jbteArray, but in attemping your challenge,
>> I saw:
>> >
>> > qjniEnv->SetByteArrayRegion(jdata, 0, _size, reinterpret_cast<jbyte
>> *>(foo));
>> >
>> > but saw this in the docs:
>> > void Set<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize
>> start, jsize len, NativeType *buf);
>> >
>> >
>> > is jdata your jni env?
>> >
>> > Sent: Friday, January 25, 2019 at 1:13 PM
>> > From: "René Hansen" <renehh at gmail.com>
>> > To: interest <interest at qt-project.org>
>> > Subject: [Interest] (JNI ERROR) jbytearray -> java.lang.String[] - What
>> am I doing wrong here?
>> >
>> > 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_______________________________________________ Interest
>> mailing list Interest at qt-project.org
>> https://lists.qt-project.org/listinfo/interest_______________________________________________
>> Interest mailing list Interest at qt-project.org
>> https://lists.qt-project.org/listinfo/interest
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190126/dd4a3deb/attachment.html>


More information about the Interest mailing list